use Win32::Service qw'GetServices GetStatus'; use strict; use POSIX; use autodie; my ($hostname, $servicename, $entry); my (%status, @array); my @state_name = qw( '' Stopped START_PENDING STOP_PENDING Started CONTINUE_PENDING PAUSE_PENDING PAUSED ERROR ); use constant DATETIME => strftime("%Y%m%d%H%M%S", localtime); # my $filename = '/Perl/XiServiceList.txt'; sub buildarray() #array subroutine { # text file read and loaded on startup #open (MYFILE, '>>XiPerlService.txt'); #open for write, overwrite #print MYFILE $theTime, " Start of perl module\n"; open( FILE, "< $filename" ) or die "Can't open $filename : $!"; while( ) { my $line = $_; chomp $line; push @array, ($line); } close FILE; # my $loop = 1; while ($loop lt 4) #infinite loop if setting this up to run continually i.e. service { #part of - infinite loop my $count = 0; my $entry; foreach $entry (@array) #loops through entire loaded array { open (MYFILE, ">> /Perl/daily\.". DATETIME."\.txt"); #open for write, overwrite if (($count == 0)) #test 1st entry for host address { $hostname = "$entry"; $count = $count + 1; } else { $servicename = "$entry"; sleep(2); &CheckServiceStatus($hostname,$servicename, \%status); $count = $count + 1; sleep(2); } close MYFILE; } #sleep(300); #part of - infinite loop $loop = $loop + 1; } #part of - infinite loop } # sub CheckServiceStatus() # check service sub routine { GetStatus($hostname, $servicename, \%status); my $state_no = $status{'CurrentState'}; printf MYFILE " ServiceStatus: %s %s %s \n", $hostname, $servicename, $state_name[$state_no]; } sub main() { buildarray(); #run array subroutine } main();