# 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"; }

In reply to Service monitor with Perl and PerlSvc by perlsage

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.