The purpose of the script is to take vmstat output and pull out the free memory and free swap and if the average falls below a set limit send out an e-mail. Is there a cleaner more Perly way to do this with less lines?
#!/usr/bin/perl -w # # Version: 1.1 # Purpose: Script determines the available free memory and swap o +n the system # and if the free memory or swap drops below a set thres +hold send e-mail. # use lib '/export/home/t31zrwf/perl_modules'; use MIME::Lite; # Limit Swap is 5GB and Memory is 3GB. $swaplim=5242880; $memlim=3145728; # Grep out lines not beginging with numbers and pump into an array. @vmstat = `vmstat 1 10 | grep -v '^ procs'| grep -v '^ r'`; # Push freeswap and freemem into separate arrays. foreach $x (@vmstat) { chomp $x; ($pos1,$pos2,$pos3,$pos4,$pos5,$pos6) = split / /, $x; push @mylist, "$pos5\n"; push @mylist1, "$pos6\n"; } # Remove first element of array because first line of vmstat gives information since boot. shift @mylist; foreach $z (@mylist) { chomp $z; $tswap += $z; } # Get average free swap. $avgswap = ($tswap/($#mylist+1)); # Remove first element of array because first line of vmstat gives information since boot. shift @mylist1; foreach $z (@mylist1) { chomp $z; $tmem += $z; } # Get average free memory. $avgmem = ($tmem/($#mylist1+1)); open(FILE,"> /tmp/error.out") || die "Can't open:$!\n"; if ($avgswap <= $swaplim) { print FILE "WARNING!!!!WARNING!!!!WARNING!!!!\n"; printf FILE "The average free swap is: %d\n", $avgswap; print FILE "The average free swap should be: $swaplim\n"; } if ($avgmem <= $memlim) { print FILE "WARNING!!!!WARNING!!!!WARNING!!!!\n"; printf FILE "The average freemem is: %d\n", $avgmem; print FILE "The average free memory should be: $memlim\n"; } close(FILE); # Set system name. chomp($sys_name=`uname -n`); $msg = new MIME::Lite; $msg->build( To => 'email_address', Subject => "Test from $sys_name", Type => 'text', Path => '/tmp/error.out', ); # Send e-mail or DIE! $msg->send('sendmail') || die "Can't open:$!\n"; # Remove temp file unlink "/tmp/error.out";

In reply to vmstat threshold script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.