[Nagiosplug-devel] new plugin check_cpu.pl
Ken Menzel
kenlist at icarz.com
Fri Oct 24 12:45:05 CEST 2003
OK - my first plugin submission. Check CPU usage on Linux, FreeBSD
and AIX.
Output looks like this
janeway# ./check_cpu.pl -c5 -w30
CPU usage OK - 5% used 95% idle
janeway# ./check_cpu.pl -c80 -w97
CPU usage WARNING - 9% used 91% idle
It will can check IDLE% USER% or SYSTEM% CPU usage. So if I want be
warned as I have LESS idle cpu (IE usages approaches %100) I use the
default mode (-i) and with -c5 -w30 I will be warned if I have less
then 30% CPU available and go critical when I have less then 5%
available.
Is this confusing?
Also if anyone wants to email me more systems to add I am OK with
that, also I can easily add SCO Openserver 5.
Hope you like my plugin,
Ken
check_cpu.pl
---------------------------------------------------------
#!/usr/bin/perl -w
# check_cpu.pl Copyright (C) 2003 Ken Menzel <kenm at icarz.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Netsaint); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
# Tell Perl what we need to use
use strict;
use Getopt::Std;
use vars qw($opt_c $opt_s $opt_u $opt_w $opt_i $opt_v
$cpu_system $cpu_user $cpu_idle
$crit_level $warn_level
%exit_codes @memlist
$percent $fmt_pct
$command_line $uname);
# Predefined exit codes for NetSaint
%exit_codes = ('UNKNOWN' ,-1,
'OK' , 0,
'WARNING' , 1,
'CRITICAL', 2,);
# Get the options
if ($#ARGV le 0)
{
&usage;
}
else
{
getopts('c:isuw:v');
}
# Shortcircuit the switches
if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
{
print "*** You must define WARN and CRITICAL levels! ****\n";
&usage;
}
elsif (!$opt_s and !$opt_u)
{
$opt_i=1;
# print "*** You must select to monitor either USED or FREE cpu!";
# &usage;
}
# Check if levels are sane
if ($opt_w <= $opt_c and $opt_i)
{
print "*** WARN level must not be less than CRITICAL when checking
IDLE CPU! ****\n";
&usage;
}
elsif ($opt_w >= $opt_c and ($opt_u or $opt_s) )
{
print "*** WARN level must not be greater than CRITICAL when
checking USER or SYSTEM CPU! ***\n";
&usage;
}
$warn_level = $opt_w;
$crit_level = $opt_c;
# This the unix command string that brings Perl the data
$uname= `uname`;
chomp($uname);
if ($opt_v) { print "Sytem Type $uname,\n"; }
if (($uname eq 'Linux') or ($uname eq 'AIX') )
{$command_line = `vmstat 1 2 | tail -1 | awk '{print \$14,\$15,
\$16}'`; }
else
{$command_line = `vmstat -c 2 -w 1 | tail -1 | awk '{print
\$17,\$18, \$19}'`; }
if ($opt_v) { print "$command_line\n"; }
chomp $command_line;
@memlist = split(/ /, $command_line);
# Define the calculating scalars
$cpu_user = $memlist[0];
$cpu_system = $memlist[1];
$cpu_idle = $memlist[2];
if ($opt_i)
{
$fmt_pct= 100 - $cpu_idle;
if ($cpu_idle <= $crit_level)
{
print "CPU usage CRITICAL - $fmt_pct% used $cpu_idle% idle \n";
exit $exit_codes{'CRITICAL'};
}
elsif ($cpu_idle <= $warn_level)
{
print "CPU usage WARNING - $fmt_pct% used $cpu_idle% idle\n";
exit $exit_codes{'WARNING'};
}
else
{
print "CPU usage OK - $fmt_pct% used $cpu_idle% idle\n";
exit $exit_codes{'OK'};
}
}
elsif ($opt_u)
{
if ($cpu_user >= $crit_level)
{
print "CPU usage CRITICAL - $cpu_user% user\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent >= $warn_level)
{
print "CPU usage WARNING - $cpu_user% user\n";
exit $exit_codes{'WARNING'};
}
else
{
print "CPU usage OK - $cpu_user% user\n";
exit $exit_codes{'OK'};
}
}
elsif ($opt_s)
{
if ($cpu_system >= $crit_level)
{
print "CPU usage CRITICAL - $cpu_system% system\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent >= $warn_level)
{
print "CPU usage WARNING - $cpu_system% system\n";
exit $exit_codes{'WARNING'};
}
else
{
print "CPU usage OK - $cpu_system% system\n";
exit $exit_codes{'OK'};
}
}
# Show usage
sub usage()
{
my $mymsg=<<MYMSG
check_cpu.pl v1.0 - Nagios/NetSaint Plugin\n
usage:
check_cpu.pl -<i|s|u> -w <warnlevel> -c <critlevel>\n
options:\n
-i Check IDLE cpu (default)
-s Check SYSTEM cpu
-u Check USER cpu
-w PERCENT Percent free/used when to warn
-c PERCENT Percent free/used when critical
-v Verbose output for debug
\nCopyright (C) 2003 Ken Menzel (kenm\@icarz.com)
check_cpu.pl comes with absolutely NO WARRANTY either implied or
explicit
This program is licensed under the terms of the
GNU General Public License (check source code for details)
MYMSG
;
print "$mymsg";
exit $exit_codes{'UNKNOWN'};
}
-----------------------------------------------------
Ken Menzel ICQ# 9325188
www.icarz.com kenm at icarz.com
More information about the Devel
mailing list