2
############################## check_snmp_mem ##############
5
# Author : Patrick Proy (nagios at proy.org)
6
# Help : http://www.manubulon.com/nagios/
7
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
8
# Contrib : Jan Jungmann
10
#################################################################
12
# Help : ./check_snmp_mem.pl -h
21
use lib "/usr/local/nagios/libexec";
22
use utils qw(%ERRORS $TIMEOUT);
24
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
30
my $nets_ram_free = "1.3.6.1.4.1.2021.4.6.0"; # Real memory free
31
my $nets_ram_total = "1.3.6.1.4.1.2021.4.5.0"; # Real memory total
32
my $nets_ram_cache = "1.3.6.1.4.1.2021.4.15.0"; # Real memory cached
33
my $nets_swap_free = "1.3.6.1.4.1.2021.4.4.0"; # swap memory free
34
my $nets_swap_total = "1.3.6.1.4.1.2021.4.3.0"; # Swap memory total
35
my @nets_oids = ($nets_ram_free,$nets_ram_total,$nets_swap_free,$nets_swap_total,$nets_ram_cache);
39
my $cisco_mem_pool = "1.3.6.1.4.1.9.9.48.1.1.1"; # Cisco memory pool
40
my $cisco_index = "1.3.6.1.4.1.9.9.48.1.1.1.2"; # memory pool name and index
41
my $cisco_valid = "1.3.6.1.4.1.9.9.48.1.1.1.4"; # Valid memory if 1
42
my $cisco_used = "1.3.6.1.4.1.9.9.48.1.1.1.5"; # Used memory
43
my $cisco_free = "1.3.6.1.4.1.9.9.48.1.1.1.6"; # Free memory
44
# .1 : type, .2 : name, .3 : alternate, .4 : valid, .5 : used, .6 : free, .7 : max free
48
my $hp_mem_pool = "1.3.6.1.4.1.11.2.14.11.5.1.1.2.2.1.1"; # HP memory pool
49
my $hp_mem_index = "1.3.6.1.4.1.11.2.14.11.5.1.1.2.2.1.1.1"; # memory slot index
50
my $hp_mem_total = "1.3.6.1.4.1.11.2.14.11.5.1.1.2.2.1.1.5"; # Total Bytes
51
my $hp_mem_free = "1.3.6.1.4.1.11.2.14.11.5.1.1.2.2.1.1.6"; # Free Bytes
52
my $hp_mem_free_seg = "1.3.6.1.4.1.11.2.14.11.5.1.1.2.2.1.1.3"; # Free segments
58
# check_snmp_storage.pl -C <community> -H <hostIP> -m "^Virtual Memory$" -w <warn %> -c <crit %>
65
my $o_host = undef; # hostname
66
my $o_community = undef; # community
67
my $o_port = 161; # port
68
my $o_help= undef; # wan't some help ?
69
my $o_verb= undef; # verbose mode
70
my $o_version= undef; # print version
71
my $o_netsnmp= 1; # Check with netsnmp (default)
72
my $o_cisco= undef; # Check cisco router mem
73
my $o_hp= undef; # Check hp procurve mem
74
my $o_warn= undef; # warning level option
75
my $o_warnR= undef; # warning level for Real memory
76
my $o_warnS= undef; # warning levels for swap
77
my $o_crit= undef; # Critical level option
78
my $o_critR= undef; # critical level for Real memory
79
my $o_critS= undef; # critical level for swap
80
my $o_perf= undef; # Performance data option
81
my $o_cache= undef; # Include cached memory as used memory
82
my $o_timeout= undef; # Timeout (Default 5)
83
my $o_version2= undef; # use snmp v2c
85
my $o_login= undef; # Login for snmpv3
86
my $o_passwd= undef; # Pass for snmpv3
87
my $v3protocols=undef; # V3 protocol list.
88
my $o_authproto='md5'; # Auth protocol
89
my $o_privproto='des'; # Priv protocol
90
my $o_privpass= undef; # priv password
94
sub p_version { print "check_snmp_mem version : $Version\n"; }
97
print "Usage: $0 [-v] -H <host> -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>]) [-p <port>] -w <warn level> -c <crit level> [-I|-N|-E] [-f] [-m] [-t <timeout>] [-V]\n";
100
sub isnnum { # Return true if arg is not a number
102
if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
107
sprintf "%.$_[1]f", $_[0];
111
print "\nSNMP Memory Monitor for Nagios version ",$Version,"\n";
112
print "(c)2004-2006 to my cat Ratoune - Author: Patrick Proy\n\n";
116
print extra debugging information (including interface list on the system)
118
print this help message
120
name or IP address of host to check
121
-C, --community=COMMUNITY NAME
122
community name for the host's SNMP agent (implies SNMP v1 or v2c with option)
125
-l, --login=LOGIN ; -x, --passwd=PASSWD
126
Login and auth password for snmpv3 authentication
127
If no priv password exists, implies AuthNoPriv
128
-X, --privpass=PASSWD
129
Priv password for snmpv3 (AuthPriv protocol)
130
-L, --protocols=<authproto>,<privproto>
131
<authproto> : Authentication protocol (md5|sha : default md5)
132
<privproto> : Priv protocole (des|aes : default des)
134
SNMP port (Default 161)
135
-w, --warn=INTEGER | INT,INT
136
warning level for memory in percent (0 for no checks)
137
Default (-N switch) : comma separated level for Real Memory and Swap
138
-I switch : warning level
139
-c, --crit=INTEGER | INT,INT
140
critical level for memory in percent (0 for no checks)
141
Default (-N switch) : comma separated level for Real Memory and Swap
142
-I switch : critical level
143
-N, --netsnmp (default)
144
check linux memory & swap provided by Net SNMP
146
include cached memory in used memory (only with Net-SNMP)
148
check cisco memory (sum of all memory pools)
150
check HP proccurve memory
152
Performance data output
153
-t, --timeout=INTEGER
154
timeout for SNMP in seconds (Default: 5)
156
prints version number
161
sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }
163
# Get the alarm signal (just in case snmp timout screws up)
165
print ("ERROR: Alarm signal (Nagios time-out)\n");
166
exit $ERRORS{"UNKNOWN"};
170
Getopt::Long::Configure ("bundling");
172
'v' => \$o_verb, 'verbose' => \$o_verb,
173
'h' => \$o_help, 'help' => \$o_help,
174
'H:s' => \$o_host, 'hostname:s' => \$o_host,
175
'p:i' => \$o_port, 'port:i' => \$o_port,
176
'C:s' => \$o_community, 'community:s' => \$o_community,
177
'l:s' => \$o_login, 'login:s' => \$o_login,
178
'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd,
179
'X:s' => \$o_privpass, 'privpass:s' => \$o_privpass,
180
'L:s' => \$v3protocols, 'protocols:s' => \$v3protocols,
181
't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,
182
'V' => \$o_version, 'version' => \$o_version,
183
'I' => \$o_cisco, 'cisco' => \$o_cisco,
184
'N' => \$o_netsnmp, 'netsnmp' => \$o_netsnmp,
185
'E' => \$o_hp, 'hp' => \$o_hp,
186
'2' => \$o_version2, 'v2c' => \$o_version2,
187
'c:s' => \$o_crit, 'critical:s' => \$o_crit,
188
'w:s' => \$o_warn, 'warn:s' => \$o_warn,
189
'm' => \$o_cache, 'memcache' => \$o_cache,
190
'f' => \$o_perf, 'perfdata' => \$o_perf
192
if (defined ($o_help) ) { help(); exit $ERRORS{"UNKNOWN"}};
193
if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"}};
194
if ( ! defined($o_host) ) # check host and filter
195
{ print "No host defined!\n";print_usage(); exit $ERRORS{"UNKNOWN"}}
196
# check snmp information
197
if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) )
198
{ print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
199
if ((defined($o_login) || defined($o_passwd)) && (defined($o_community) || defined($o_version2)) )
200
{ print "Can't mix snmp v1,2c,3 protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
201
if (defined ($v3protocols)) {
202
if (!defined($o_login)) { print "Put snmp V3 login info with protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
203
my @v3proto=split(/,/,$v3protocols);
204
if ((defined ($v3proto[0])) && ($v3proto[0] ne "")) {$o_authproto=$v3proto[0]; } # Auth protocol
205
if (defined ($v3proto[1])) {$o_privproto=$v3proto[1]; } # Priv protocol
206
if ((defined ($v3proto[1])) && (!defined($o_privpass))) {
207
print "Put snmp V3 priv login info with priv protocols!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
209
if (defined($o_timeout) && (isnnum($o_timeout) || ($o_timeout < 2) || ($o_timeout > 60)))
210
{ print "Timeout must be >1 and <60 !\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
211
if (!defined($o_timeout)) {$o_timeout=5;}
212
#Check Warning and crit are present
213
if ( ! defined($o_warn) || ! defined($o_crit))
214
{ print "Put warning and critical values!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}}
218
# if -N or -E switch , undef $o_netsnmp
219
if (defined($o_cisco) || defined($o_hp) ) {
221
if ( isnnum($o_warn) || isnnum($o_crit))
222
{ print "Numeric value for warning or critical !\n";print_usage(); exit $ERRORS{"UNKNOWN"} }
223
if ( ($o_crit != 0) && ($o_warn > $o_crit) )
224
{ print "warning <= critical ! \n";print_usage(); exit $ERRORS{"UNKNOWN"}}
226
if (defined($o_netsnmp)) {
227
my @o_warnL=split(/,/ , $o_warn);
228
my @o_critL=split(/,/ , $o_crit);
229
if (($#o_warnL != 1) || ($#o_critL != 1))
230
{ print "2 warnings and critical !\n";print_usage(); exit $ERRORS{"UNKNOWN"}}
231
for (my $i=0;$i<2;$i++) {
232
if ( isnnum($o_warnL[$i]) || isnnum($o_critL[$i]))
233
{ print "Numeric value for warning or critical !\n";print_usage(); exit $ERRORS{"UNKNOWN"} }
234
if (($o_critL[$i]!= 0) && ($o_warnL[$i] > $o_critL[$i]))
235
{ print "warning <= critical ! \n";print_usage(); exit $ERRORS{"UNKNOWN"}}
236
if ( $o_critL[$i] > 100)
237
{ print "critical percent must be < 100 !\n";print_usage(); exit $ERRORS{"UNKNOWN"}}
239
$o_warnR=$o_warnL[0];$o_warnS=$o_warnL[1];
240
$o_critR=$o_critL[0];$o_critS=$o_critL[1];
245
########## MAIN #######
249
# Check gobal timeout if snmp screws up
250
if (defined($TIMEOUT)) {
251
verb("Alarm at $TIMEOUT");
254
verb("no timeout defined : $o_timeout + 10");
255
alarm ($o_timeout+10);
259
my ($session,$error);
260
if ( defined($o_login) && defined($o_passwd)) {
262
if (!defined ($o_privpass)) {
263
verb("SNMPv3 AuthNoPriv login : $o_login, $o_authproto");
264
($session, $error) = Net::SNMP->session(
265
-hostname => $o_host,
267
-username => $o_login,
268
-authpassword => $o_passwd,
269
-authprotocol => $o_authproto,
270
-timeout => $o_timeout
273
verb("SNMPv3 AuthPriv login : $o_login, $o_authproto, $o_privproto");
274
($session, $error) = Net::SNMP->session(
275
-hostname => $o_host,
277
-username => $o_login,
278
-authpassword => $o_passwd,
279
-authprotocol => $o_authproto,
280
-privpassword => $o_privpass,
281
-privprotocol => $o_privproto,
282
-timeout => $o_timeout
286
if (defined ($o_version2)) {
288
verb("SNMP v2c login");
289
($session, $error) = Net::SNMP->session(
290
-hostname => $o_host,
292
-community => $o_community,
294
-timeout => $o_timeout
298
verb("SNMP v1 login");
299
($session, $error) = Net::SNMP->session(
300
-hostname => $o_host,
301
-community => $o_community,
303
-timeout => $o_timeout
307
if (!defined($session)) {
308
printf("ERROR opening session: %s.\n", $error);
309
exit $ERRORS{"UNKNOWN"};
315
########### Cisco memory check ############
316
if (defined ($o_cisco)) {
318
# Get Cisco memory table
319
$resultat = (Net::SNMP->VERSION < 4) ?
320
$session->get_table($cisco_mem_pool)
321
:$session->get_table(Baseoid => $cisco_mem_pool);
323
if (!defined($resultat)) {
324
printf("ERROR: Description table : %s.\n", $session->error);
326
exit $ERRORS{"UNKNOWN"};
328
my (@oid,@index)=(undef,undef);
330
foreach my $key ( keys %$resultat) {
331
verb("OID : $key, Desc : $$resultat{$key}");
332
if ( $key =~ /$cisco_index/ ) {
333
@oid=split (/\./,$key);
334
$index[$nindex++] = pop(@oid);
338
# Check if at least 1 memory pool exists
340
printf("ERROR: No memory pools found");
342
exit $ERRORS{"UNKNOWN"};
345
# Test every memory pool
346
my ($c_output,$prct_free)=(undef,undef);
347
my ($warn_s,$crit_s)=(0,0);
348
my ($used,$free)=(0,0);
350
$c_output .="," if defined ($c_output);
351
if ( $$resultat{$cisco_valid . "." . $_} == 1 ) {
352
$used += $$resultat{$cisco_used . "." . $_};
353
$free += $$resultat{$cisco_free . "." . $_};
354
$prct_free=round($$resultat{$cisco_used . "." . $_}*100/($$resultat{$cisco_free . "." . $_}+$$resultat{$cisco_used . "." . $_}) ,0);
355
$c_output .= $$resultat{$cisco_index . "." . $_} . ":" . $prct_free . "%";
356
if (($o_crit!=0)&&($o_crit <= $prct_free)) {
358
} elsif (($o_warn!=0)&&($o_warn <= $prct_free)) {
362
$c_output .= $$resultat{$cisco_index . "." . $_} . ": INVALID";
366
my $total=$used+$free;
367
$prct_free=round($used*100/($total),0);
368
verb("Total used : $used, free: $free, output : $c_output");
370
$c_output .=" : " . $prct_free ."% : ";
372
$c_output .= " > " . $o_crit ;
373
$c_status="CRITICAL";
376
$c_output.=" > " . $o_warn;
380
$c_output .= " ; ".$c_status;
381
if (defined ($o_perf)) {
382
$c_output .= " | ram_used=" . $used.";";
383
$c_output .= ($o_warn ==0)? ";" : round($o_warn * $total/100,0).";";
384
$c_output .= ($o_crit ==0)? ";" : round($o_crit * $total/100,0).";";
385
$c_output .= "0;" . $total ;
388
print "$c_output \n";
389
exit $ERRORS{$c_status};
392
########### HP Procurve memory check ############
393
if (defined ($o_hp)) {
395
# Get hp memory table
396
$resultat = (Net::SNMP->VERSION < 4) ?
397
$session->get_table($hp_mem_pool)
398
:$session->get_table(Baseoid => $hp_mem_pool);
400
if (!defined($resultat)) {
401
printf("ERROR: Description table : %s.\n", $session->error);
403
exit $ERRORS{"UNKNOWN"};
405
my (@oid,@index)=(undef,undef);
407
foreach my $key ( keys %$resultat) {
408
verb("OID : $key, Desc : $$resultat{$key}");
409
if ( $key =~ /$hp_mem_index/ ) {
410
@oid=split (/\./,$key);
411
$index[$nindex++] = pop(@oid);
415
# Check if at least 1 memory slots exists
417
printf("ERROR: No memory slots found");
419
exit $ERRORS{"UNKNOWN"};
422
# Consolidate the datas
423
my ($total,$free)=(0,0);
424
my ($c_output,$prct_free)=(undef,undef);
426
$c_output .="," if defined ($c_output);
427
$total += $$resultat{$hp_mem_total . "." . $_};
428
$free += $$resultat{$hp_mem_free . "." . $_};
429
$c_output .= "Slot " . $$resultat{$hp_mem_index . "." . $_} . ":"
431
100 - ($$resultat{$hp_mem_free . "." . $_} *100 /
432
$$resultat{$hp_mem_total . "." . $_}) ,0)
435
my $used = $total - $free;
436
$prct_free=round($used*100/($total),0);
437
verb("Used : $used, Free: $free, Output : $c_output");
439
$c_output .=" : " . $prct_free ."% : ";
440
if (($o_crit!=0)&&($o_crit <= $prct_free)) {
441
$c_output .= " > " . $o_crit ;
442
$c_status="CRITICAL";
444
if (($o_warn!=0)&&($o_warn <= $prct_free)) {
445
$c_output.=" > " . $o_warn;
449
$c_output .= " ; ".$c_status;
450
if (defined ($o_perf)) {
451
$c_output .= " | ram_used=" . $used.";";
452
$c_output .= ($o_warn ==0)? ";" : round($o_warn * $total/100,0).";";
453
$c_output .= ($o_crit ==0)? ";" : round($o_crit * $total/100,0).";";
454
$c_output .= "0;" . $total ;
457
print "$c_output \n";
458
exit $ERRORS{$c_status};
461
########### Net snmp memory check ############
462
if (defined ($o_netsnmp)) {
464
# Get NetSNMP memory values
465
$resultat = (Net::SNMP->VERSION < 4) ?
466
$session->get_request(@nets_oids)
467
:$session->get_request(-varbindlist => \@nets_oids);
469
if (!defined($resultat)) {
470
printf("ERROR: netsnmp : %s.\n", $session->error);
472
exit $ERRORS{"UNKNOWN"};
475
my ($realused,$swapused)=(undef,undef);
477
$realused= defined($o_cache) ?
478
($$resultat{$nets_ram_total}-$$resultat{$nets_ram_free})/$$resultat{$nets_ram_total}
480
($$resultat{$nets_ram_total}-($$resultat{$nets_ram_free}+$$resultat{$nets_ram_cache}))/$$resultat{$nets_ram_total};
482
if($$resultat{$nets_ram_total} == 0) { $realused = 0; }
484
$swapused= ($$resultat{$nets_swap_total} == 0) ? 0 :
485
($$resultat{$nets_swap_total}-$$resultat{$nets_swap_free})/$$resultat{$nets_swap_total};
486
$realused=round($realused*100,0);
487
$swapused=round($swapused*100,0);
489
verb ("Ram : $$resultat{$nets_ram_free} / $$resultat{$nets_ram_total} : $realused")
491
verb ("Ram : $$resultat{$nets_ram_free} ($$resultat{$nets_ram_cache} cached) / $$resultat{$nets_ram_total} : $realused");
492
verb ("Swap : $$resultat{$nets_swap_free} / $$resultat{$nets_swap_total} : $swapused");
495
my $n_output="Ram : " . $realused . "%, Swap : " . $swapused . "% :";
496
if ((($o_critR!=0)&&($o_critR <= $realused)) || (($o_critS!=0)&&($o_critS <= $swapused))) {
497
$n_output .= " > " . $o_critR . ", " . $o_critS;
498
$n_status="CRITICAL";
500
if ((($o_warnR!=0)&&($o_warnR <= $realused)) || (($o_warnS!=0)&&($o_warnS <= $swapused))) {
501
$n_output.=" > " . $o_warnR . ", " . $o_warnS;
505
$n_output .= " ; ".$n_status;
506
if (defined ($o_perf)) {
507
if (defined ($o_cache)) {
508
$n_output .= " | ram_used=" . ($$resultat{$nets_ram_total}-$$resultat{$nets_ram_free}).";";
511
$n_output .= " | ram_used=" . ($$resultat{$nets_ram_total}-$$resultat{$nets_ram_free}-$$resultat{$nets_ram_cache}).";";
513
$n_output .= ($o_warnR ==0)? ";" : round($o_warnR * $$resultat{$nets_ram_total}/100,0).";";
514
$n_output .= ($o_critR ==0)? ";" : round($o_critR * $$resultat{$nets_ram_total}/100,0).";";
515
$n_output .= "0;" . $$resultat{$nets_ram_total}. " ";
516
$n_output .= "swap_used=" . ($$resultat{$nets_swap_total}-$$resultat{$nets_swap_free}).";";
517
$n_output .= ($o_warnS ==0)? ";" : round($o_warnS * $$resultat{$nets_swap_total}/100,0).";";
518
$n_output .= ($o_critS ==0)? ";" : round($o_critS * $$resultat{$nets_swap_total}/100,0).";";
519
$n_output .= "0;" . $$resultat{$nets_swap_total};
522
print "$n_output \n";
523
exit $ERRORS{$n_status};