~ubuntu-branches/ubuntu/precise/nagios-snmp-plugins/precise

« back to all changes in this revision

Viewing changes to check_snmp_mem.pl

  • Committer: Bazaar Package Importer
  • Author(s): Jan Wagner
  • Date: 2007-04-25 22:00:50 UTC
  • Revision ID: james.westby@ubuntu.com-20070425220050-tesozvdx9rwgo0j4
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w 
 
2
############################## check_snmp_mem ##############
 
3
# Version : 1.1
 
4
# Date : Jul 09 2006
 
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
 
9
# TODO : 
 
10
#################################################################
 
11
#
 
12
# Help : ./check_snmp_mem.pl -h
 
13
#
 
14
 
 
15
use strict;
 
16
use Net::SNMP;
 
17
use Getopt::Long;
 
18
 
 
19
# Nagios specific
 
20
 
 
21
use lib "/usr/local/nagios/libexec";
 
22
use utils qw(%ERRORS $TIMEOUT);
 
23
#my $TIMEOUT = 15;
 
24
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
 
25
 
 
26
# SNMP Datas
 
27
 
 
28
# Net-snmp memory 
 
29
 
 
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);
 
36
 
 
37
# Cisco 
 
38
 
 
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
 
45
 
 
46
# HP Procurve
 
47
 
 
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
 
53
 
 
54
# AS/400 
 
55
 
 
56
# Windows NT/2K/(XP?)
 
57
 
 
58
# check_snmp_storage.pl -C <community> -H <hostIP> -m "^Virtual Memory$"  -w <warn %> -c <crit %>
 
59
 
 
60
 
 
61
# Globals
 
62
 
 
63
my $Version='1.1';
 
64
 
 
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
 
84
# SNMPv3 specific
 
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
 
91
 
 
92
# functions
 
93
 
 
94
sub p_version { print "check_snmp_mem version : $Version\n"; }
 
95
 
 
96
sub print_usage {
 
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";
 
98
}
 
99
 
 
100
sub isnnum { # Return true if arg is not a number
 
101
  my $num = shift;
 
102
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
 
103
  return 1;
 
104
}
 
105
 
 
106
sub round ($$) {
 
107
    sprintf "%.$_[1]f", $_[0];
 
108
}
 
109
 
 
110
sub help {
 
111
   print "\nSNMP Memory Monitor for Nagios version ",$Version,"\n";
 
112
   print "(c)2004-2006 to my cat Ratoune - Author: Patrick Proy\n\n";
 
113
   print_usage();
 
114
   print <<EOT;
 
115
-v, --verbose
 
116
   print extra debugging information (including interface list on the system)
 
117
-h, --help
 
118
   print this help message
 
119
-H, --hostname=HOST
 
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)
 
123
-2, --v2c
 
124
   Use snmp v2c
 
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) 
 
133
-P, --port=PORT
 
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 
 
145
-m, --memcache
 
146
   include cached memory in used memory (only with Net-SNMP)
 
147
-I, --cisco
 
148
   check cisco memory (sum of all memory pools)
 
149
-E, --hp
 
150
   check HP proccurve memory
 
151
-f, --perfdata
 
152
   Performance data output
 
153
-t, --timeout=INTEGER
 
154
   timeout for SNMP in seconds (Default: 5)
 
155
-V, --version
 
156
   prints version number
 
157
EOT
 
158
}
 
159
 
 
160
# For verbose output
 
161
sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }
 
162
 
 
163
# Get the alarm signal (just in case snmp timout screws up)
 
164
$SIG{'ALRM'} = sub {
 
165
     print ("ERROR: Alarm signal (Nagios time-out)\n");
 
166
     exit $ERRORS{"UNKNOWN"};
 
167
};
 
168
 
 
169
sub check_options {
 
170
    Getopt::Long::Configure ("bundling");
 
171
        GetOptions(
 
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
 
191
    );
 
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"}}
 
208
        }
 
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"}}
 
215
    # Get rid of % sign
 
216
    $o_warn =~ s/\%//g; 
 
217
    $o_crit =~ s/\%//g;
 
218
    # if -N or -E switch , undef $o_netsnmp
 
219
    if (defined($o_cisco) || defined($o_hp) ) {
 
220
      $o_netsnmp=undef;
 
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"}}
 
225
    }
 
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"}}
 
238
      }
 
239
      $o_warnR=$o_warnL[0];$o_warnS=$o_warnL[1];
 
240
      $o_critR=$o_critL[0];$o_critS=$o_critL[1];
 
241
    }
 
242
  
 
243
}
 
244
 
 
245
########## MAIN #######
 
246
 
 
247
check_options();
 
248
 
 
249
# Check gobal timeout if snmp screws up
 
250
if (defined($TIMEOUT)) {
 
251
  verb("Alarm at $TIMEOUT");
 
252
  alarm($TIMEOUT);
 
253
} else {
 
254
  verb("no timeout defined : $o_timeout + 10");
 
255
  alarm ($o_timeout+10);
 
256
}
 
257
 
 
258
# Connect to host
 
259
my ($session,$error);
 
260
if ( defined($o_login) && defined($o_passwd)) {
 
261
  # SNMPv3 login
 
262
  if (!defined ($o_privpass)) {
 
263
  verb("SNMPv3 AuthNoPriv login : $o_login, $o_authproto");
 
264
    ($session, $error) = Net::SNMP->session(
 
265
      -hostname         => $o_host,
 
266
      -version          => '3',
 
267
      -username         => $o_login,
 
268
      -authpassword     => $o_passwd,
 
269
      -authprotocol     => $o_authproto,
 
270
      -timeout          => $o_timeout
 
271
    );  
 
272
  } else {
 
273
    verb("SNMPv3 AuthPriv login : $o_login, $o_authproto, $o_privproto");
 
274
    ($session, $error) = Net::SNMP->session(
 
275
      -hostname         => $o_host,
 
276
      -version          => '3',
 
277
      -username         => $o_login,
 
278
      -authpassword     => $o_passwd,
 
279
      -authprotocol     => $o_authproto,
 
280
      -privpassword     => $o_privpass,
 
281
          -privprotocol => $o_privproto,
 
282
      -timeout      => $o_timeout
 
283
    );
 
284
  }
 
285
} else {
 
286
   if (defined ($o_version2)) {
 
287
     # SNMPv2 Login
 
288
         verb("SNMP v2c login");
 
289
         ($session, $error) = Net::SNMP->session(
 
290
        -hostname  => $o_host,
 
291
            -version   => 2,
 
292
        -community => $o_community,
 
293
        -port      => $o_port,
 
294
        -timeout   => $o_timeout
 
295
     );
 
296
   } else {
 
297
    # SNMPV1 login
 
298
        verb("SNMP v1 login");
 
299
    ($session, $error) = Net::SNMP->session(
 
300
       -hostname  => $o_host,
 
301
       -community => $o_community,
 
302
       -port      => $o_port,
 
303
       -timeout   => $o_timeout
 
304
    );
 
305
  }
 
306
}
 
307
if (!defined($session)) {
 
308
   printf("ERROR opening session: %s.\n", $error);
 
309
   exit $ERRORS{"UNKNOWN"};
 
310
}
 
311
 
 
312
# Global variable
 
313
my $resultat=undef;
 
314
 
 
315
########### Cisco memory check ############
 
316
if (defined ($o_cisco)) {
 
317
 
 
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);
 
322
  
 
323
  if (!defined($resultat)) {
 
324
    printf("ERROR: Description table : %s.\n", $session->error);
 
325
    $session->close;
 
326
    exit $ERRORS{"UNKNOWN"};
 
327
  }
 
328
  my (@oid,@index)=(undef,undef);
 
329
  my $nindex=0;
 
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);
 
335
     }
 
336
  }
 
337
  
 
338
  # Check if at least 1 memory pool exists
 
339
  if ($nindex == 0) { 
 
340
   printf("ERROR: No memory pools found");
 
341
   $session->close;
 
342
   exit $ERRORS{"UNKNOWN"};
 
343
  }
 
344
 
 
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);
 
349
  foreach (@index) {
 
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)) { 
 
357
        $crit_s =1;
 
358
      } elsif (($o_warn!=0)&&($o_warn <= $prct_free)) {
 
359
        $warn_s=1;
 
360
      }
 
361
    } else {
 
362
      $c_output .= $$resultat{$cisco_index . "." . $_} . ": INVALID";
 
363
      $crit_s =1;
 
364
    }
 
365
  }
 
366
  my $total=$used+$free; 
 
367
  $prct_free=round($used*100/($total),0);
 
368
  verb("Total used : $used, free: $free, output : $c_output");
 
369
  my $c_status="OK";
 
370
  $c_output .=" : " . $prct_free ."% : ";
 
371
  if ($crit_s == 1 ) {
 
372
    $c_output .= " > " . $o_crit ;
 
373
    $c_status="CRITICAL";
 
374
  } else {
 
375
    if ($warn_s == 1 ) {
 
376
      $c_output.=" > " . $o_warn;
 
377
      $c_status="WARNING";
 
378
    }
 
379
  }
 
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 ;
 
386
  }             
 
387
  $session->close; 
 
388
  print "$c_output \n";
 
389
  exit $ERRORS{$c_status};
 
390
}
 
391
 
 
392
########### HP Procurve memory check ############
 
393
if (defined ($o_hp)) {
 
394
 
 
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);
 
399
  
 
400
  if (!defined($resultat)) {
 
401
    printf("ERROR: Description table : %s.\n", $session->error);
 
402
    $session->close;
 
403
    exit $ERRORS{"UNKNOWN"};
 
404
  }
 
405
  my (@oid,@index)=(undef,undef);
 
406
  my $nindex=0;
 
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);
 
412
     }
 
413
  }
 
414
  
 
415
  # Check if at least 1 memory slots exists
 
416
  if ($nindex == 0) { 
 
417
   printf("ERROR: No memory slots found");
 
418
   $session->close;
 
419
   exit $ERRORS{"UNKNOWN"};
 
420
  }
 
421
 
 
422
  # Consolidate the datas
 
423
  my ($total,$free)=(0,0);
 
424
  my ($c_output,$prct_free)=(undef,undef);
 
425
  foreach (@index) {
 
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 . "." . $_} . ":" 
 
430
                 .round( 
 
431
                   100 - ($$resultat{$hp_mem_free . "." . $_} *100 / 
 
432
                        $$resultat{$hp_mem_total . "." . $_}) ,0)
 
433
                 . "%";
 
434
  }
 
435
  my $used = $total - $free; 
 
436
  $prct_free=round($used*100/($total),0);
 
437
  verb("Used : $used, Free: $free, Output : $c_output");
 
438
  my $c_status="OK";
 
439
  $c_output .=" : " . $prct_free ."% : ";
 
440
  if (($o_crit!=0)&&($o_crit <= $prct_free)) {
 
441
    $c_output .= " > " . $o_crit ;
 
442
    $c_status="CRITICAL";
 
443
  } else {
 
444
    if (($o_warn!=0)&&($o_warn <= $prct_free)) {
 
445
      $c_output.=" > " . $o_warn;
 
446
      $c_status="WARNING";
 
447
    }
 
448
  }
 
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 ;
 
455
  }             
 
456
  $session->close; 
 
457
  print "$c_output \n";
 
458
  exit $ERRORS{$c_status};
 
459
}
 
460
 
 
461
########### Net snmp memory check ############
 
462
if (defined ($o_netsnmp)) {
 
463
 
 
464
  # Get NetSNMP memory values
 
465
  $resultat = (Net::SNMP->VERSION < 4) ?
 
466
                $session->get_request(@nets_oids)
 
467
                :$session->get_request(-varbindlist => \@nets_oids);
 
468
  
 
469
  if (!defined($resultat)) {
 
470
    printf("ERROR: netsnmp : %s.\n", $session->error);
 
471
    $session->close;
 
472
    exit $ERRORS{"UNKNOWN"};
 
473
  }
 
474
  
 
475
  my ($realused,$swapused)=(undef,undef);
 
476
  
 
477
  $realused= defined($o_cache) ? 
 
478
    ($$resultat{$nets_ram_total}-$$resultat{$nets_ram_free})/$$resultat{$nets_ram_total}
 
479
  :
 
480
    ($$resultat{$nets_ram_total}-($$resultat{$nets_ram_free}+$$resultat{$nets_ram_cache}))/$$resultat{$nets_ram_total};
 
481
 
 
482
  if($$resultat{$nets_ram_total} == 0) { $realused = 0; }
 
483
 
 
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);
 
488
  defined($o_cache) ? 
 
489
    verb ("Ram : $$resultat{$nets_ram_free} / $$resultat{$nets_ram_total} : $realused")
 
490
    :
 
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");
 
493
  
 
494
  my $n_status="OK";
 
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";
 
499
  } else {
 
500
    if ((($o_warnR!=0)&&($o_warnR <= $realused)) || (($o_warnS!=0)&&($o_warnS <= $swapused))) {
 
501
      $n_output.=" > " . $o_warnR . ", " . $o_warnS;
 
502
      $n_status="WARNING";
 
503
    }
 
504
  }
 
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}).";";
 
509
    }
 
510
    else {
 
511
      $n_output .= " | ram_used=" . ($$resultat{$nets_ram_total}-$$resultat{$nets_ram_free}-$$resultat{$nets_ram_cache}).";";
 
512
    }
 
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};
 
520
  }  
 
521
  $session->close;
 
522
  print "$n_output \n";
 
523
  exit $ERRORS{$n_status};
 
524
 
 
525
}