Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

monitorlogs.pl

by vroom (His Eminence)
on Feb 08, 2002 at 15:38 UTC ( [id://144117]=sourcecode: print w/replies, xml ) Need Help??
Category: Utitilty Scripts
Author/Contact Info Tim Vroom-
Description: Script which can be run intermittently against a log file. It keeps track of the length at the time of the last run and e-mails you anything that has been added since the last time the script has run. One caveat-log rotation would muck up this scheme. Best used for low activity logfiles.
use Net::SMTP;
my $lengthfile="\var\logs\www\myapp.log.length";
my $logfile="\var\logs\www\myapp.log";
my $applicationname="myapplication";
my $smtpserver="mysmtp";
my $from=q{serveradmin@blah.net};
my $to=q{serveradmin@blah.net};

open FILE, $lengthfile || die "Couldn't open lengthfile";
my $length=<FILE>;
close FILE;
my $size= -s $logfile;
if($size>$length){
   open LOG, $logfile || die "Couldn't open logfile";
   seek (LOG, $length+1,0);
   my $smtp=Net::SMTP->new($smtpserver,Timeout=>30);
   $smtp->mail($from);
   $smtp->to($to);
   $smtp->data();
   $smtp->datasend("From: $from\n");
   $smtp->datasend("Subject: $applicationname Error\n");
   $smtp->datasend("\n");

   while(<LOG>){
      $smtp->datasend($_); 
   }
   $smtp->dataend();
   $smtp->quit();
open FILE, ">$lengthfile" || die "Couldn't open lengthfile for writing
+";
print FILE $size;
close FILE;
}
Replies are listed 'Best First'.
Re: monitorlogs.pl
by BigJoe (Curate) on Feb 08, 2002 at 17:12 UTC
    to make it work with rotating logs add an elsif at the end and do this:
    } elsif ($size < $length) { my $rolled_log = "$logfile.1"; if(-e $rolled_log){ open(LOG, $rolled_log) || die "couldn't open file"; seek (LOG, $length+1,0); my $data_to_send = ""; while(<LOG>){ $data_to_send .= $_; } close LOG; open LOG, $logfile || die "Couldn't open logfile"; seek (LOG, $length+1,0); while(<LOG>){ $data_to_send .= $_; } my $smtp=Net::SMTP->new($smtpserver,Timeout=>30); $smtp->mail($from); $smtp->to($to); $smtp->data(); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $applicationname Error\n"); $smtp->datasend("\n$data_to_send"); $smtp->dataend(); $smtp->quit(); open FILE, ">$lengthfile" || die "Couldn't open lengthfile for w +riting"; print FILE $size; close FILE; }


    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.
Re: monitorlogs.pl
by grinder (Bishop) on Feb 09, 2002 at 21:34 UTC

    Bzzzt! What's with the

      open FILE, $lengthfile || die "Couldn't open lengthfile";

    If you're running this script against two or more logfiles, which one blew up? Why did it blow up? We all know that should be written as

      open FILE, $lengthfile || die "Couldn't open lengthfile $lengthfile for input: $!\n";

    A die expression is a terrible thing to waste.

    But that's still no good. If the open fails, nothing is going to be printed out in any event, because precedence rules imply that the statement should be parsed as:

      open( FILE, ($lengthfile || die) );

    Which means unless the scalar $lengthfile is undefined false (thanks Juerd!), the die is never going to be executed. And then the error message would be really puzzling. So it should be written as:

      open FILE, $lengthfile or die "Couldn't open lengthfile $lengthfile for input: $!\n";

    There's also a bit of an indentation flaw at the end of the code. At first I thought you were systematically rewriting the lengthfile, until I noticed that the code was actually within the if block.

    g r i n d e r
    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';

      Which means unless the scalar $lengthfile is undefined,

      s/undefined/false/

      False are: undef, 0, "0", ""

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: monitorlogs.pl
by belg4mit (Prior) on Feb 08, 2002 at 20:31 UTC
    Cool a perl version of this (It doesn't do mail though, no idea I found it as I can't find it in most search engines. Some other sources, apparently it comes with debian?) This could be so handy, I wish I'd remember to use it more.

    UPDATE</b: Also note it uses the inode and not the size to track changes, the author believes it to be more reliable.

    --
    perl -pe "s/\b;([st])/'\1/mg"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://144117]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2024-04-23 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found