Sorry for the confusion with the code above...I didn't put everything on the forum correctly. It was missing lots of code. I'm faily new at PERL and I'm learning as I go, putting bits and pieces of code together that I've obtained on the internet.
I don't know how to put everything together. This is the first code section I want to use....The code will use
"TAIL.exe" placed in the WINNT\SYSTEM32 directory.
How do I get the the "TAILED" result from the "LOG1.LOG" and create a new log with that informaton. Once this is done, I need to send an email with the newly created log file. Once the email is sent the weblogic service needs to be restarted. (I want to run the scipt remotely).
- Can I specify remote machines to run this script, for example "\\Server1\Logs\testlog.log" and remote service to restart "weblogic".
----First code section is to run TAIL.exe and get the last 50 lines from the from the log it's tailing. Once this is done, I want to add another section to take that result and create another log file (with date if possible) and send it via email to let the users know that there was an error and the "Weblogic Service" will be restarted.
#!/usr/bin/perl -w
use Getopt::Long;
use MIME::Lite;
use Win32::Lanman;
use strict;
my $tail = 'C:\Winnt\System32\Tail.exe -50';
my %logs =
(
'C:\\LOG\\\LOG1.LOG,
);
----Third section is restarting the remote service....I don't know how to create a log fie with the tailed data and send that (second code section....) - The perl script will
be run using a batch file becase the remote restart needs
certain parameter..
C:\PERL\BIN\PERL NetRestartService.pl -Server TESTSERVER -Service Weblogic -action Restart
---BEGIN OR THIRD SECTION-----
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_WIN3
+2, &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", '', "$ser
+vice", \%config))
{
print "Can't query config of $service on $server " . W
+in32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$servi
+ce})} 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", '', "$ser
+vice", \%config))
{
print "Can't query config of $service on $server " . W
+in32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$servi
+ce})} and set to start ${start{$config{'start'}}}\n";
exit 1;
}
if ((@mightbe == 1)&&($commands{$action} == 1))
{
$service = $mightbe[0];
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$ser
+vice", \%config))
{
print "Can't query config of $service on $server " . W
+in32::Lanman::GetLastError() . "\n";
}
print "$service ($config{'display'}) is $status{($state{$servi
+ce})} 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{$sta
+te{${$_}{'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", '', "$ser
+vice", \%config))
{
print "Can't query config of $service on $server " . W
+in32::Lanman::GetLastError() . "\n";
}
if ($action eq "")
{
print "$service ($config{'display'}) is $status{($stat
+e{$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{'sta
+rt'}}} - No change needed\n";
exit 1;
}
&$action;
exit 1;
}
}
sub syntax
{
print "\n\nSYNTAX:\n\nperl sc.pl -server [servername] -servic
+e [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 servic
+e on Server1";
print "\n\n\tPossible actions - stop, start, restart, automati
+c, 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";
}
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", "" ,"$servi
+ce", {start => "$startup{$action}"}))
{
print "Unable to update the $service service configura
+tion on $server\n";
return;
}
if(!Win32::Lanman::QueryServiceConfig("\\\\$server", '', "$ser
+vice", \%config))
{
print "Can't query config of $service on $server " . W
+in32::Lanman::GetLastError() . "\n";
}
print "$service is $status{($state{$service})} and set to star
+t ${start{$config{'start'}}}\n";
exit 1;
}
sub cstate
{
if(!Win32::Lanman::EnumServicesStatus("\\\\$server", "", &SERV
+ICE_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'};
}
}
}
----- END THIRD SECTION----------------------------------
Edit by tye