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

Re^5: Simple awk question

by GotToBTru (Prior)
on Jun 05, 2014 at 20:15 UTC ( [id://1088911]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Simple awk question
in thread Simple awk question

For this purpose, you want the text of the error message as much as the timestamp, to know if the error is the same or different. That should be relatively simple. The question would be how to enforce the time window.

Two approaches suggest themselves to me. First, store the timestamp and error message in a file. The next time your script runs, read the timestamp from that file and compare to the current time. This will involve learning how to convert strings into a scalar (or object) representing time. There are CPAN modules to help with this; Time::Piece is the first one I found using a simple search. This would be useful skill to have, and in my opinion worth the effort.

However, a simpler approach would be to write just the error message out to a file (so you can compare it to the next message), and treat the create date of the file as the timestamp. It should be reasonably close to the actual error timestamp (worst case plus the running time interval of your script). And, you can use the -M operator which returns the age of the file in days to determine if it has been 30 minutes or not since you last saw that error.

use strict; use warnings; my $window = 30.0 / 1440; # 30 minutes my $error_file = 'error.txt'; my $error_message = shift; chomp($error_message); if (! -e $error_file) { # generate warning and then write_error_file(); } else { chomp(my $old_error = `cat $error_file`); # two conditions warrant a warning # a) either a new type of error or b) it has been # more than 30 minutes since we last saw this one if (($old_error ne $error_message) || (-M $error_file > $window)) { # generate warning and then write_error_file(); } } sub write_error_file { my $ofh; print "New error $error_message\n"; open $ofh,'>',$error_file; print $ofh $error_message; close $ofh; }
1 Peter 4:10

Replies are listed 'Best First'.
Re^6: Simple awk question
by czah7 (Initiate) on Jun 05, 2014 at 21:24 UTC
    So I already have that sort of function in the script, it turns the date into a number and compares it. It compares the time stampe of the error, with the timestamp of the date the last date that it ran and if it's not an old error, then it alerts. So that means if I have a problem that happens every few seconds, then every time it sees this error it will be a new error. So maybe I need another check to see if it's the same error, don't alert.

    What I was thinking actually was redo'ing how it even checks the error log. Instead of even looking at an "old error", it can grep for everything that was "10 min ago" or 30minutes. Save that to a file, and only search that file. Then all I need to do is make sure my $lasterror isn't the same as my $currenterror or something right?

Log In?
Username:
Password:

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

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

    No recent polls found