|
1 #!/usr/bin/perl -w |
|
2 |
|
3 use 5.010; |
|
4 use strict; |
|
5 use warnings; |
|
6 use File::Basename; |
|
7 use Pod::Usage; |
|
8 use Getopt::Long; |
|
9 |
|
10 $ENV{LANG} = "C"; |
|
11 |
|
12 my $ME = basename $0; |
|
13 my $VERSION = "0.1"; |
|
14 my $mpstat = "/usr/bin/mpstat"; |
|
15 |
|
16 sub version($$); |
|
17 sub usage($$); |
|
18 |
|
19 my $opt = { |
|
20 processor => 'ALL', |
|
21 averageall => 0, |
|
22 warning => 10, |
|
23 critical => 80, |
|
24 interval => 1, |
|
25 ok => 0 |
|
26 }; |
|
27 |
|
28 my %ERRORS = ( |
|
29 OK => 0, |
|
30 WARNING => 1, |
|
31 CRITICAL => 2, |
|
32 UNKNOWN => 3 |
|
33 ); |
|
34 |
|
35 MAIN: { |
|
36 Getopt::Long::Configure('bundling'); |
|
37 GetOptions( |
|
38 "p|processor=s" => \$opt->{processor}, |
|
39 "a|averageall" => \$opt->{averageall}, |
|
40 "w|warning=i" => \$opt->{warning}, |
|
41 "c|critical=i" => \$opt->{critical}, |
|
42 "i|interval=i" => \$opt->{interval}, |
|
43 "o|ok" => \$opt->{ok}, |
|
44 "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) }, |
|
45 "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) }, |
|
46 "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; } |
|
47 ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL}); |
|
48 |
|
49 not -x $mpstat and do { |
|
50 print "CPU UNKNOWN - $mpstat ($!)\n"; |
|
51 exit $ERRORS{UNKNOWN}; |
|
52 }; |
|
53 |
|
54 usage($opt->{processor}, $opt->{interval}); |
|
55 } |
|
56 |
|
57 sub version($$) { |
|
58 my $progname = shift; |
|
59 my $version = shift; |
|
60 |
|
61 print <<_VERSION |
|
62 $progname version $version |
|
63 |
|
64 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
65 $progname comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
66 and you are welcome to redistribute it under certain conditions. |
|
67 See the GNU General Public Licence for details. |
|
68 _VERSION |
|
69 } |
|
70 |
|
71 sub usage($$) { |
|
72 my $processor = shift; |
|
73 my $interval = shift; |
|
74 |
|
75 my @status_information = (); |
|
76 my @performance_data = (); |
|
77 my @values = (); |
|
78 |
|
79 foreach (`$mpstat -P $processor $interval 1`) { |
|
80 /^\d{2}:\d{2}:\d{2}\s+(\S+)/i or next; |
|
81 |
|
82 state @fields; |
|
83 my %stat; |
|
84 |
|
85 $1 eq "CPU" and @fields = map { /^%?(.*)/ } split and next; |
|
86 if ($opt->{averageall}) { |
|
87 $1 =~ /all/ and do { |
|
88 @stat{@fields} = split; |
|
89 my $output = sprintf( |
|
90 "$stat{CPU} cpus: %d%\% for the last $opt->{interval}s | $stat{CPU}=%d%\%;%d;%d;0;0", |
|
91 100 - $stat{idle}, 100 - $stat{idle}, |
|
92 $opt->{warning}, $opt->{critical} |
|
93 ); |
|
94 if ($opt->{ok}) { |
|
95 print "CPU OK - average usage over " . $output . "\n"; |
|
96 exit $ERRORS{OK}; |
|
97 } |
|
98 |
|
99 my $value = sprintf("%d", 100 - $stat{idle}); |
|
100 if ($value >= $opt->{critical}) { |
|
101 print "CPU CRITICAL - average usage over " . $output . "\n"; |
|
102 exit $ERRORS{CRITICAL}; |
|
103 } |
|
104 elsif ($value >= $opt->{warning}) { |
|
105 print "CPU WARNING - average usage over " . $output . "\n"; |
|
106 exit $ERRORS{WARNING}; |
|
107 } |
|
108 print "CPU OK - average usage over " . $output . "\n"; |
|
109 exit $ERRORS{OK}; |
|
110 }; |
|
111 } |
|
112 |
|
113 @stat{@fields} = split; |
|
114 if ($1 eq "all") { |
|
115 push @status_information, |
|
116 sprintf("$stat{CPU}: %d%\%", 100 - $stat{idle}); |
|
117 push @performance_data, |
|
118 sprintf( |
|
119 "$stat{CPU}=%d%\%;%d;%d;0;0", |
|
120 100 - $stat{idle}, |
|
121 $opt->{warning}, $opt->{critical} |
|
122 ); |
|
123 } |
|
124 else { |
|
125 push @status_information, |
|
126 sprintf("cpu$stat{CPU}: %d%\%", 100 - $stat{idle}); |
|
127 push @performance_data, |
|
128 sprintf( |
|
129 "cpu$stat{CPU}=%d%\%;%d;%d;0;0", |
|
130 100 - $stat{idle}, |
|
131 $opt->{warning}, $opt->{critical} |
|
132 ); |
|
133 } |
|
134 push @values, sprintf("%d", 100 - $stat{idle}); |
|
135 } |
|
136 |
|
137 if ($opt->{ok}) { |
|
138 print "CPU OK - average usage @status_information" |
|
139 . " for the last $opt->{interval}s | " |
|
140 . "@performance_data" . "\n"; |
|
141 exit $ERRORS{OK}; |
|
142 } |
|
143 |
|
144 my $rc = "OK"; |
|
145 foreach (@values) { |
|
146 if ($_ >= $opt->{critical}) { |
|
147 $rc = "CRITICAL"; |
|
148 last; |
|
149 } |
|
150 elsif ($_ >= $opt->{warning}) { |
|
151 $rc = "WARNING"; |
|
152 } |
|
153 } |
|
154 |
|
155 print "CPU $rc - average cpu usage @status_information" |
|
156 . " for the last $opt->{interval}s | " |
|
157 . "@performance_data" . "\n"; |
|
158 exit $ERRORS{$rc}; |
|
159 } |
|
160 |
|
161 __END__ |
|
162 |
|
163 =head1 NAME |
|
164 |
|
165 check_cpu - nagios plugin to check cpu usage |
|
166 |
|
167 =head1 SYNOPSIS |
|
168 |
|
169 B<check_cpu> [OPTION...] |
|
170 |
|
171 =head1 OPTIONS |
|
172 |
|
173 =over |
|
174 |
|
175 =item B<-p>, B<--processor> { cpu [,...] | ALL } |
|
176 |
|
177 Indicate the processor number for which usage are to be reported. I<cpu> is the processor number. |
|
178 Note that processor 0 is the first processor. The B<ALL> keyword indicates that usage are to be reported for all processors. |
|
179 The default value is B<ALL>. |
|
180 |
|
181 =item B<-a>, B<--averageall> |
|
182 |
|
183 Only output the average cpu usage over all processors. |
|
184 |
|
185 =item B<-w>, B<--warning> INTEGER |
|
186 |
|
187 Exit with I<WARNING> status if cpu usage is greater than INTEGER in percent and less than B<--critical> INTEGER in percent. |
|
188 The default value is B<10> percent. |
|
189 |
|
190 =item B<-c>, B<--critical> INTEGER |
|
191 |
|
192 Exit with I<CRITICAL> status if usage is greater than INTEGER in percent. The default value is B<80> percent. |
|
193 |
|
194 =item B<-i>, B<--interval> INTEGER |
|
195 |
|
196 The I<interval> parameter specifies the average time period in B<seconds> for cpu usage. The default value is B<1> second. |
|
197 |
|
198 =item B<-o>, B<--ok> |
|
199 |
|
200 Exit always with I<OK> status. |
|
201 |
|
202 =item B<-h>, B<--help> |
|
203 |
|
204 Print detailed help screen. |
|
205 |
|
206 =item B<-m>, B<--man> |
|
207 |
|
208 Print manual page. |
|
209 |
|
210 =item B<-V>, B<--version> |
|
211 |
|
212 Print version information. |
|
213 |
|
214 =back |
|
215 |
|
216 =head1 DESCRIPTION |
|
217 |
|
218 Nagios plugin for checking cpu usage. This plugin requires the B<sysstat> package on your system. |
|
219 |
|
220 =head1 EXAMPLES |
|
221 |
|
222 =over |
|
223 |
|
224 =item B<check_cpu> |
|
225 |
|
226 Output average cpu usage for each processor for the last second. |
|
227 |
|
228 =item B<check_cpu -i 10> |
|
229 |
|
230 Output average cpu usage for each processor for the last 10 seconds. |
|
231 |
|
232 =item B<check_cpu -a -i 10> |
|
233 |
|
234 Output only the average cpu usage over all processors for the last 10 seconds. |
|
235 |
|
236 =item B<check_cpu -o -a -i 10> |
|
237 |
|
238 Output only the average cpu usage over all processors for the last 10 seconds and the exit value is always I<OK>. |
|
239 |
|
240 =back |
|
241 |
|
242 =head1 EXIT VALUES |
|
243 |
|
244 =over |
|
245 |
|
246 =item B<0> |
|
247 |
|
248 status I<OK> |
|
249 |
|
250 =item B<1> |
|
251 |
|
252 status I<WARNING> |
|
253 |
|
254 =item B<2> |
|
255 |
|
256 status I<CRITICAL> |
|
257 |
|
258 =item B<3> |
|
259 |
|
260 status I<UNKNOWN> |
|
261 |
|
262 =back |
|
263 |
|
264 =head1 VERSION |
|
265 |
|
266 This man page is current for version 0.1 of check_cpu. |
|
267 |
|
268 =head1 AUTHOR |
|
269 |
|
270 Written by Christian Arnold L<arnold@schlittermann.de> |
|
271 |
|
272 =head1 COPYRIGHT |
|
273 |
|
274 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
275 This is free software, and you are welcome to redistribute it under certain conditions. |
|
276 See the GNU General Public Licence for details. |
|
277 |
|
278 =cut |