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

Hello, Great Ones! Does anybody know a way to monitor CPU load and send a notification via email if load is over 95% for a specified time. The reason for this is I have 70 users with X sessions working off one server and once in a while, a process goes crazy (usually netscape) and because I can't watch xosview all day, I need another solution. I looked at bigbrother and netsaint, but I don't need all that. Something simpler would be better. (RH 7.2 w/dual PIII 700's)Any ideas greatly appreciated. Thanks in advance!

Replies are listed 'Best First'.
Re: CPU Load Notification
by valdez (Monsignor) on Sep 26, 2002 at 18:35 UTC

    You can start with Sys::CpuLoad, it works under Linux and BSD; then you can glue it to Proc::ProcessTable.

    Ciao, Valerio

    Update: i wrote this little test:

    #!/usr/bin/perl use strict; use Sys::CpuLoad; use Proc::ProcessTable; use vars qw( $sleep_time @memory $memory_size $count $threshold $progr +am @pids ); $program = '/usr/bin/perl'; $threshold = 1; #load $sleep_time = 1; #seconds $memory_size = 10; $count = 0; while (1) { if (load_average() > $threshold) { @pids = search_procs(); if (@pids) { killall(@pids); } } $count++; sleep $sleep_time; } exit; sub load_average { my ($last, $average); $last = (Sys::CpuLoad::load())[0]; $memory[$count % $memory_size] = $last; $average += $_ foreach @memory; $average = $average / $memory_size; return $average; } sub search_procs { my ($ppt, $proc, @pids); $ppt = new Proc::ProcessTable; foreach $proc (@{$ppt->table}) { next unless ($proc->{exec} eq $program); push @pids, $proc->{pid}; } return @pids; } sub killall { print join(' ', @_), "\n"; }

    This code is only a proof of concept.

Re: CPU Load Notification
by samurai (Monk) on Sep 26, 2002 at 17:26 UTC
    The vmstat command has the "CPU idle" field. If you run that command, you can grep the output for the "id" field, and if it gets > 10, pop off an email to yourself.

    --
    perl: code of the samurai

Re: CPU Load Notification
by perrin (Chancellor) on Sep 26, 2002 at 18:51 UTC
Re: CPU Load Notification
by talexb (Chancellor) on Sep 26, 2002 at 17:17 UTC
    Not knowing that much about Linux I would probably have something that sits in a loop grabbing the output from the uptime command .. if the load numbers at the end of the line (it's the number of processes waiting, not CPU load, I know) gets into the red zone, fire off an E-Mail using Mail::Sendmail. Have it sleep for 60 seconds after each run, perhaps wait until you have three to five successive red zone numbers.

    --t. alex
    but my friends call me T.