I have cut down my perlmon to the basics, and here are some ideas you can grab:

1. Use a $RUNFILE that holds in its modification time when it was run last. This way you can compare the modification time of the $IN_FILE, and hopefully skip early because there has been no update.

2. Use an $ERRORSTATE file. Here I have made a modification in that the filesize is the same as the hour you have analized. If not, it is the first encountered error in the current $IN_FILE, and thus, we email.

3. There IS this problem, where you miss out on the logging.

09:59:01 your monitor runs
09:59:50 some error is written to ./logs/09
10:00:01 your monitor runs again, but check ./logs/10 (which probably is empty).

If you really just have 24 directories (one for each hour), then you should keep state files for each one of them (and check modification times), maybe tie them to a file (provides persistant data, like a DB, just good enough)

Assuming you only have one file in the logs directory, you can get all the files like so:

@CHECK_THESE_FILES = </raid/logs/*>;

Here is the minimized code:

#!/usr/bin/perl use strict; use warnings; my $HOUR = (localtime(time))[2]; # $HOUR = '0'.$HOUR if $HOUR<10; # make it a 09 instead of 9 my $RUNFILE = "/tmp/minimon.run"; my $ERRORSTATE = "/tmp/minimon.error"; my $lastrun = -f $RUNFILE ? (int( (-M $RUNFILE) *60*60*24) || 1) : 0; +# Seconds ago it has run. (or 1 if less than 1) # touch early to avoid bordercases (we rather check double than not) if(open(FF, ">", $RUNFILE)){ close FF; }else{ warn "Could not open $RUNFILE, $!"; } my $IN_FILE = "/tmp/raid/logs/$HOUR"; if (-f $IN_FILE){ my $fileage = -M $IN_FILE; if($lastrun > $fileage){ print "File $IN_FILE has not been updated, no action"; exitOK(); }else{ # loop through file here and determine exitERROR() or exitOK() } }else{ warn "No RAID files? I expected $IN_FILE"; } my $cmd = "cat /raid/logs/`date +%H`"; my $out_file = "/home/resource/certchange.txt"; sub exitOK{ unlink $ERRORSTATE if -f $ERRORSTATE; exit 0; } sub exitERROR{ if(-f $ERRORSTATE && ( -s $ERRORSTATE eq $HOUR) ){ warn "Already reported an error in $IN_FILE"; return 0; } if(open(ERR, ">", $ERRORSTATE)){ print ERR "." x $HOUR; close ERR; }else{ warn "Could not open ERRORSTATE $ERRORSTATE $!"; } # do email thing here exit 0; }

In reply to Re^3: How can one open a filehandle in realtime or current log by FreeBeerReekingMonk
in thread How can one open a filehandle in realtime or current log by new2perl2016

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.