#!/usr/bin/perl # # # AUTHORS: # Copyright (C) 2003-2013 Opsview Limited. All rights reserved # # This file is part of Opsview # # Opsview is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Opsview is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Opsview; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # use strict; use warnings; use FindBin qw($Bin); use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib"; use Net::SNMP; use Opsview::NagiosPlugin::SNMP; my $np = Opsview::NagiosPlugin::SNMP->new( usage => "Usage: %s", shortname => "check_snmp_checkproc", version => "2.2", blurb => "Checks whether specified process is running", snmp => { "snmp-version" => 1, "snmp-timeout" => 4, }, ); $np->add_arg( spec => "processes|P=s", help => qq{-P, --processes=STRING Process name (eg: httpd, snmpd)}, #default => 60, ); $np->add_arg( spec => "timeout|t=s", help => qq{-t, --timeout=INTEGER Timeout after seconds}, default => 4, ); $np->add_arg( spec => "retries|r=s", help => qq{-r, --retries=INTEGER Retry SNMP connection time}, default => 2, ); $np->getopts; my $retries = $np->opts->retries; my $timeout = $np->opts->timeout; my $processes = $np->opts->processes; my $oid_sysDescr = ".1.3.6.1.2.1.1.1.0"; my $hrswrunname = ".1.3.6.1.2.1.25.4.2.1.2.1"; my $basehrswrunname = ".1.3.6.1.2.1.25.4.2.1.2"; my $s = $np->snmp; my $status = 0; my $returnstring = ""; my $counter = 0; my $line; my $critical = 0; my $oid; my $returnedprocess; my $sysdescr; my $tempstring; my $matches; my @processes; my @matches; #Make sure process is provided if ( defined($processes) ) { $processes = lc($processes); $processes =~ s/\.exe//g; # removes .exe file extentions $processes =~ s/\.ex//g; # removes .ex file extentions $processes =~ s/\.e//g; # removes .e file extentions } else { print "No processes specified\n"; exit(1); } # Works out what type of device we are looking at # and chances the OID if necessary if ( !defined( $s->get_request(".1.3.6.1.2.1.1.1.0") ) ) { if ( !defined( $s->get_request($oid_sysDescr) ) ) { print "Status is a Warning Level - SNMP agent not responding\n"; exit 1; } else { print "Status is a Warning Level - SNMP OID does not exist\n"; exit 1; } } else { foreach ( $s->var_bind_names() ) { $oid = $_; $sysdescr = $s->var_bind_list()->{$oid}; } } # if($sysdescr =~ /3Com/){ # $ifdescr = ".1.3.6.1.2.1.2.2.1.2.101"; # } $tempstring = ""; while ( $processes ne $tempstring ) { $processes =~ s/,/ /g; # replaces ',' with ' ' to make parsing easier $processes =~ /(\w+)/; # Finds first word $tempstring = $1; $tempstring =~ s/ //g; # removes spaces $processes =~ s/$tempstring //g; # removes dmy from string push( @processes, $tempstring ); } # Initialises array foreach $line (@processes) { $matches[$counter] = 0; $counter++; } $matches = 0; while ( $hrswrunname =~ /$basehrswrunname/ ) { if ( !defined( $s->get_request($hrswrunname) ) ) { exit 1; } else { foreach ( $s->var_bind_names() ) { $oid = $_; $returnedprocess = $s->var_bind_list()->{$oid}; $returnedprocess = lc($returnedprocess); $returnedprocess =~ s/\.exe//g; # removes .exe file extentions $returnedprocess =~ s/\.ex//g; # removes .ex file extentions $returnedprocess =~ s/\.e//g; # removes .e file extentions $counter = 0; foreach $line (@processes) { if ( $returnedprocess =~ m/$line/ ) { $matches[$counter] = $matches[$counter] + 1; } $counter++; } } } if ( !defined( $s->get_next_request($hrswrunname) ) ) { if ( !defined( $s->get_request($oid_sysDescr) ) ) { $returnstring = "SNMP agent not responding"; $critical = 1; } else { $returnstring = "SNMP OID does not exist"; $critical = 1; } } else { foreach ( $s->var_bind_names() ) { $hrswrunname = $_; } } } $counter = 0; $critical = 0; foreach $line (@processes) { append( "$line($matches[$counter]) " ); if ( $matches[$counter] < 1 ) { $critical = 1; } $counter++; } if ( $critical == 0 ) { print "Status is OK - Instances: $returnstring\n"; exit 0; } else { print "Status is Critical - Instances: $returnstring\n"; exit 2; } # Close the session $s->close(); exit 0; sub append { my $appendstring = $_[0]; $returnstring = "$returnstring$appendstring"; }