Plugin:check centreon MS multiple services


 * 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_p $opt_n $result @result %process_list);

my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);


 * 1) Plugin var init

my($proc, $proc_run);

$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, );

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'},    "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'},    "h"     => \$opt_h, "help"          => \$opt_h, "V"    => \$opt_V, "version"       => \$opt_V, "n"    => \$opt_n, "number"        => \$opt_n, "p=s"  => \$opt_p, "process=s"     => \$opt_p);

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

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

my $process; if(!$opt_p) { print_usage; exit $ERRORS{'OK'}; } elsif ($opt_p !~ /([-.A-Za-z0-9\,]+)/){ print_usage; exit $ERRORS{'OK'}; } $process = $opt_p; my @process_temp = split /\,/,$opt_p; my (@proc,@status); foreach (@process_temp) { $status[scalar(@status)] = 0; $proc[scalar(@proc)] = $_; } my $name = $0;

my $OID_SW_ServiceName = "1.3.6.1.4.1.77.1.2.3.1.1"; my $OID_SW_ServiceInstallStatus = "1.3.6.1.4.1.77.1.2.3.1.2"; my $OID_SW_ServiceStatus = "1.3.6.1.4.1.77.1.2.3.1.3";
 * 1) Plugin snmp requests

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

$result = Centreon::SNMP::Utils::get_snmp_table($OID_SW_ServiceName, $session, $ERRORS{'UNKNOWN'}, \%OPTION); my %install_status = ( 1=>"Uninstalled", 2=> "Install-pending", 3=> "Uninstall-Pending", 4=>"Installed"); my %defined_status = ( 1=>"Active", 2=>"Continue-Pending", 3=> "Pause-Pending", 4=> "Paused"); foreach my $key (oid_lex_sort(keys %$result)) { my $found = 0; for(my $i = 0; $i < scalar(@proc) && !$found; $i++) { my $value = substr($key, length($OID_SW_ServiceName)+1, length($key)); if (lc $proc[$i] eq lc $result->{$key}) { my $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_ServiceInstallStatus . "." . $value], $session, $ERRORS{'UNKNOWN'}); if ((my $temp_installStatus_val = $result2->{$OID_SW_ServiceInstallStatus. "." . $value})) { if ($temp_installStatus_val != 1 && $temp_installStatus_val != 3) { $result2 = Centreon::SNMP::Utils::get_snmp_leef([$OID_SW_ServiceStatus . "." . $value], $session, $ERRORS{'UNKNOWN'}); $status[$i] = $defined_status{$result2->{$OID_SW_ServiceStatus. "." . $value}}; } else { $status[$i] = $install_status{$temp_installStatus_val}; }           }            $found = 1; }   } }

my $final_status = "OK"; my $active = ""; my $inactive = ""; my $unknown = ""; for (my $i = 0; $i < scalar(@proc); $i++) { if (defined($status[$i])) { my $written = 0; if ($final_status ne "CRITICAL" && $status[$i] eq "0") { $status[$i] = "Unknown"; $final_status = "WARNING"; $written = 1; $unknown .= $proc[$i]." - "; }       if ($final_status ne "CRITICAL" && $status[$i] eq "Uninstall-Pending" || $status[$i] eq "Pause-Pending") { $final_status = "WARNING"; $written = 1; $inactive .= $proc[$i]." - "; }       if ($status[$i] && ($status[$i] eq "Uninstalled" || $status[$i] eq "paused")) { $final_status = "CRITICAL"; $written = 1; $inactive .= $proc[$i]." - "; }       if (!$written) { $active .= $proc[$i]." - "; }   } } my $msg = ""; if ($inactive ne "") { $msg .= "INACTIVE : $inactive "; } if ($unknown ne "") { $msg .= "UNKNOWN : $unknown "; } if ($active ne "") { $msg .= "ACTIVE : $active"; } $msg =~ s/\- $//; print "$msg\n"; exit $ERRORS{$final_status};
 * 1) Plugin return code

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 "  --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 "  -n (--number)     Return the number of current running processes. \n"; print "  -p (--process)    Set the process name ex: by default smbd\n"; print "  -V (--version)    Plugin version\n"; print "  -h (--help)       usage help\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"; }