I have been trying to write a Perl script that would check the service status of Remote Servers. I am using the Win32::Service module to accomplish my goal.
I find that the for some services value returned by Win32::Service::GetStatus CurrentStatus is not exactly the same as observed from services.msc applet.
Here is the piece of script I am using along with both outputs from script and servics.msc.
use Data::Dumper;
use Win32;
use Win32::Service;
use strict;
use warnings;
my %statcodeHash = ( '1' => 'stopped.',
'2' => 'start pending.',
'3' => 'stop pending.',
'4' => 'running.',
'5' => 'continue pending.',
'6' => 'pause pending.',
'7' => 'paused.' );
my @serviceNames = qw(NNMAction RpcEptMapper smstsmgr SNMPTRAP);
foreach my $serv (@serviceNames)
{ my %status;
my $ret = Win32::Service::GetStatus('nnmi.hclt.corp.hcl.in', $serv, \%status);
if ($ret)
{ print "success\t$statcodeHash{$status{CurrentState}}\t$serv\n";
}
else
{ print Win32::FormatMessage(Win32::GetLastError()), "\n";
}
}
OUTPUT from Script
D:\AVI MEHENWAL\PERL\SCRIPTS\PROJECTS\Serve Management>perl -w perl_RemoteServiceStatus.pl
success stopped. NNMAction
success running. RpcEptMapper
success stopped. smstsmgr
success stopped. SNMPTRAP
OUTPUT from Services.msc
Name Descrition Status StartupType LogOnAs
NNMAction bla bla bla Started Manual LocalSystem
Can anyone suggest me what approach should I follow to achieve my goal, Is my method wrong or there is something I am missing from Perl module point of view?
Updated
I noticed that
nnmi.hclt.corp.hcl.inseems to be a domain name rather than a Windows style machine name. First, find out the IP address corresponding tonnmi.hclt.corp.hcl.in. Let’s say it isx.x.x.x.Now, run
nbtstat -A x.x.x.x. That should tell you the name of the machine. Let’s say it isNNMI. Then, you should specify that in themy $remoteline below, replacingMYREMOTEwithNNMI, and try again.I am assuming you have the correct privileges etc. If you get an authorization related error, I am afraid I cannot help you, but you can ask on ServerFault to find out what you need to do so as to be able to query services on a remote machine in the domain.
For reference, the
SERVICE_STATUSstructure is documented here.