Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: vmstat threshold script
by gellyfish (Monsignor) on Jun 23, 2003 at 16:52 UTC | |
|
Re: vmstat threshold script
by ant9000 (Monk) on Jun 23, 2003 at 17:25 UTC | |
by Anonymous Monk on Jun 23, 2003 at 18:10 UTC | |
|
Re: vmstat threshold script
by jkenneth (Pilgrim) on Jun 24, 2003 at 15:11 UTC | |
|
Re: vmstat threshold script
by CountZero (Bishop) on Jun 23, 2003 at 21:40 UTC |