#!/usr/bin/perl # # # AUTHORS: # Copyright (C) 2003-2012 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 lib qw ( /usr/local/nagios/perl/lib ); use Net::SNMP 4.1.0 qw(DEBUG_ALL); use Getopt::Std; $script = "check_runningproc"; $script_version = "2.1"; # $metric = 1; $firsttime = 0; $change = 0; $interfacesup = 0; $interfacesdown = 0; @matches = (); $oid_sysDescr = ".1.3.6.1.2.1.1.1.0"; $hrswrunname = ".1.3.6.1.2.1.25.4.2.1.2.1"; $basehrswrunname = ".1.3.6.1.2.1.25.4.2.1.2"; $hostname = ''; $version = "1"; $retries = 2; $timeout = 4; $returnstring = ""; my $port = 161; $community = "public"; # Default community string $configfilepath = "/usr/local/nagios/etc"; # Do we have enough information? if ( @ARGV < 1 ) { print "Too few arguments\n"; usage(); } getopts("hH:C:P:p:r:t:"); if ($opt_h) { usage(); exit(0); } if ($opt_H) { $hostname = $opt_H; # print "Hostname $opt_H\n"; } else { print "No hostname specified\n"; usage(); } if ($opt_C) { $community = $opt_C; # print "Using community $opt_C\n"; } else { # print "Using community $community\n"; } if ($opt_P) { $processes = lc($opt_P); $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); } if ($opt_r) { $retries = $opt_r; } if ($opt_t) { $timeout = $opt_t; } if ($opt_p) { $port = $opt_p; } # Create the SNMP session my ( $s, $e ) = Net::SNMP->session( -community => $community, -hostname => $hostname, -version => $version, -retries => $retries, -timeout => $timeout, -port => $port, ); # 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"; } sub usage { print << "USAGE"; $script v$script_version Checks whether specified process is running Usage: $script -H -c [...] Options: -H Hostname or IP address -p Port (default: 161) -C Community (default is public) -p Process name (eg: httpd, snmpd) -t Timeout after seconds -r Retry SNMP connection time USAGE exit 1; }