#! /usr/bin/perl -wT

# -----------------------------------------------------------------------------
# File Name:		check_cmds.pl
#
# Author:		Emmett Hogan
#
# Date:			2004/11/12
#
# Description:		This script will check the list of processes on a machine
#			for ANY OCCURANCE OF THE ARGs.  If any one of the ARGS does
#			not appear in the process table, it is a CRITICAL error
#			Either everything is there or it's not, so there is
# 			no WARNING level.
#
# -----------------------------------------------------------------------------

require 5.004;

# -------------------------------------------------------------------[ Uses ]--

use Socket;
use strict;
use Getopt::Long;
use vars qw($help @srch_cmds @srch_args $verbose);
use vars qw($PROGNAME);
use lib "/opt/nagios-1.2/libexec" ;
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);

# ----------------------------------------------------[ Function Prototypes ]--

sub print_help ();
sub print_usage ();

# -------------------------------------------------------------[ Enviroment ]--

$ENV{PATH} = "";
$ENV{ENV} = "";
$ENV{BASH_ENV} = "";

# -----------------------------------------------------------------[ Global ]--

$PROGNAME = "check_mult_procs";
	
my $ps_cmd = "/bin/ps";
my $ps_args = "-A -o args";
my $SEP = "\x07"; # Use a bell as separator for our "ugly strings" below.
my $state;
my $answer;

# ------------------------------------------------------------[ print_usage ]--

sub print_usage () {
	print "Usage: $PROGNAME [-v] [-timeout #] string1 [string2] ....\n";
}

# -------------------------------------------------------------[ print_help ]--

sub print_help ()
{
	print_revision($PROGNAME,'$Revision: 1.0 $ ');
	print "Perl Check Multiple Process Check plugin for Nagios\n\n";
	print_usage();
	print "
-v, --verbose
   Print extra debugging information

-t[imeout] ##
   How long to wait for an answer. (Default: 15sec)

string1 string2 ...
   strings to search for in the process table (commands AND arguments)
";
}

# -------------------------------------------------------------[ bindRemote ]--

# ===================================================================[ MAIN ]==

MAIN:
{
	my $hostname;
	my @not_found;

#	Getopt::Long::Configure('bundling');
	GetOptions (
		"help"       => \$help,
		"verbose"    => \$verbose,
		"timeout=i"=> \$TIMEOUT
		);

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

	# Just in case of problems, let's not hang Nagios
	$SIG{'ALRM'} = sub {
		print "Somthing is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
		exit $ERRORS{"UNKNOWN"};
	};
	
	alarm($TIMEOUT);

	chomp($hostname = `/bin/hostname`);
	$hostname = $1 if ($hostname =~ /([-.a-zA-Z0-9]+)/);
	
	print "MAIN(debug): hostname = $hostname\n" if $verbose;
	print "MAIN(debug): Checking process table...\n" if $verbose;

	my @commands;
	if (! -x "$ps_cmd") {
		print "UNKNOWN - Cannot run $ps_cmd on $hostname\n";
		exit $ERRORS{"UNKNOWN"};
	}
	open(PS, "$ps_cmd $ps_args|") ||  exit $ERRORS{"UNKNOWN"}; 
	while (<PS>) {
		chomp;
		next if (/$0/); # Don't want to include this script in the list
		next if (/COMMAND/); # Don't include the header;
		push(@commands, $_);
	}
	# Drop a couple of lines (like the header and the one for THIS command!)
	my $key; my $args;
	my $ugly_string_of_running_cmds = join("$SEP",@commands);

	foreach $key ( @ARGV ) {
		push(@not_found, "$key") if ( "${SEP}${ugly_string_of_running_cmds}${SEP}" !~ /${key}/ );
	}

        if ($#not_found >= 0) {
   
                $state = "CRITICAL"; 
                $answer = "CRITICAL - ($hostname) Not Running: " . join(", ", @not_found) . ".\n";

        } else {
                $state = "OK";
                $answer = "CMDS OK - ($hostname) Checked: " . join(", ", @ARGV) . "\n";
        }

        print $answer;
        exit $ERRORS{$state};

}

