agent has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks ! I am a newbie to perl and would like to start off by writing a script to monitor swap space. I would like to use a configuration file where i can define the thresholds. Any pointers will be highy appreciated !! Thank You Agent

Replies are listed 'Best First'.
Re: swap space monitor script
by Abigail-II (Bishop) on Sep 18, 2003 at 23:18 UTC
    Monitoring swap space is, unfortunally, not a system independent thing. On Solaris, I'd use "swap -l", on Linux, I'd use "free". On HP, I'd use an advisor script, and probably wouldn't have a need for Perl. On Windows, I'd resign and seek employment elsewhere.

    The easiest way is to use a system dependent tool, and write a wrapper around it. And I most likely use a monitoring tool, be it a free one like 'mon' or 'big brother' or a commercial one like 'openview' or 'tivoli'. Some monitoring tools will already have swap space monitoring build in. For those who don't, I'd use Perl to mangle the output of the system dependent tool into input that can be parse by the monitoring tool. Such scripts are typically 10 lines or less, but will depend highly on the used front- and back-ends.

    Abigail

Re: swap space monitor script
by BrowserUk (Patriarch) on Sep 19, 2003 at 00:17 UTC

    Just in case your on Win32, here's how to obtain that information.

    P:\test>perl -MWin32::SystemInfo CON Win32::SystemInfo::MemoryStatus(%m,'MB'); printf '%15s := %15s'.$/, $_, $m{$_} for keys %m; ^Z TotalVirtual := 2047.875 TotalPage := 501.4296875 AvailPhys := 112.84765625 TotalPhys := 223.23828125 MemLoad := 0 AvailVirtual := 2015.80859375 AvailPage := 308.515625

    Simple enough that Abigail wouldn't need to give up his day job if charged with this onerous task:)

    The significant value as far "swap space" is concerned is the last value, AvailPage. Unsurprisingly, this is the amount of page file space currently available for use. It's when this value begins to approach 0 that you will get the infamous "You are running low on system resources" popup.

    Actually, it appears to warn you when this is approach around 15MB, but I haven't been able to confirm this. Of course, but the time you reach this point, things are getting pretty sluggish, as most of the OS itself has also be swapped out by then


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: swap space monitor script
by demerphq (Chancellor) on Sep 18, 2003 at 23:06 UTC

    Ok, it would seem that you and I know about as much as each other about how to do this. Presumably you know something about swap space, where I do not, and clearly you do not know any perl whereas I do. So that makes us even. :-)

    So, where to start? Well presumably there are some tools that you use routinely to monitor and manage your swapspace. Get em out and start figuring out how to parse them. That would mean using regexes, which are documented in perlre. You mention a config file. Well, personally I use Config::Inifile or plain oldData::Dumper to do this type of stuff, but a lot of people seem to be quite keen on YAML. Of course you'll want to exec the tools from the perl side of things so you should have a look at backticks, system and perlopentut.

    Hopefully thats a good starting point?


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

    • Update:  
    Conversation in the CB has lead to me hunting down the following link How (Not) To Ask A Question which is the (IMO) canonical PM node about how to get the best out of the talents that hang out here. My node is a bit tongue in cheek because theres no code for us to help you on. Beyond what Abigail-II and I have said theres not much anybody can say. Try to reply or ask your question again with a better question and im sure youll get something that you can work with. :-)


Re: swap space monitor script
by kvale (Monsignor) on Sep 18, 2003 at 23:27 UTC
    On Linux, you could read swap usage from the /proc filesystem:
    % cat /proc/swaps Filename Type Size Used Priori +ty /dev/ide/host0/bus0/target1/lun0/part3 partition 2056312 0 + -1
    From this, a perl program:
    open IN, "</proc/swaps" or die "No swap file: $!\n"; <IN>; my @fields = split <IN>; $field[3] = 0 unless $field[3]; print "swap used = $field[3]\n";

    -Mark