# 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\=$ui
+d\;PWD\=$pwd\;";
$dbh=DBI->connect("DBI:ODBC:$dsn");
my $sname=$svc{$_};
my $state=${$_}{'CurrentState'};
if ($dbh){
$exec=$dbh->prepare("insert into serviceinfo (server, service, sta
+tus) 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}, "$Disp
+layName", "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";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.