use Win32::Service;
use Win32::Message;
use HTTP::Request::Common qw(POST);
use URI::URL;
use LWP::UserAgent;
$host = "\\\\servername";
$date = localtime;
$message_type = "pcs";
$recipient = Win32::NodeName();
$sender = Win32::LoginName();
$server = "";
open(FILE, "+>status.txt") || die "Can't clobber file: ($!)\n";
print FILE "============== $date ==============\n";
@exchange =
(
"Microsoft Exchange System Attendant",
"Microsoft Exchange Directory",
"Microsoft Exchange Event Service",
"Microsoft Exchange Information Store",
"Microsoft Exchange Internet Mail Service",
"Microsoft Exchange Message Transfer Agent",
"IIS Admin Service",
"World Wide Web Publishing Service",
);
%state =
(
0 => "Unknown",
1 => "Stopped",
2 => "Starting",
3 => "Stopping",
4 => "Running",
5 => "Resuming",
6 => "Pausing",
7 => "Paused",
);
exchange();
sub exchange
{
print FILE "======== Exchange Service status on $host ========\
+n\n";
foreach $myservice (@exchange)
{
getstatus($myservice);
}
}
sub getstatus
{
if (Win32::Service::GetServices($host, \%services))
{
if ($services{$myservice})
{
Win32::Service::GetStatus($host, $services{$myservice}, \%
+status);
if ($state{$status{CurrentState}} eq "Running")
{
write FILE;
}
elsif ($state{$status{CurrentState}} eq "Stopped")
{
write FILE;
print FILE "\nWARNING : $myservice is currently Stopped.\n";
print FILE "Attempting to restart the stopped service.... ";
send_alert();
start_service();
}
elsif ($state{$status{CurrentState}} eq "Stopping")
{
print FILE "\nWARNING : $myservice is attempting to Stop.\n";
send_alert();
}
elsif ($state{$status{CurrentState}} eq "Starting")
{
print FILE "\nWARNING : $myservice is attempting to Start.\n";
send_alert();
}
elsif ($state{$status{CurrentState}} eq "Pausing")
{
print FILE "\nWARNING : $myservice is attempting to Pause.\n";
send_alert();
}
elsif ($state{$status{CurrentState}} eq "Paused")
{
print FILE "\nWARNING : $myservice is currently Paused.\n";
send_alert();
resume_service();
}
elsif ($state{$status{CurrentState}} eq "Resuming")
{
print FILE "\nWARNING : $myservice is Resuming.\n";
send_alert();
}
else
{
print FILE "\nWARNING : $myservice is in an Uknown status.\n";
send_alert();
}
}
else
{
print FILE "WARNING : $myservice doesn't exist!\n";
}
}
else
{
print FILE "Error connecting to $host: ";
print FILE Win32::FormatMessage(Win32::GetLastError() ), "\n";
send_alert();
}
}
print FILE "\n========================================================
+\n";
close(FILE);
sub start_service
{
if (Win32::Service::StartService($host, $services{$myservice}) )
{
print FILE "Success!\n";
$stat = "Success";
$attempt = "start";
send_status($stat, $attempt);
}
else
{
print FILE "Failed.\n";
print FILE Win32::FormatMessage(Win32::GetLastError() ), "\n";
$stat = "Failed";
$attempt = "start";
send_status($stat, $attempt);
}
}
sub resume_service
{
print FILE "Attempting to resume the $myservice service: ";
if (Win32::Service::ResumeService($host, $services{$myservice}) )
{
print FILE "Success!\n";
$stat = "Success";
$attempt = "resume";
send_status($stat, $resume);
}
else
{
print FILE "Failed!\n";
print FILE Win32::FormatMessage(Win32::GetLastError() ), "\n";
print FILE "Check Event Log for further details.\n";
$stat = "Failed";
$attempt = "resume";
send_status($stat, $resume);
}
}
sub send_alert
{
if ($message_type eq "net")
{
$error = $state{$status{CurrentState}}, "\n";
$message = "\t\t *** FAILURE STATUS REPORT ***\n\n"
. "The MS Exchange Service Tool found a problem when monit
+oring $host.\n"
. "The current state for $myservice is: " . $error . "\n\n
+"
. "An attempt will be made to restart the $myservice."
. "\n\n\t\t *****************************\n";
if (Win32::Message::Send($server, $recipient, $sender, $me
+ssage))
{
print FILE "Sent administrative failure message to $recipi
+ent.\n";
}
else
{
print FILE "The administrative message to $recipient faile
+d.\n";
print FILE Win32::FormatMessage(Win32::GetLastError() ), "
+\n";
}
}
elsif ($message_type eq "pcs")
{
$error = $state{$status{CurrentState}}, "\n";
$message = "**** FAILURE STATUS REPORT ****\n $myservice is $e
+rror\n. Attempting to restart\n";
send_sms($message);
}
}
sub send_status
{
if ($message_type eq "net")
{
$message = "\t\t *** SERVICE STATUS REPORT ***\n\n"
. "Attempting to $attempt service: $myservice\n"
. "Result: $stat!"
. "\n\n\t\t ****************************\n";
if (Win32::Message::Send($server, $recipient, $sender, $me
+ssage))
{
print FILE "Sent administrative status message to $recipie
+nt.\n";
}
else
{
print FILE "The administrative status message to $recipien
+t failed.\n";
print FILE Win32::FormatMessage(Win32::GetLastError() ), "
+\n";
}
}
elsif ($message_type eq "pcs")
{
$message = "**** SERVICE STATUS REPORT ****\n Attempt to $atte
+mpt service $myservice: $stat!";
send_sms($message);
}
}
sub send_sms
{
%form =
(
AREA_CODE => "555",
PIN1 => "555",
PIN2 => "5555",
SENDER => "5555555555",
"emapnew--DESC--which" => "ORIG",
PAGETEXT1 => "$message",
);
$ua = LWP::UserAgent->new();
$req = url("http://sabre.cantelatt.com/cgi-bin/sendpcs.cgi");
$req->query_form(%form);
$response = "";
$response = $ua->request(HTTP::Request->new('GET', $req));
$response = $response->{_content};
if ($response =~ /^\<html\>\<head\>\<title\>Thank You\<\/title\>\<
+\/head\>/)
{
print FILE "SMS Administrative message Succeeded\n";
}
else
{
print FILE "SMS Administrative message Failed\n";
}
}
format FILE =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
$myservice, $state{$status{CurrentState}}
.
-
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.