Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

best way to....

by dbs (Sexton)
on Aug 31, 2011 at 17:53 UTC ( #923474=perlquestion: print w/replies, xml ) Need Help??

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

I trying to write code that checks for values greater than 90, write to a log, if -s log alert via email. Then, because of the -s test print a "$flag\n" to a local file as a way to keep track of emails sent for the next run 30 minutes later or 28 hours later. So like the code below, my problem is the flag in the file never gets cleared for the next run, so the logic
elsif ( $cntr > 2 ) { for (@p) { ....
will never be executed and if I truncate the $cntrF file the flag will always remain at 1 and thus $cntr will too. So I need a simple way to keep track of how many times -s succeeds (aka an email is sent) for every run of this job over 4 or 5 days which then the flag counter can be set to zero.
use strict; use warnings; if ( -s $log ) { $cntr = 0; $flag = 1; open $CF, ">>", $cntrF or die "'$cntrF' did not open $!\n"; print $CF "$flag\n"; sleep 1; close $CF; sleep 1; open $CF, "+<", $cntrF or die "'$cntrF' did not open $!\n"; while (<$CF>) { while (m/\b$flag\b/g) { $cntr++; } } print $cntr; if ( $cntr <= 2 ) { _mailme($alertaddr, $Esubject, $log); $CF->close; } elsif ( $cntr > 2 ) { for (@p) { if ( $_ >= 99 ) { _mailme($alertaddr, $Esubject, $log); last; } } $CF->close; } else { $CF->close; } }
thnkU!

Replies are listed 'Best First'.
Re: best way to....
by SuicideJunkie (Vicar) on Aug 31, 2011 at 18:58 UTC

    It looks to me like '$cntrF' is not defined

    Also, it sounds like you just want to save a counter in a file. Why is your file manipulation so complicated and why are you sleeping?

    Two easy ways to save your counter:

      • Incrementing: open for append, write one byte. (You're sorta doing this already, but writing 2 or 3 bytes depending on whether its running under linux or windows)
      • Reading: $count = -s $filename
      • Incrementing: open file for read, $count = <$fh>; $count++;. Open file for write (not append), print $fh $count;
      • Reading: Open for read, $count = <$fh>.

    Naturally, the first way eats up more space on disk, but you probably aren't going to count very high anyways.

      I did not quite follow your suggestions...sorry, but here is the code. I trying to write code that checks for values greater than 90, write to a log, if -s log alert via email. Then, because of the -s test print a "$flag\n" to a local file as a way to keep track of emails sent for the next run 30 minutes later or 28 hours later. So like the code below, my problem is the flag in the file never gets cleared for the next run, so the logic elsif ( $cntr > 2 ) { for (@p) { .... will never be executed and if I truncate the $cntrF file the flag will always remain at 1 and thus $cntr will too. So I need a simple way to keep track of how many times -s succeeds (aka an email is sent) for every run of this job over 4 or 5 days which then the flag counter can be set to zero.
      use strict; my ($percnt,$threshhold @p,$cntr, $flag, $CF, $alertaddr, $Esubject,); my $log = qq(/var/adm/USE_monitor.csv); my $cntrF = qq(/var/adm/USE_monitor.cntr); open LOG, "+<", $log or die "'$log' did not open $!\n"; ... {snip} ... if ( $percnt >= $threshhold ) { write (LOG); push @p, $percnt; } if ( -s $log ) { $cntr = 0; $flag = 1; open $CF, ">>", $cntrF or die "'$cntrF' did not open $!\n"; print $CF "$flag\n"; sleep 1; close $CF; sleep 1; open $CF, "+<", $cntrF or die "'$cntrF' did not open $!\n"; print $cntr; while (<$CF>) { while (m/\b$flag\b/g) { $cntr++; } } print $cntr; if ( $cntr <= 2 ) { _mailme($alertaddr, $Esubject, $log); $CF->close; } elsif ( $cntr > 2 ) { for (@p) { if ( $_ >= 99 ) { _mailme($alertaddr, $Esubject, $log); last; } } $CF->close; } else { $CF->close; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://923474]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2021-04-23 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?