#!/usr/bin/perl
# Author : jakubowski Benjamin
# Date : 19/12/2005
# check_win_snmp_process.pl IP COMMUNITY service


sub print_usage 
{
print "
############################## check_win_snmp_process.pl ############## \
# Version : 1.0 \
# Date : Dec 20 2005  \
# Author  : Benjamin Jakubowski \
# Help : http://www.xenux.net \
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt \
#######################################################################\n";
	print "check_win_snmp_process.pl IP COMMUNITY service\n";
}
$PROGNAME = "check_win_snmp_process.pl";
if  ( @ARGV[0] eq "" || @ARGV[1] eq "" || @ARGV[2] eq "" )
{
    print_usage();
    exit 0;
}


$STATE_CRITICAL = 2;

$STATE_OK = 0;

$IP=@ARGV[0];
$COMMUNITY=@ARGV[1];
$Service=@ARGV[2];
$resultat =`snmpwalk -v 1 -c $COMMUNITY $IP  hrSWRunName | grep $Service`;
if ( $resultat )
{
	@sid=split(/\n/,$resultat);
	$nb_service = $#sid + 1;
	# Un seul process du nom $Service
	if ( $nb_service eq 1 )
	{
		$sid=$resultat;
		$sid=~ s/HOST-RESOURCES-MIB::hrSWRunName.//g;
		@information_sid=split (/ /,$sid);
		$sid=@information_sid[0];
		$memory = `snmpwalk -v 1 -c $COMMUNITY $IP hrSWRunPerfMem | grep hrSWRunPerfMem.$sid`;
		$memory=~ s/HOST-RESOURCES-MIB::hrSWRunPerfMem.$sid = INTEGER: //g;
		print "OK : Service $Service PID( $sid ) Running Memory used $memory\n";
		exit $STATE_OK;
	}
	# Il y a plusieurs process du meme nom $Service
	else
	{
		$i=1;
		# definition d'une variable qui contiendra la sommes de la memoire utilisée de tous les process $Service
		$total_memory_used=0;
		# Si la memoire est exprimer en autre chose qu'en KBytes, cette somme sera eroné donc affichage des resultats
		$char_memory="";	
		$liste_pid ="";
		# On boucle sur tous les process
		while ( @sid[$i] )
		{
			$num_sid = @sid[$i];
			$num_sid=~ s/HOST-RESOURCES-MIB::hrSWRunName.//g;
			@information_sid=split (/ /,$num_sid);
			$num_sid=@information_sid[0];
			$liste_pid .= "$num_sid ";

			$memory = `snmpwalk -v 1 -c $COMMUNITY $IP hrSWRunPerfMem | grep hrSWRunPerfMem.$num_sid`;
			$memory=~ s/HOST-RESOURCES-MIB::hrSWRunPerfMem.$num_sid = INTEGER: //g;
			$memory=~ s/\n//g;
			@information_memroy =split(/ /,$memory);
			if (  $information_memroy[1] eq "KBytes")
			{
				$total_memory_used+=$information_memroy[0];
			}
			else
			{
				$pb_calcul=1;
				$char_memory.=$memory;
			}
			$i++;
		} 
		if ( $pb_calcul )
		{
			print "OK : Service $Service PID's( $liste_pid) Running impossible to ckeck memory : $char_memory\n";
                        exit $STATE_OK;
		}
		else
		{
			print "OK : Service $Service PID's( $liste_pid) Running Memory used $total_memory_used KBytes\n";
			exit $STATE_OK;
		}
	}
	print "OK : Service $Service Running Memory used $memory\n";
        exit $STATE_OK;
}
esle
{
	print "Critical  : Service $Service not found\n";
        exit $STATE_CRTICAL;
}




