# HG changeset patch # User Christia Arnold # Date 1318604259 -7200 # Node ID a5f5e3961230233d82d3f1f08d71c9e561507e7c import to mercurial diff -r 000000000000 -r a5f5e3961230 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,8 @@ +syntax: glob +*.swp +debian/files +check_cpu + +syntax: regexp +(build|configure)-stamp$ +debian/nagios-plugin-cpu[./] diff -r 000000000000 -r a5f5e3961230 .perltidyrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.perltidyrc Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,2 @@ +--paren-tightness=2 +--square-bracket-tightness=2 diff -r 000000000000 -r a5f5e3961230 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,22 @@ +SCRIPTS = check_cpu +CLEANFILES = ${SCRIPTS} +DESTDIR = +prefix = /usr + +plugindir = ${prefix}/lib/nagios/plugins/ius + +.PHONY: all clean install + +all: ${SCRIPTS} + +clean: + -rm -f ${CLEANFILES} + +install: all + install -d -m 0755 ${DESTDIR}/${plugindir} + install -m 0755 $(SCRIPTS) ${DESTDIR}/${plugindir}/ + +%: %.pl + @perl -c $< + @cp -f $< $@ + @chmod +x $@ diff -r 000000000000 -r a5f5e3961230 check_cpu.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/check_cpu.pl Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,278 @@ +#!/usr/bin/perl -w + +use 5.010; +use strict; +use warnings; +use File::Basename; +use Pod::Usage; +use Getopt::Long; + +$ENV{LANG} = "C"; + +my $ME = basename $0; +my $VERSION = "0.1"; +my $mpstat = "/usr/bin/mpstat"; + +sub version($$); +sub usage($$); + +my $opt = { + processor => 'ALL', + averageall => 0, + warning => 10, + critical => 80, + interval => 1, + ok => 0 +}; + +my %ERRORS = ( + OK => 0, + WARNING => 1, + CRITICAL => 2, + UNKNOWN => 3 +); + +MAIN: { + Getopt::Long::Configure('bundling'); + GetOptions( + "p|processor=s" => \$opt->{processor}, + "a|averageall" => \$opt->{averageall}, + "w|warning=i" => \$opt->{warning}, + "c|critical=i" => \$opt->{critical}, + "i|interval=i" => \$opt->{interval}, + "o|ok" => \$opt->{ok}, + "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) }, + "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) }, + "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; } + ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL}); + + not -x $mpstat and do { + print "CPU UNKNOWN - $mpstat ($!)\n"; + exit $ERRORS{UNKNOWN}; + }; + + usage($opt->{processor}, $opt->{interval}); +} + +sub version($$) { + my $progname = shift; + my $version = shift; + + print <<_VERSION +$progname version $version + +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. +$progname comes with ABSOLUTELY NO WARRANTY. This is free software, +and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. +_VERSION +} + +sub usage($$) { + my $processor = shift; + my $interval = shift; + + my @status_information = (); + my @performance_data = (); + my @values = (); + + foreach (`$mpstat -P $processor $interval 1`) { + /^\d{2}:\d{2}:\d{2}\s+(\S+)/i or next; + + state @fields; + my %stat; + + $1 eq "CPU" and @fields = map { /^%?(.*)/ } split and next; + if ($opt->{averageall}) { + $1 =~ /all/ and do { + @stat{@fields} = split; + my $output = sprintf( +"$stat{CPU} cpus: %d%\% for the last $opt->{interval}s | $stat{CPU}=%d%\%;%d;%d;0;0", + 100 - $stat{idle}, 100 - $stat{idle}, + $opt->{warning}, $opt->{critical} + ); + if ($opt->{ok}) { + print "CPU OK - average usage over " . $output . "\n"; + exit $ERRORS{OK}; + } + + my $value = sprintf("%d", 100 - $stat{idle}); + if ($value >= $opt->{critical}) { + print "CPU CRITICAL - average usage over " . $output . "\n"; + exit $ERRORS{CRITICAL}; + } + elsif ($value >= $opt->{warning}) { + print "CPU WARNING - average usage over " . $output . "\n"; + exit $ERRORS{WARNING}; + } + print "CPU OK - average usage over " . $output . "\n"; + exit $ERRORS{OK}; + }; + } + + @stat{@fields} = split; + if ($1 eq "all") { + push @status_information, + sprintf("$stat{CPU}: %d%\%", 100 - $stat{idle}); + push @performance_data, + sprintf( + "$stat{CPU}=%d%\%;%d;%d;0;0", + 100 - $stat{idle}, + $opt->{warning}, $opt->{critical} + ); + } + else { + push @status_information, + sprintf("cpu$stat{CPU}: %d%\%", 100 - $stat{idle}); + push @performance_data, + sprintf( + "cpu$stat{CPU}=%d%\%;%d;%d;0;0", + 100 - $stat{idle}, + $opt->{warning}, $opt->{critical} + ); + } + push @values, sprintf("%d", 100 - $stat{idle}); + } + + if ($opt->{ok}) { + print "CPU OK - average usage @status_information" + . " for the last $opt->{interval}s | " + . "@performance_data" . "\n"; + exit $ERRORS{OK}; + } + + my $rc = "OK"; + foreach (@values) { + if ($_ >= $opt->{critical}) { + $rc = "CRITICAL"; + last; + } + elsif ($_ >= $opt->{warning}) { + $rc = "WARNING"; + } + } + + print "CPU $rc - average cpu usage @status_information" + . " for the last $opt->{interval}s | " + . "@performance_data" . "\n"; + exit $ERRORS{$rc}; +} + +__END__ + +=head1 NAME + +check_cpu - nagios plugin to check cpu usage + +=head1 SYNOPSIS + +B [OPTION...] + +=head1 OPTIONS + +=over + +=item B<-p>, B<--processor> { cpu [,...] | ALL } + +Indicate the processor number for which usage are to be reported. I is the processor number. +Note that processor 0 is the first processor. The B keyword indicates that usage are to be reported for all processors. +The default value is B. + +=item B<-a>, B<--averageall> + +Only output the average cpu usage over all processors. + +=item B<-w>, B<--warning> INTEGER + +Exit with I status if cpu usage is greater than INTEGER in percent and less than B<--critical> INTEGER in percent. +The default value is B<10> percent. + +=item B<-c>, B<--critical> INTEGER + +Exit with I status if usage is greater than INTEGER in percent. The default value is B<80> percent. + +=item B<-i>, B<--interval> INTEGER + +The I parameter specifies the average time period in B for cpu usage. The default value is B<1> second. + +=item B<-o>, B<--ok> + +Exit always with I status. + +=item B<-h>, B<--help> + +Print detailed help screen. + +=item B<-m>, B<--man> + +Print manual page. + +=item B<-V>, B<--version> + +Print version information. + +=back + +=head1 DESCRIPTION + +Nagios plugin for checking cpu usage. This plugin requires the B package on your system. + +=head1 EXAMPLES + +=over + +=item B + +Output average cpu usage for each processor for the last second. + +=item B + +Output average cpu usage for each processor for the last 10 seconds. + +=item B + +Output only the average cpu usage over all processors for the last 10 seconds. + +=item B + +Output only the average cpu usage over all processors for the last 10 seconds and the exit value is always I. + +=back + +=head1 EXIT VALUES + +=over + +=item B<0> + +status I + +=item B<1> + +status I + +=item B<2> + +status I + +=item B<3> + +status I + +=back + +=head1 VERSION + +This man page is current for version 0.1 of check_cpu. + +=head1 AUTHOR + +Written by Christian Arnold L + +=head1 COPYRIGHT + +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. +This is free software, and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. + +=cut diff -r 000000000000 -r a5f5e3961230 debian/README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/README Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,6 @@ +The Debian Package nagios-plugin-cpu +---------------------------- + +Comments regarding the Package + + -- Christian Arnold Fri, 14 Oct 2011 16:47:13 +0200 diff -r 000000000000 -r a5f5e3961230 debian/README.Debian --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/README.Debian Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,6 @@ +nagios-plugin-cpu for Debian +---------------------------- + + + + -- Christian Arnold Fri, 14 Oct 2011 16:47:13 +0200 diff -r 000000000000 -r a5f5e3961230 debian/README.source --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/README.source Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,9 @@ +nagios-plugin-cpu for Debian +---------------------------- + + + + + + diff -r 000000000000 -r a5f5e3961230 debian/changelog --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/changelog Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,5 @@ +nagios-plugin-cpu (0.1) stable; urgency=low + + * Initial Release. + + -- Christian Arnold Fri, 14 Oct 2011 16:53:38 +0200 diff -r 000000000000 -r a5f5e3961230 debian/compat --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/compat Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,1 @@ +7 diff -r 000000000000 -r a5f5e3961230 debian/control --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/control Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,13 @@ +Source: nagios-plugin-cpu +Section: net +Priority: extra +Maintainer: Christian Arnold +Build-Depends: debhelper (>= 7.0.50~) +Standards-Version: 3.8.4 +Homepage: https://keller.schlittermann.de/hg/ius/nagios/nagios-plugin-cpu/ + +Package: nagios-plugin-cpu +Architecture: all +Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}, sysstat +Description: nagios plugin to check cpu status + This nagios plugin checks the cpu status of the system. diff -r 000000000000 -r a5f5e3961230 debian/copyright --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/copyright Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,39 @@ +This work was packaged for Debian by: + + Christian Arnold on Fri, 14 Oct 2011 16:47:13 +0200 + +It was downloaded from: + + https://keller.schlittermann.de/hg/ius/nagios/nagios-plugin-cpu/ + +Upstream Author(s): + + Christian Arnold + +Copyright: + + Copyright (C) 2011 Schlittermann internet & unix support + +License: + + 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 3 of the License, or + (at your option) any later version. + + This package 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. If not, see . + +On Debian systems, the complete text of the GNU General +Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". + +The Debian packaging is: + + Copyright (C) 2011 Christian Arnold + +and is licensed under the GPL version 3, see above. diff -r 000000000000 -r a5f5e3961230 debian/docs diff -r 000000000000 -r a5f5e3961230 debian/rules --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/rules Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,13 @@ +#!/usr/bin/make -f +# -*- makefile -*- +# Sample debian/rules that uses debhelper. +# This file was originally written by Joey Hess and Craig Small. +# As a special exception, when this file is copied by dh-make into a +# dh-make output file, you may use that output file without restriction. +# This special exception was added by Craig Small in version 0.37 of dh-make. + +# Uncomment this to turn on verbose mode. +#export DH_VERBOSE=1 + +%: + dh $@ diff -r 000000000000 -r a5f5e3961230 debian/source/format --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debian/source/format Fri Oct 14 16:57:39 2011 +0200 @@ -0,0 +1,1 @@ +3.0 (native)