Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: reading of log files
by DrZaius (Monk) on May 07, 2001 at 19:01 UTC
    Well, if it is unix, you don't have to worry about it if you are running it with cron.

    The cron daemon generally runs you script and emails any output to you.

    First, you need to tell crond about your script. You'd do something like this in /etc/cron.d/backup (you don't have to call it backup -- that is just the filename I chose):

    MAILTO="whereever you want to email any output to" # the format goes minute hour day month day_of_week # this will run the backup script as root ever day at # 4:23 in the morning 23 4 * * * root /path/to/backup/script.pl
    Now, in your script, print any errors to STDERR, and die if you have to. This will be mailed to you.

    cheers.

      If you'd rather avoid depending on an outside tool for more than scheduling (I wouldn't reccommend writing a scheduler, most OSes have one), You could look into Mail::Sender for sending out emails. Benefits:

      -Doesn't require a particular scheduler/ OS, or mail program
      -You can specify multiple email addresses, or specify a different address depending on the messages that need sending

      Or this may just be overkill.
Re: reading of log files
by mr.nick (Chaplain) on May 07, 2001 at 20:52 UTC
    To really help, we'd need an example of your typical logfile output, and THEN an example of one that failed. With those pieces, one could write the script you are looking for. Without them, it'll all be generalities and guesses.
Re: reading of log files
by thpfft (Chaplain) on May 08, 2001 at 16:07 UTC

    Sounds to me like you need logcheck. It's a shell script, usually set up by the admin as an hourly cron job, but also workable in your home space if you have permission to access the logs. it might save you some work.

    Failing that, here's the bit that will read the file and record anything bad it finds:

    my $logfile = '/full/path/to/logfile'; my $badnews; open (LF, "<$logfile") or die ("open $logfile failed: $!); flock (LF,1) or die ("flock $logfile failed: $!"); while (<LF>) { $badnews .= $_ if (m/bad news/i); # some work needed } close(LF) or die ("close $logfile failed: $!); if ($badnews) { # do something }

    Which just leaves you to write the regex that will spot bad news in the file, and a sub to send you $badnews in an email message if it finds any.

    You should also replace the or die() instructions with something that will send you another sort of message if reading the logfile fails.

    Bear in mind that there are issues with constantly incremented files: when perl opens the file it will remember the length of it and read only that far unless you resest the EOF flag. It probably doesn't matter, but the cookbook has more information if you need it.

    Hope this helps.

Re: reading of log files
by codechuck (Initiate) on May 07, 2001 at 19:13 UTC
    Maybe perl has some module to handle this but I use "Blat" for NT and "MAIL" for Unix. These 2 subs should help. You need to open your logfile and check for an error string. If you catch it you will get an e-mail.
    sub Notify { local $Message = $_[0]; &MailMessage(${MailList},"***${JobName}: ${Host}: ${Message} ${LogFil +e}",${LogFile},${Host} ); }; sub MailMessage { # This file will send mail to the recipient with the given subject. # If the body is supplied, it will also be sent. If a sender is # supplied, the message will be marked with that sender in the "From" # field. local $Recipient = $_[0]; local $Subject = $_[1]; local $Body = $_[2]; local $Sender = $_[3]; $Recipient =~ s/\n+//; $Subject =~ s/\n+//; $Sender =~ s/\n+//; if (!defined $MailFile) { if ($OSFlag ne "Y") { &DefineOS; }; $MailFile = "${Temp}/mis_mail.$$"; }; if (-r $Body) { open (MAILFILE, "> $MailFile"); open (BODY, "$Body"); while (<BODY>) { print MAILFILE "$_"; }; close (BODY); close (MAILFILE); } else { open (MAILFILE, "> $MailFile"); print MAILFILE "$Body"; close (MAILFILE); }; if ($OS eq "MSWin32") { $Mail = qq! $Mail $MailFile -t "$Recipient" -s "$Subject" ! . qq! -i "$Sender" -f "$Sender" !; print "using blat...\n"; open (MAIL, "| $Mail"); while (<MAIL>) { print $_; }; close (MAIL); print "closing blat...\n"; } else { open (MAILFILE, "$MailFile"); open (MAIL, "| $Mail $Recipient"); print MAIL "To: $Recipient\n"; print MAIL "From: $Sender\n"; print MAIL "Subject: $Subject\n"; while (<MAILFILE>) { print MAIL $_; }; print MAIL "\n.\n"; close (MAIL); close (MAILFILE); }; $mesg = "Mail sent to $Recipient\n"; LogMessage($mesg); unlink ("$MailFile"); };