# Register this routine with the plugin reqistry
$CHECKFUNCS{'cpu'} = \&check_cpu;

# This routine checks the 5min CPU load avg from the uptime comand. This
# check also return a status of warning if the uptime is less then 1 hour.

# $Id: check_cpu,v 1.3 2001/09/28 19:16:59 sljohnson Exp $

use Spong::SafeExec qw(safe_exec);

sub check_cpu { 
   my $color    = "green";
   my $uptime   = safe_exec($UPTIME,30);
   my( $message, $jobs );

   my $s = 'up\s+([^\,]+)\,.*\s(\d+) user.+?' . 
      '[0-9\.]+\,\s*([0-9\.]+)\,\s*([0-9\.]+)\s*$';
   my( $up, $users, $load ) = ( $uptime =~ /$s/ );

   my @msg = safe_exec($PS,30); 
   for ( 0..10 ) { $message .= $msg[$_] };
   $jobs = $#msg; # num of jobs is 1 less number of entries in @msg

   # Check to see if the system has only been up a short time, and if it has
   # only been less then an hour, send a page

   if( $up =~ /min/ )      { $color = "yellow"; }
   if( $load > $CPUWARN )  { $color = "yellow"; }
   if( $load > $CPUCRIT )  { $color = "red"; }

   my $summary = "up $up, load = $load, $users users, $jobs jobs";

   &debug( "cpu - $color, $summary" );
   &status( $SPONGSERVER, $HOST, "cpu", $color, $summary, $message );
}

# I'm include perl code, I need this.
1;
