#! /usr/bin/perl -w # # Nagios plugin to check a remote process over rsh or ssh # This plugin allows to check a number of process expected and allows to exclude some process # # Written by: Guillaume AUCHERE # e-mail: guil.auchere@laposte.net # # # Plugin tested on Linux, Solaris, AIX (remote target) # # License Information: # This program 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. # # This program 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. # # ############################################################################ use POSIX; use strict; use Getopt::Long; use vars qw($opt_shell $opt_host $opt_process $opt_exclusion $opt_number $opt_t $opt_help @exclude $longueur $grep_v $i $RMPROC $PRGNAME $VERSION); use lib "/usr/local/nagios/libexec" ; use utils qw(%ERRORS &print_revision &support &usage ); $PRGNAME="check_remote_process"; $VERSION="1.0"; sub show_help(); sub print_usage(); Getopt::Long::Configure('bundling'); GetOptions ("s=s" => \$opt_shell, "H=s" => \$opt_host, "p=s" => \$opt_process, "e=s" => \$opt_exclusion, "n=i" => \$opt_number, "t=i" => \$opt_t, "h" => \$opt_help ); if (defined $opt_help){ show_help(); exit 0; } if (! defined $opt_shell){ print_usage(); exit 1; } if (! defined $opt_host){ print_usage(); exit 1; } if (! defined $opt_process){ print_usage(); exit 1; } if (! defined $opt_exclusion){ $opt_exclusion=""; } if (! defined $opt_number){ $opt_number=0; } if (! defined $opt_t){ $opt_t = $utils::TIMEOUT ; } $SIG{'ALRM'} = sub { print ("ERROR: timed out waiting for $RMPROC on $opt_host\n"); exit $ERRORS{"WARNING"}; }; if (defined $opt_exclusion){ @exclude = split /\+/, $opt_exclusion; $longueur = @exclude; $grep_v=""; if ($longueur == 1){ $grep_v = "grep -v $exclude[0] |"; } if ($longueur > 1){ for ($i=0;$i<$longueur;$i++){ $grep_v = $grep_v . "grep -v ".$exclude[$i]." |"; } } } if ($opt_number>0){ $RMPROC = `$opt_shell $opt_host ps -ef | grep $opt_process | $grep_v wc -l`; chomp($RMPROC); alarm($opt_t); if ($opt_number==$RMPROC){ print ("OK - $opt_number process for $opt_process\n"); exit $ERRORS{"OK"}; } else { print ("CRITICAL - $RMPROC process, expecting $opt_number for $opt_process\n"); exit $ERRORS{"CRITICAL"}; } } else { $RMPROC = `$opt_shell $opt_host "ps -ef | grep $opt_process | $grep_v wc -l"`; chomp($RMPROC); if ($RMPROC>=1){ print ("OK - process $opt_process is running\n"); exit $ERRORS{"OK"}; } else { print ("CRITICAL - process $opt_process is not running\n"); exit $ERRORS{"CRITICAL"}; } } sub show_help() { print("\n"); print("$PRGNAME - version : $VERSION - published by Guillaume AUCHERE\n"); print("GNU General Public License\n"); print ("\n"); print_usage(); print("Check a process on a remote system over RSH or SSH\n"); print("-s = ssh or rsh (required)\n"); print("-H = remote server name (required)\n"); print ("-p = process to test (required)\n"); print ("-e = exclude one or multiple process separated by \"+\" (optional)\n"); print ("-n = number of process expected (optional)\n\n"); print ("Ex : We're expecting 2 process for sshd except grep and root:\n"); print ("./$PRGNAME -s rsh -H computer1 -p sshd -e grep+root -n 2\n"); print ("\n"); } sub print_usage(){ print ("Usage : $PRGNAME -s -H -p [-e ] [-n ]\n"); print ("\n"); }