Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi All,

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.

Thank you in advance,

Clarke

Ultimate Goal...
1. Tail the last 50 lines from a log file on remote machine \\Server\MYlog$\testlog.log
2. Take the tailed data and create a new log file with the date as the file name.
3. Send the log via email.
4. Restart the remote service...(i.e. weblogic)

--- I'm using TAIL.exe for windows. The server is Win2K
with active Perl 5.6 loaded.
--- I'm using MIME::Lite module to send email
--- I'm using Win32::Lanman module

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. -----------------------------------------------------------

#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_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"; } # 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", "" ,"$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'}; } } }

Edit by tye


In reply to Help e-mailing last 50 lines of log file by Anonymous Monk

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found