/[nagios-plugins-perl]/trunk/plugins/check_netapp.pl
ViewVC logotype

Contents of /trunk/plugins/check_netapp.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (show annotations) (download)
Mon Dec 18 14:45:38 2017 UTC (2 years, 11 months ago) by racvision
File MIME type: text/plain
File size: 14622 byte(s)
Remise en ordre XH 201712
1 #!/usr/bin/perl
2 # Original version written by ken.mckinlay@curtisswright.com
3 # Parameter checks and SNMP v3 based on code by Christoph Kron
4 # and S. Ghosh (check_ifstatus)
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20 # (above directly taken from Ken's original script
21 # This version was created by Erik Welch (ewelch@taos.com)
22 # Many of the concepts were created by Ken, I just redid the logic and
23 # made it so I only had to edit one location to add a new variable to check
24 # Bugs and updates can be sent to me.
25 # I have not tested any of the snmp v2c or v3 functions
26 # they may be horribly broken or implemented badly, I redid some of Ken's
27 # logic to fit my simple mind, but haven't utilized it yet...
28 #
29 # $Id: check_netapp2,v 2.3 2006/06/05 19:04:05 root Exp $
30 use warnings;
31 use strict;
32 use lib "/usr/lib/nagios/plugins";
33 use utils qw/$TIMEOUT %ERRORS &print_revision &support/;
34 use Net::SNMP;
35 use Getopt::Long;
36 use constant KB_PER_MB => 1024;
37 use constant KB_PER_GB => KB_PER_MB*KB_PER_MB;
38 use constant OK => 'OK';
39 use constant WARNING => 'WARNING';
40 use constant CRITICAL => 'CRITICAL';
41 use constant UNKNOWN => 'UNKNOWN';
42 Getopt::Long::Configure('bundling');
43
44 my $PROGNAME = $0;
45 my $PROGREVISION = '2.2';
46 my %stats2chk;
47
48 ##########
49 #I don't recommend editing the following vars
50 ##########
51 my $state = UNKNOWN;
52 my $maxmsgsize = 1472; #Net::SNMP default
53 my $port = 161;
54 my $result; #connection to Net::SNMP result
55 ##########
56 #Editable variables
57 ##########
58 my %opts = (
59 'snmp_version' => 1,
60 'community' => 'public',
61 'timeout' => $TIMEOUT,
62 'port' => 161,
63 );
64
65 %stats2chk = (
66 #Primary key:
67 #The variable name from the command line
68 #Secondary keys:
69 #oid is the base oid to be monitored
70 #answer string to pass to sprintf (Status Information)
71 #perfdata string to pass to sprintf (Performance Data)
72 #help string that is used in the help output (-h)
73 #
74 #if you have a specialized routine to process the data you get from
75 #the oid use the "sub" key
76 #sub name of the key in the special_subs hash
77 # this was the only way I could reference a sub without it calculating
78 # the sub...
79 #other secondary keys (I would recommend naming similar to subopts)
80 #can be created as needed. Use DISKUSED as an example
81 'FAILEDDISK' =>
82 { 'oid' => '.1.3.6.1.4.1.789.1.6.4.7',
83 'answer' => 'Disks failed: %d',
84 'perfdata' => 'faileddisk=%d',
85 'help' => '%s - disk failed state',
86 },
87 'FAN' =>
88 { 'oid' => '.1.3.6.1.4.1.789.1.2.4.2',
89 'answer' => 'Fans failed: %d',
90 'perfdata' => 'failedfans=%d',
91 'help' => '%s - fan failed state',
92 },
93 'PS' =>
94 { 'oid' => '.1.3.6.1.4.1.789.1.2.4.4',
95 'answer' => 'Power supplies failed: %d',
96 'perfdata' => 'failedpowersupplies=%d',
97 'help' => '%s - Power Supply failed state',
98 },
99 'UPTIME' =>
100 { 'oid' => '.1.3.6.1.2.1.1.3',
101 'answer' => 'System uptime: %s',
102 'perfdata' => 'uptime=%s',
103 'help' => '%s - up time',
104 },
105 'CPULOAD' =>
106 { 'oid' => '.1.3.6.1.4.1.789.1.2.1.3',
107 'answer' => 'CPU load: %d%%',
108 'perfdata' => 'cpuload=%d%%',
109 'help' => '%s - CPU load',
110 },
111 'TEMP' =>
112 { 'oid' => '.1.3.6.1.4.1.789.1.2.4.1',
113 'answer' => 'Over temperature: %s',
114 'perfdata' => 'overtemperature=%s',
115 'help' => '%s - over temperature check',
116 },
117 'NVRAM' =>
118 { 'oid' => '.1.3.6.1.4.1.789.1.2.5.1',
119 'answer' => 'NVRAM battery status: %d',
120 'perfdata' => 'nvrambatterystatus=%d',
121 'help' => '%s - nvram battery status',
122 },
123 'SNAPSHOT' =>
124 { 'oid' => '.1.3.6.1.4.1.789.1.5.8',
125 'answer' => '%s Snapshots disabled',
126 'perfdata' => '',
127 'help' => '%s - volume snapshot status',
128 'sub' => 'check_oidvals',
129 'subopts' => [".1.3.6.1.4.1.789.1.5.8",".1.2",".1.7"],
130 },
131 'DISKUSED' =>
132 { 'oid' => '.1.3.6.1.4.1.789.1.5.4.1',
133 'answer' => 'Kb free : %d',
134 'perfdata' => '%s',
135 'help' => '%s - disk space avail',
136 'sub' => 'check_oidvals',
137 'subopts' => [".1.3.6.1.4.1.789.1.5.4.1",".2",".5"],
138 'subminval' => 30*KB_PER_GB, #GBytes
139 'subskip' => "snapshot", #skip volumes with this at the end of name
140 },
141 );
142 ##########
143 sub usage {
144 #prints usage and dies
145 print <<EOU;
146 Missing or incorrect arguments!
147
148 (SNMP v1, v2c)
149 $PROGNAME -H ip_address -v variable
150 [ -P snmp_version ] [ -C community ] [ -p port_number ]
151
152 (SNMP v3)
153 $PROGNAME -H ip_address -v variable -L sec_level -U sec_name
154 [ -w warn_range ] [ -c crit_range ] [ -C community ] [ -t timeout ]
155 [ -p port_number ] [ -P 3 ] [ -a auth_proto ]
156 [ -A auth_passwd ] [ -X priv_passwd ] [-o volume ]
157
158 $PROGNAME -h, --help
159
160 EOU
161 support();
162 exit $ERRORS{UNKNOWN};
163 }
164
165 sub print_help {
166 #prints more usage info and dies
167 print <<EOH;
168 check_netapp plugin for Nagios monitors the status of a NetApp system
169
170 Usage:
171 -H, --hostname
172 hostname to query (required)
173 -C, --community
174 SNMP read community (defaults to public)
175 -t, --timeout
176 seconds before the plugin times out (default=$TIMEOUT)
177 -p, --port
178 SNMP port (default 161)
179 -P, --snmp_version
180 1 for SNMP v1 (default),
181 2 for SNMP v2c
182 3 for SNMP v3 (requires -U & -L)
183 -L, --seclevel
184 "noAuthNoPriv"
185 "authNoPriv" (requires -a & -A)
186 "authpriv" (requires -a -x & -A)
187 -U, --secname
188 user name for SNMPv3 context
189 -a, --authproto
190 authentication protocol (MD5 or SHA1)
191 -A, --authpass
192 authentication password
193 -X, --privpass
194 privacy password in hex with 0x prefix generated by snmpkey
195 -V, --version
196 plugin version
197 -w, --warning
198 warning level
199 -c, --critical
200 critical level
201 -o, --volume
202 volume to query (defaults to all)
203 -h, --help
204 usage help
205 -v, --variable
206 variable to query, can be:
207 EOH
208 #print the help line from the %stats2chk hash
209 for (sort keys %stats2chk) { printf "\t$stats2chk{$_}{'help'}\n", $_ }
210 print "\n";
211 print_revision($PROGNAME,"\$Revision: 2.2 $PROGREVISION\$");
212 exit $ERRORS{UNKNOWN};
213 }
214
215 #the specialized subs that may bee needed based on a variable on the
216 #command line. See comments above for mor info
217 my %special_subs = (
218 'check_oidvals' => sub {
219 my($result, $oids, $stats2chk, $var2check) = @_;
220 my $base_oid = shift @$oids;
221 my($oidName, $oidOpts) = map { $base_oid . $_ }@$oids;
222
223 my $mystate = OK;
224 my $myanswer = $stats2chk->{$var2check}{'answer'};
225 my $myperfdata = $stats2chk->{$var2check}{'perfdata'};
226
227 my $volume;
228 if (defined $opts{'volume'}) { $volume = $opts{'volume'} }
229
230 ############
231 my $minVal;
232 if (defined $stats2chk->{$var2check}{'subminval'} ) {
233 $minVal = $stats2chk->{$var2check}{'subminval'};
234 $minVal = ($opts{'critical'}*KB_PER_GB) if defined $opts{'critical'};
235 }
236 my $subskip;
237 if (defined $stats2chk->{$var2check}{'subskip'}) {
238 $subskip = $stats2chk->{$var2check}{'subskip'};
239 }
240 ############
241
242 my @oidkey2name;
243 for (sort keys %$result) {
244 #create a array - key last digit(s) from oid - value volume name
245 if ($_ =~ /^$oidName\.(\d+)/) {
246 next if $volume && $volume ne $result->{$_};
247 next if $subskip && $result->{$_} =~ /$subskip$/;
248 $oidkey2name[$1] = $result->{$_};
249 }
250
251 if ( $_ =~ /^$oidOpts\.(\d+)/ ) {
252 #tempkey is the last digit from the current oid
253 my($tempkey) = $1;
254 next if $volume && !defined $oidkey2name[$tempkey];
255 next if $subskip && !defined $oidkey2name[$tempkey];
256
257 if ($minVal && ($result->{$_} < $minVal )) {
258 $mystate = WARNING;
259 #$result->{$_} is kbytes avail
260 my $f_answer = sprintf($myanswer, $result->{$_});
261 my $f_perfdata = sprintf($myperfdata, $oidkey2name[$tempkey]);
262 report_exit($var2check, $mystate, $f_answer, $f_perfdata);
263 } elsif ( grep /nosnap=on/, $result->{$_} ) {
264 #format_answers($mystate, $myanswer, $myperfdata);
265 $mystate = WARNING;
266 my $f_answer = sprintf($myanswer, $oidkey2name[$tempkey]);
267 my $f_perfdata = sprintf($myperfdata, $oidkey2name[$tempkey]);
268 report_exit($var2check, $mystate, $f_answer, $f_perfdata);
269 }
270 }
271
272 }
273 my $f_answer = " Checks OK";
274 if ($volume) {
275 $volume .= $f_answer;
276 report_exit($var2check, $mystate, $volume, "");
277 } else {
278 report_exit($var2check, $mystate, $f_answer, "");
279 }
280 },
281 #an example of a simple sub that you can use for a new variable
282 #'foo' => sub {
283 # print "foo\n";
284 #},
285 );
286
287 sub report_exit ($$$$) {
288 #accepts 4 variables
289 # var -variable checked (i.e., FAN)
290 # state -exit status
291 # answer -formatted answer string
292 # perf -formatted performance data string
293 #prints a string for nagios
294 #exits with status $state
295 my($var, $state, $answer, $perf) = @_;
296 print "$var $state - $answer|$perf\n";
297 exit $ERRORS{$state};
298 }
299
300 sub process_arguments {
301 #process the command line args
302 #$opts is a referene to a hash
303 my $opts = shift;
304 my($session, $error);
305
306 my $status = GetOptions ( $opts,
307 'version|V',
308 'help|h',
309 'snmp_version|P=i',
310 'community|C=s',
311 'seclevel|L=s',
312 'authproto|a=s',
313 'secname|U=s',
314 'authpass|A=s',
315 'privpass|X=s',
316 'hostname|H=s',
317 'timeout|t=i',
318 'variable|v=s',
319 'warning|w=i',
320 'critical|c=i',
321 'volume|o=s',
322 );
323
324 #print each %$opts, "\n";
325 usage unless $status;
326 print_help if defined $opts->{'help'};
327 usage unless defined $opts->{'variable'};
328 usage unless defined $opts->{'hostname'};
329 #usage if $opts->{'snmp_version'} !~ /[123]/;
330 usage unless $opts->{'snmp_version'} == (1||2||3);
331 $opts->{'variable'} = uc $opts->{'variable'};
332
333 usage unless grep { $opts->{'variable'} eq $_ } keys %stats2chk;
334
335 if ( $opts->{'snmp_version'} == (1||2) ) {
336 ($session,$error) = Net::SNMP->session(
337 -hostname => $opts->{'hostname'},
338 -port => $opts->{'port'},
339 -version => $opts->{'snmp_version'},
340 -timeout => $opts->{'timeout'},
341 -community => $opts->{'community'},
342 );
343 }
344
345 if ( $opts->{'snmp_version'} == 3 ) {
346 usage unless defined $opts->{'seclevel'} && defined $opts->{'secname'};
347 usage unless ( $opts->{'seclevel'} eq \
348 ('noAuthNoPriv'||'authNopriv'||'authPriv') );
349 my($auth, $priv);
350
351 if ( $opts->{'seclevel'} eq 'noAuthNoPriv' ) {
352 ($session,$error) = Net::SNMP->session(
353 -hostname => $opts->{'hostname'},
354 -community => $opts->{'community'},
355 -port => $opts->{'port'},
356 -version => $opts->{'snmp_version'},
357 -username => $opts->{'secname'},
358 -timeout => $opts->{'timeout'},
359 );
360 } elsif ( $opts->{'seclevel'} eq ('authNoPriv' || 'authPriv' ) ) {
361 usage unless ( $opts->{'authproto'} eq ('MD5' || 'SHA1') );
362 usage unless ( defined $opts->{'authpass'} );
363 if ( $opts->{'authpass'} =~ /^0x/ ) {
364 $auth = "-authkey => $opts->{'authpass'}";
365 } else {
366 $auth = "-authpassword => $opts->{'authpass'}";
367 }
368 if ( $opts->{'seclevel'} eq 'authPriv' ) {
369 usage unless ( defined $opts->{'privpass'} );
370 if ( $opts->{'privpass'} -~ /^0x/ ) {
371 $priv = "-privkey => $opts->{'privpass'}";
372 } else {
373 $priv = "-privpassword => $opts->{'privpass'}";
374 }
375 }
376 ($session,$error) = Net::SNMP->session(
377 -hostname => $opts->{'hostname'},
378 -community => $opts->{'community'},
379 -port => $opts->{'port'},
380 -version => $opts->{'snmp_version'},
381 -username => $opts->{'secname'},
382 -timeout => $opts->{'timeout'},
383 -authprotocol => $opts->{'authproto'},
384 $auth,
385 $priv,
386 );
387 }
388 }
389
390 if ( ! defined $session ) {
391 $state = UNKNOWN;
392 print "$state:$error";
393 exit $ERRORS{$state};
394 }
395
396 return $session;
397 }
398
399 ##########
400
401 $SIG{'ALRM'} = sub {
402 print "ERROR: No snmp response from $opts{'hostname'} (alarm timeout)\n";
403 exit $ERRORS{UNKNOWN};
404 };
405
406 my $session = process_arguments \%opts;
407
408 my $var2check = $opts{'variable'};
409 my $mycritical = $opts{'critical'};
410 my $mywarn = $opts{'warning'};
411
412 my $myoid = $stats2chk{$var2check}{'oid'};
413 #my $mystate = $stats2chk{$var2check}{'state'};
414 my $myanswer = $stats2chk{$var2check}{'answer'};
415 my $myperfdata = $stats2chk{$var2check}{'perfdata'};
416
417 alarm $opts{'timeout'};
418
419 if ( ! defined ( $result = $session->get_table(-baseoid => $myoid) ) ) {
420 my $answer = $session->error;
421 $session->close;
422 my $state = CRITICAL;
423 print "$state:$answer for $myoid with snmp version $opts{'snmp_version'}\n";
424 exit $ERRORS{$state};
425 }
426 $session->close;
427 alarm(0);
428
429 if (defined (my $mysub = $stats2chk{$var2check}{'sub'}) ){
430 my $subopts = $stats2chk{$var2check}{'subopts'};
431
432 &{$special_subs{$mysub}}($result, $subopts, \%stats2chk, $var2check);
433 #just in case the special_sub doesn't exit (a bug)
434 exit $ERRORS{$state};
435 } else {
436 #set state to WARNING if this is true
437 $state = ( ( defined $mywarn ) && ($result->{$myoid . ".0"} >= $mywarn) )?
438 WARNING:
439 #set state to CRITICAL if this is true
440 ( ( defined $mycritical ) && ($result->{$myoid . ".0"} >= $mycritical) ) ?
441 CRITICAL :
442 #set state to OK if none are true
443 OK;
444 my $f_answer = sprintf($myanswer, $result->{$myoid . ".0"});
445 my $f_perfdata = sprintf($myperfdata, $result->{$myoid . ".0"});
446 report_exit($var2check, $state, $f_answer, $f_perfdata);
447 }
448
449 #print "$var2check $state - $myanswer|$myperfdata\n";
450 exit $ERRORS{$state};

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8