perlquestion
Anonymous Monk
Hi All,
<p>
I'm running into a little problem. I'm trying to create a script with Perl to tail a log file and send the result vial email.
<p>
Thank you in advance,
<p>
Clarke
<p>
Ultimate Goal...
<br />1. Tail the last 50 lines from a log file on remote machine \\Server\MYlog$\testlog.log
<br />2. Take the tailed data and create a new log file with the date as the file name.
<br />3. Send the log via email.
<br />4. Restart the remote service...(i.e. weblogic)
<p>
--- I'm using TAIL.exe for windows. The server is Win2K
<br /> with active Perl 5.6 loaded.
<br />--- I'm using MIME::Lite module to send email
<br />--- I'm using Win32::Lanman module
<p>
MY Code to Restart Remote service....But I need to first tail the last 50 lines from the log file, create a new log based on that, and send it before I can restart the "weblogic service" on the remote machine.
-----------------------------------------------------------
<readmore><code>
#Example: perl NetRestartService.pl -server SERVER1 service Weblogic -action RESTART
use Getopt::Long;
use MIME::Lite;
use Win32::Lanman;
GetOptions ('server:s', \$server, 'service:s', \$service, 'action:s', \$action);
if ((@ARGV[0] eq "help") || (@ARGV[0] eq "?")){&syntax; exit 1;}
if ($server eq ""){$server = $ENV{COMPUTERNAME};}
if ($action ne ""){$action = "\U$action\E";}
%commands = ('STOP' => 1,'START' => 1,'RESTART' => 1,'AUTOMATIC' => 1,'MANUAL' => 1,'DISABLED' => 1);
if (($action ne "")&&($commands{$action} != 1))
{
print "\n\nInvalid command - $action\n\n";
&syntax;
exit 1;
}
%status = ( 1 => 'Stopped', 2 => 'Start Pending',
3 => 'Stop Pending', 4 => 'Running',
5 => 'Continue Pending', 6 => 'Pause Pending',
7 => 'Paused');
%start = (2 => 'Automatically', 3 => 'Manually', 4 => 'Disabled');
%startup = (AUTOMATIC => '2', MANUAL => '3', DISABLED => '4');
if(!Win32::Lanman::EnumServicesStatus("\\\\$server", "", &SERVICE_WIN32, &SERVICE_STATE_ALL, \@services))
{
print "\n\nCannot read services information on $server\n\n";
&syntax;
exit 1;
}
foreach (@services)
{
$state{${$_}{'name'}} = ${$_}{'state'};
if ("\U${$_}{'name'}\E" eq "\U$service\E")
{
$service = ${$_}{'name'};
$sn = 1;
last;
}
elsif (${$_}{'display'} =~ /$service/i)
{
@mightbe = (@mightbe, "${$_}{'name'}");
}
}
if (($sn != 1)&&(@mightbe < 1))
{
foreach (@services){print "${$_}{'name'} = ${$_}{'display'}\n";}
print "\nUnable to locate $service service\n";
exit 1;
}
if (($sn == 1)&&($action eq ""))
{
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$service", \%config))
{
print "Can't query config of $service on $server " . Win32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$service})} and set to start ${start{$config{'start'}}}\n";
exit 1;
}
if (($sn == 1)&&($commands{$action} == 1))
{
&$action;
exit 1;
}
if ((@mightbe == 1)&&($action eq ""))
{
$service = $mightbe[0];
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$service", \%config))
{
print "Can't query config of $service on $server " . Win32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$service})} and set to start ${start{$config{'start'}}}\n";
exit 1;
}
if ((@mightbe == 1)&&($commands{$action} == 1))
{
$service = $mightbe[0];
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$service", \%config))
{
print "Can't query config of $service on $server " . Win32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$service})} and set to start ${start{$config{'start'}}}\n";
if ("\U${start{$config{'start'}}}\E" eq $action)
{
print "Startup is already ${start{$config{'start'}}} - No change needed\n";
exit 1;
}
&$action;
exit 1;
}
if (@mightbe > 1)
{
print "\n\n\n";
$mbcount = 1;
foreach (@mightbe)
{
print "$mbcount - $mightbe[$mbcount-1] -";
foreach (@services)
{
if (${$_}{'name'} eq $mightbe[$mbcount-1])
{
print " ${$_}{'display'} ($status{$state{${$_}{'name'}}})\n";
}
}
$mbcount++;
}
until (($trash > 0)&&($trash <= @mightbe))
{
print "\n\nPlease select the service (Ctrl/C to abort)\n\n";
$trash = <STDIN>;
chomp $trash;
}
$trash = $trash -1;
$service = $mightbe[$trash];
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$service", \%config))
{
print "Can't query config of $service on $server " . Win32::Lanman::GetLastError() . "\n";
}
if ($action eq "")
{
print "$service ($config{'display'}) is $status{($state{$service})} and set to start ${start{$config{'start'}}}\n";
exit 1;
}
else
{
if ("\U${start{$config{'start'}}}\E" eq $action)
{
print "Startup is already ${start{$config{'start'}}} - No change needed\n";
exit 1;
}
&$action;
exit 1;
}
}
sub syntax
{
print "\n\nSYNTAX:\n\nperl sc.pl -server [servername] -service [servicename] -action [action]";
print "\n\n\nEXAMPLES:\n\nperl sc.pl -server server1 -service spooler -action restart";
print "\n\n\tStops the starts the Spooler service on Server1";
print "\n\nperl sc.pl -server server1 -service spooler ";
print "\n\n\tDisplays the current status of the Spooler service on Server1";
print "\n\n\tPossible actions - stop, start, restart, automatic, manual, disabled\n\n";
}
sub STOP
{
if ($state{$service} != 4)
{
if ($action eq "RESTART"){return();}
print "\n\n$service currently state is $status{($state{$service})} - Cannot stop\n\n";
exit 0;
}
if(!Win32::Lanman::StopService("\\\\$server", '', "$service", \%H1))
{
&cstate;
print "\n\nError stopping $service service on $server\n\nCurrent state is $status{$state}\n";
exit 0;
}
sleep(2);
&cstate;
$count = 0;
until (($state == 1)||($count == 20))
{
sleep(1);
&cstate;
$count++;
}
if (($state != 1)&&($count == 20))
{
print "\n\nError stopping $service service on $server\n\nCurrent state is $status{$state}\n";
exit 1;
}
print "$service service on $server is $status{$state}\n";
}
sub START
{
if(!Win32::Lanman::StartService("\\\\$server", '', "$service"))
{
print "\n\nError starting $service service on $server\n\n";
exit 0;
}
sleep(2);
&cstate;
$count = 0;
until (($state == 4)||($count == 20))
{
sleep(1);
&cstate;
$count++;
}
if (($state != 4)&&($count == 20))
{
print "\n\nError starting $service service on $server\n\nCurrent state is $status{$state}\n";
exit 1;
}
print "$service service on $server is $status{$state}\n";
}
# SENDING EMAIL ---------------------------
sub send_email () {
my $msg;
MIME::Lite->send('smtp', "192.168.2.3", Timeout=>60);
$msg = MIME::Lite->new(
From =>'NetRestartService',
To =>'Test@test.com',
Cc =>'Test2@test2.com',
Subject =>'Perl Script to restart reomote NT Service - NetRestartService.pl',
Data =>'This email was generated after an error was detected, the NetRestartService.pl script was executed.',
);
$msg->send;
}
# END SENDING EMAIL ---------------------------
sub RESTART
{
&STOP;
&START;
}
sub AUTOMATIC
{
&CHANGE;
}
sub MANUAL
{
&CHANGE;
}
sub DISABLED
{
&CHANGE;
}
sub CHANGE
{
print "Modifying startup parameter of $service on $server\n";
if (!Win32::Lanman::ChangeServiceConfig("$server", "" ,"$service", {start => "$startup{$action}"}))
{
print "Unable to update the $service service configuration on $server\n";
return;
}
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$service", \%config))
{
print "Can't query config of $service on $server " . Win32::Lanman::GetLastError() . "\n";
}
print "$service is $status{($state{$service})} and set to start ${start{$config{'start'}}}\n";
exit 1;
}
sub cstate
{
if(!Win32::Lanman::EnumServicesStatus("\\\\$server", "", &SERVICE_WIN32, &SERVICE_STATE_ALL, \@services))
{
print "\n\nCannot read services information on $server\n\n";
exit 0;
}
foreach (@services)
{
if (${$_}{'name'} eq $service)
{
$state = ${$_}{'state'};
}
}
}
</code>
<p><small>Edit by [tye]</small></p>
0