# Svcwatch is meant to be used with PerlSvc to create the actual # service. #%status = ( # 0 => 'unknown', # 1 => 'stopped', # 2 => 'starting', # 3 => 'stopping', # 4 => 'running', # 5 => 'resuming', # 6 => 'pausing', # 7 => 'paused', #); package PerlSvc; use Win32::Service; use Win32::TieRegistry(Delimiter=>"/"); use DBI; use DBD::ODBC; use Win32::EventLog; # the short name by which your service will be known (cannot be 'my') our $Name = "svcwatch"; # the display name. This is the name that the Windows Control Panel # will display (cannot be 'my') our $DisplayName = "Service Watch"; $|=1; our $cn=Win32::NodeName(); #modify this to change the location of the settings $regkey="LMachine/Software/SvcWatch"; # the startup routine is called when the service starts sub Startup { Win32::Service::GetServices("", \%tmp); %svc=reverse %tmp; @reg=split /,/,my $reg=$Registry->{$regkey}->GetValue("services"); $pwd=$Registry->{$regkey}->GetValue("Pwd")||die $!; $srv=$Registry->{$regkey}->GetValue("Srv")||die $!; $uid=$Registry->{$regkey}->GetValue("Uid")||die $!; while (ContinueRun()) { foreach (@reg){ if (Win32::Service::GetStatus("",$_,\%{$_})){ unless(${$_}{'CurrentState'} eq ${$_}{'Previous'}){ ${$_}{'Previous'}=${$_}{'CurrentState'}; $dsn="driver=\{SQL Server\}\;Server=$srv\;UID\=$uid\;PWD\=$pwd\;"; $dbh=DBI->connect("DBI:ODBC:$dsn"); my $sname=$svc{$_}; my $state=${$_}{'CurrentState'}; if ($dbh){ $exec=$dbh->prepare("insert into serviceinfo (server, service, status) values ('$cn', '$sname', $state)"); $exec->execute; $exec->finish;}else{ Win32::EventLog::WriteEventLog ($ENV{Computer}, "$DisplayName", "1", "0", "1", "\n$DBI::errstr\n", "\n\$! $!\n$DBI::errstr\n"); } } }else { unless (${$_} eq "1"){ ${$_}=1; $dsn="driver=\{SQL Server\}\;Server=$srv\;UID\=$uid\;PWD\=$pwd\;"; $dbh=DBI->connect("DBI:ODBC:$dsn"); if ($dbh){ $exec=$dbh->prepare("insert into serviceinfo (server, service, status) values ('$cn', '$_', 0)"); $exec->execute; $exec->finish;}else {#write to eventlog $dbi::errstr Win32::EventLog::WriteEventLog ($ENV{Computer}, "$DisplayName", "1", "0", "1", "\n$DBI::errstr\n", "\n\$! $!\n$DBI::errstr\n"); } } } } sleep(5); } } sub Install { # add your additional install messages or functions here print "The $Name Service has been installed.\n"; print "Start the service with the command: net start $Name\n"; my $reg=$Registry->{$regkey}->GetValue("services")||die $!; print "\nWatching for...\n\t".(join ",",$reg); } sub Remove { # add your additional remove messages or functions here print "The $Name service has been removed.\n"; } sub Help { # add your additional help messages or functions here print "No help available.\n"; }