Hi All,
I have written a little script which I want to monitor the cpu load of the server it is on and email me if the loads gets too high. I am having some difficulty doing this because I am trying to read the current CPU loads from /proc/loadavg virtual file, but im having trouble actually using the output, as I only want to use the first number which represents the current load ... here is what I have, any help would be greatly appreciated.
#!/usr/bin/perl
%config = (
# a) Server name
server => 'Server',
# b) Notification Email
email => 'me@me.com',
# c) Alert level (when system load > ?)
alert => '5'
);
system("cat /proc/loadavg");
while (<STDIN>) {
push @loads, $_;
}
@loads = split(/ /);
if ($loads[1] > $config{'notify'}) {
# Alert !! :)
print "@loads";
print "System [Alert] Current Load = $loads[1]\n\n";
¬ify();
} else {
print "System [OK] Current Load = $loads[1]\n\n";
}
sub notify {
$time = system("/bin/date");
open (MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $config{'email'}";
print MAIL "From: Load Monitor";
print MAIL "Subject: ALERT High Load";
print MAIL "Server Alert (Server Name $config{'server'
+})";
print MAIL "Time: $time";
print MAIL "$config{'server'} has exceeded the load al
+ert of $config{'alert'}";
close(MAIL);
}
Thanks for any advice.
John
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.