#!/usr/bin/perl
use strict;
use Data::Dumper;
use Getopt::Std;

# /usr/local/opmon/libexec/opservices/check_process_snmp.pl -H 127.0.0.1 -p "alerts.php" -c l4g04d0s392p4t0s
sub main {

	my %opts;
	my $snmpwalk = "/usr/bin/snmpwalk";
	my $command_line;
	my $running = 0;

	getopts('H:c:p:',\%opts ) or usage();

	if ( !defined($opts{'H'}) or !defined($opts{'c'}) or !defined($opts{'p'}) ) {
		usage();
	}

	$command_line = sprintf("%s -v2c -c '%s' -t2 -On '%s' .1.3.6.1.2.1.25.4.2.1.2", $snmpwalk, $opts{'c'}, $opts{'H'});
	foreach(`$command_line`) {
		if (m/^.+\Q$opts{'p'}\E.*$/) {
			$running += 1;
		}
	}

	print "$running running process found with name '".$opts{'p'}."' |running_processes=$running;;;;\n";

	if ($running > 0) {
		exit(0);
	}
	exit(2);

}

sub usage {
	print "check_process_snmp.pl checks through snmp if a given process(-p) is running\n";
	print "\tUsage:\n";
	print "\tcheck_process_snmp.pl -H <host address> -p <process name> -c <community>\n";
	exit 3;
}


main()
