Plugin:check centreon snmp memory


 * 1) ! /usr/bin/perl -w
 * 2) Copyright 2004-2013 MERETHIS
 * 3) Centreon is developped by : Julien Mathis and Romain Le Merlus under
 * 4) GPL Licence 2.0.
 * 5) This program is free software; you can redistribute it and/or modify it under
 * 6) the terms of the GNU General Public License as published by the Free Software
 * 7) Foundation ; either version 2 of the License.
 * 8) This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * 9) WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * 10) PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 11) You should have received a copy of the GNU General Public License along with
 * 12) this program; if not, see .
 * 13) Linking this program statically or dynamically with other modules is making a
 * 14) combined work based on this program. Thus, the terms and conditions of the GNU
 * 15) General Public License cover the whole combination.
 * 16) As a special exception, the copyright holders of this program give MERETHIS
 * 17) permission to link this program with independent modules to produce an executable,
 * 18) regardless of the license terms of these independent modules, and to copy and
 * 19) distribute the resulting executable under terms of MERETHIS choice, provided that
 * 20) MERETHIS also meet, for each linked independent module, the terms  and conditions
 * 21) of the license of that module. An independent module is a module which is not
 * 22) derived from this program. If you modify this program, you may extend this
 * 23) exception to your version of the program, but you are not obliged to do so. If you
 * 24) do not wish to do so, delete this exception statement from your version.
 * 25) For more information : contact@centreon.com
 * 26) SVN : $URL$
 * 27) SVN : $Id$
 * 28) Script init
 * 1) exception to your version of the program, but you are not obliged to do so. If you
 * 2) do not wish to do so, delete this exception statement from your version.
 * 3) For more information : contact@centreon.com
 * 4) SVN : $URL$
 * 5) SVN : $Id$
 * 6) Script init
 * 1) SVN : $Id$
 * 2) Script init
 * 1) Script init
 * 1) Script init
 * 1) Script init

use strict; require "/usr/lib/nagios/plugins/Centreon/SNMP/Utils.pm";

use vars qw($PROGNAME); use Getopt::Long; use vars qw($opt_V $opt_h $opt_w $opt_c $opt_s);

my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3); my %centreon = Centreon::SNMP::Utils::load_oids($ERRORS{'UNKNOWN'}, "/usr/lib/nagios/plugins/centreon.conf");


 * 1) Plugin var init
 * 1) Plugin var init

my ($hrStorageDescr, $hrStorageAllocationUnits, $hrStorageSize, $hrStorageUsed); my ($AllocationUnits, $Size, $Used); my ($tot, $used, $pourcent, $return_code);

$PROGNAME = "$0"; sub print_help ; sub print_usage ;

my %OPTION = (   "host" => undef,    "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161,     "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",    "snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",    "maxrepetitions" => undef, "snmptimeout" => undef,    "64-bits" => undef,    "disable-swap" => undef );

Getopt::Long::Configure('bundling'); GetOptions (   "H|hostname|host=s"         => \$OPTION{'host'},    "C|community=s"             => \$OPTION{'snmp-community'},    "v|snmp|snmp-version=s"     => \$OPTION{'snmp-version'},    "P|snmpport|snmp-port=i"    => \$OPTION{'snmp-port'},    "u|username=s"              => \$OPTION{'snmp-auth-user'},    "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},    "k|authkey=s"               => \$OPTION{'snmp-auth-key'},    "authprotocol=s"            => \$OPTION{'snmp-auth-protocol'},    "privpassword=s"            => \$OPTION{'snmp-priv-password'},    "privkey=s"                 => \$OPTION{'snmp-priv-key'},    "privprotocol=s"            => \$OPTION{'snmp-priv-protocol'},    "maxrepetitions=s"          => \$OPTION{'maxrepetitions'},    "snmp-timeout=i"            => \$OPTION{'snmptimeout'},    "64-bits"                   => \$OPTION{'64-bits'},    "S"     => \$OPTION{'disable-swap'}, "h"    => \$opt_h, "help"         => \$opt_h, "V"    => \$opt_V, "version"      => \$opt_V, "w=s"  => \$opt_w, "warning=s"    => \$opt_w, "c=s"  => \$opt_c, "critical=s"   => \$opt_c, "s=s"  => \$opt_s, "swap=s"       => \$opt_s);

if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.2 $'); exit $ERRORS{'OK'}; }

if ($opt_h) { print_help; exit $ERRORS{'OK'}; }

my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);

($opt_c) || ($opt_c = shift) || ($opt_c = 95); my $critical = $1 if ($opt_c =~ /([0-9]+)/);

($opt_w) || ($opt_w = shift) || ($opt_w = 80); my $warning = $1 if ($opt_w =~ /([0-9]+)/); if ($critical <= $warning){ print "(--crit) must be superior to (--warn)"; print_usage; exit $ERRORS{'OK'}; }

my $swap_limit; if ($opt_s) { ($opt_s) || ($opt_s = shift) || ($opt_s = 10); $swap_limit = $1 if ($opt_s =~ /([0-9]+)/); }

my $start=time; my $name = $0;


 * 1) Plugin snmp requests


 * 1) my $OID_hrStorageDescr = $centreon{MIB2}{HR_STORAGE_DESCR};
 * 2) my $OID_hrStorageAllocationUnits =$centreon{MIB2}{HR_STORAGE_ALLOCATION_UNITS};
 * 3) my $OID_hrStorageSize =$centreon{MIB2}{HR_STORAGE_SIZE};
 * 4) my $OID_hrStorageUsed =$centreon{MIB2}{HR_STORAGE_USED};

my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);
 * 1) create a SNMP session


 * 1) snmp request
 * 1) snmp request

my $OID_descr_storage = ".1.3.6.1.2.1.25.2.3.1.3"; #"1.3.6.1.2.1.25.2.3.1.1"; my $result = Centreon::SNMP::Utils::get_snmp_table($OID_descr_storage, $session, $ERRORS{'UNKNOWN'}, \%OPTION);

my ($virt_alloc, my $virt_used, my $virt_size);

my $indexV = 0; my $indexR = 0; my $indexC = 0;

foreach my $key (oid_lex_sort(keys %$result)) { if ($result->{$key} =~ m/Swap|Virtual/) { my @cpt = split /\./,$key; $indexV = $cpt[scalar(@cpt) - 1];; }   if  ($result->{$key} =~ m/Real|Physical/) { my @cpt = split /\./,$key; $indexR = $cpt[scalar(@cpt)-1]; }   if  ($result->{$key} =~ m/Cached/) { my @cpt = split /\./,$key; $indexC = $cpt[scalar(@cpt)-1]; } } if ($indexV == 0 || $indexR == 0) { printf("ERROR: cannot find ram information"); exit $ERRORS{'UNKNOWN'}; } my $OID_hrStorage_used = ".1.3.6.1.2.1.25.2.3.1.6"; my $OID_Swap_storage_used = ".1.3.6.1.2.1.25.2.3.1.6.".$indexV; my $OID_RealM_storage_used = ".1.3.6.1.2.1.25.2.3.1.6.".$indexR;

my $used_mem = Centreon::SNMP::Utils::get_snmp_table($OID_hrStorage_used, $session, $ERRORS{'UNKNOWN'}, \%OPTION);

my $OID_hrStorage_size = ".1.3.6.1.2.1.25.2.3.1.5"; my $OID_Swap_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexV; my $OID_RealM_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexR; my $OID_Cache_storage_size = ".1.3.6.1.2.1.25.2.3.1.5.".$indexC;

my $total_mem = Centreon::SNMP::Utils::get_snmp_table($OID_hrStorage_size, $session, $ERRORS{'UNKNOWN'}, \%OPTION);

my $OID_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4"; my $OID_Swap_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexV; my $OID_RealM_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexR; my $OID_Cache_storage_allocationUnits = ".1.3.6.1.2.1.25.2.3.1.4.".$indexC; my $alloc_units = Centreon::SNMP::Utils::get_snmp_table($OID_storage_allocationUnits, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my $swap_used = $used_mem->{$OID_Swap_storage_used} * $alloc_units->{$OID_Swap_storage_allocationUnits}; my $realM_used = $used_mem->{$OID_RealM_storage_used} * $alloc_units->{$OID_RealM_storage_allocationUnits};

my $cache_used = 0;

if (defined $indexC > 0) { if (defined($total_mem->{$OID_Cache_storage_size}) && defined($alloc_units->{$OID_Cache_storage_allocationUnits})) { $cache_used = $total_mem->{$OID_Cache_storage_size} * $alloc_units->{$OID_Cache_storage_allocationUnits}; } }

my $swap_size = $total_mem->{$OID_Swap_storage_size} * $alloc_units->{$OID_Swap_storage_allocationUnits}; my $realM_size = $total_mem->{$OID_RealM_storage_size} * $alloc_units->{$OID_RealM_storage_allocationUnits}; my ($total_memory_used, $total_memory_size); if (defined($OPTION{'disable-swap'})) { $total_memory_used = $realM_used - $cache_used; $total_memory_size = $realM_size; } else { $total_memory_used = $swap_used + $realM_used - $cache_used; $total_memory_size = $swap_size + $realM_size; }

if ($swap_size eq "0"){ $swap_size = 1; }
 * 1) percentage of total, physical and swap  memory used

my $percent_used = ($total_memory_used/$total_memory_size)*100; my $percent_swap_used = ($swap_used/$swap_size) * 100; my $percent_realM_used = ($realM_used/$realM_size) * 100; $percent_swap_used =~ s/\.[0-9]+//; $percent_used =~ s/\.[0-9]+//; $percent_realM_used =~ s/\.[0-9]+//;


 * 1) return

if (($opt_s) && ($opt_s =~ /([0-9]+)/)) { if (($percent_realM_used >= 99) && ($percent_swap_used > $swap_limit)) { print "Swap threshold (".$opt_s."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used : ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; exit $ERRORS{'CRITICAL'}; } }

if ($percent_used >= $opt_c){ print "Threshold (".$opt_c."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used : ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; exit $ERRORS{'CRITICAL'}; } elsif ($percent_used >= $opt_w){ print "Threshold (".$opt_w."%) excedeed : total memory used : ".$percent_used."%, ram used : ".$percent_realM_used."%, swap used ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; exit $ERRORS{'WARNING'}; } else { print "Total memory used : ".$percent_used."% ram used : ".$percent_realM_used."%, swap used ".$percent_swap_used."% | used=".$total_memory_used."o size=".$total_memory_size."o\n"; exit $ERRORS{'OK'}; }

sub print_usage { print "\nUsage:\n"; print "$PROGNAME\n"; print "  -H (--hostname)   Hostname to query (required)\n"; print "  -C (--community)  SNMP read community (defaults to public)\n"; print "                    used with SNMP v1 and v2c\n"; print "  -v (--snmp-version)  1 for SNMP v1 (default)\n"; print "                       2 for SNMP v2c\n"; print "                       3 for SNMP v3\n"; print "  -P (--snmp-port)  SNMP port (default: 161)\n"; print "  -k (--authkey)    snmp V3 key\n"; print "  -u (--username)   snmp V3 username \n"; print "  -p (--password)   snmp V3 password\n"; print "  --authprotocol    protocol MD5/SHA  (v3)\n"; print "  --privprotocol    encryption system (DES/AES)(v3) \n"; print "  --privpassword    passphrase (v3) \n"; print "  --64-bits         Use 64 bits OID\n"; print "  --maxrepetitions  To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n"; print "                    Work only with SNMP v2c and v3 (Example: --maxrepetitions=1)\n"; print "  --snmp-timeout    SNMP Timeout\n"; print "  -V (--version)    Plugin version\n"; print "  -h (--help)       usage help\n"; print "  -c (--critical)   percentage of memory used at which a critical message will be generated\n"; print "  -w (--warning)    percentage of memory used at which a warning message will be generated\n"; print "  -s (--swap)       limit of swap memory can reach before the service turns critical, while phyical memory is near 100%\n"; print "  -S                Do not add swap size in total memory size, perfdata will show ram size corresponding physical size installed\n"; }

sub print_help { print "##############################################\n"; print "#   Copyright (c) 2004-2013 Centreon        #\n"; print "#   Bugs to http://forge.centreon.com/      #\n"; print "##############################################\n"; print_usage; print "\n"; }