#!/usr/bin/perl
# wmi_service.pl Name Service
# Nagios script to get Windows Service Status 
# author: Benjamin Jakubowski 
# license: GPL - http://www.fsf.org/licenses/gpl.txt

# Changes and Modification
# ========================
# 26-Dec-2005 -  Benjamin Jakubowski


$STATE_CRITICAL = 2;
$STATE_OK = 0;
$STATE_WARNING = 1;
$STATE_UNKNOWN = -1;
$SCRIPT = "wmi-service.pl";

use Win32::OLE qw(in with);
use Win32::Registry;

$host = "localhost";
$service = @ARGV[0];

if  ( @ARGV[0] eq "" )
{
    print_usage ();
    exit 0;
}
sub print_help ()
{
        print($SCRIPT,': 1.0');
        printf "\n";
        print "Copyright (c) 2005 Benjamin Jakubowski \nPerl WMI Name Service check for Nagios \
";
}

sub print_usage ()
{
	print_help();
	printf "\n\nUsage : $SCRIPT \"Name Service\"\n
\n";

}

$WMI = Win32::OLE->new('WbemScripting.SWbemLocator') ||
die "Cannot access WMI on local machine: ", Win32::OLE->LastError; 

$Services = $WMI->ConnectServer($host) ||
die "Cannot access WMI on remote machine: ", Win32::OLE->LastError; 

$sys_service = $Services->InstancesOf("Win32_Service");

foreach $sys (in($sys_service)) 
{
	$Caption=$sys->{'Caption'};
	if ( $service eq $Caption )
	{
	$AcceptPause=$sys->{'AcceptPause'};
	$AcceptStop=$sys->{'AcceptStop'};
	
	$CheckPoint=$sys->{'CheckPoint'};
	$CreationClassName=$sys->{'CreationClassName'};
	$Description=$sys->{'Description'};
	$DesktopInteract=$sys->{'DesktopInteract'};
	$DisplayName=$sys->{'DisplayName'};
	$ErrorControl=$sys->{'ErrorControl'};
	$ExitCode=$sys->{'ExitCode'};
	$InstallDate=$sys->{'InstallDate'};
	$Name=$sys->{'Name'};
	$PathName=$sys->{'PathName'};
	$ProcessId=$sys->{'ProcessId'};
	$ServiceSpecificExitCode=$sys->{'ServiceSpecificExitCode'};
	$ServiceType=$sys->{'ServiceType'};
	$Started=$sys->{'Started'};
	$StartMode=$sys->{'StartMode'};
	$StartName=$sys->{'StartName'};
	$State=$sys->{'State'};
	$Status=$sys->{'Status'};
	$SystemCreationClassName=$sys->{'SystemCreationClassName'};
	$SystemName=$sys->{'SystemName'};
	$TagId=$sys->{'TagId'};
	$WaitHint=$sys->{'WaitHint'};
	if ( $Started )
	{
		print "OK : process $Caption - PID : ($ProcessId) - méthode de démarrage $StartMode-";
		exit $STATE_OK;
	}
	else
	{
		print "CRITICAL : process $Caption non démarré - méthode de démarrage : $StartMode";
		exit $STATE_CRITICAL;
	}
$service_trouve= 1;
}
}
if  ( ! $service_trouve )
{
	print "UNKNOWN Service inconnu";
	exit $STATE_UNKNOWN;
}

 
 