Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Simple awk question

by czah7 (Initiate)
on Jun 05, 2014 at 18:35 UTC ( [id://1088893]=note: print w/replies, xml ) Need Help??


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

So to make it simple.
#!/usr/bin/perl $error = '2014-06-04T15:24:21-05:00 syslog_dp [0x80e00099][ftp][error] + secure-backup(FBB): trans(5487)'; $time = print $error|print $F[0];
$time should then = 2014-06-04T15:24:21-05:00

Replies are listed 'Best First'.
Re^3: Simple awk question
by fishmonger (Chaplain) on Jun 05, 2014 at 19:02 UTC
    $time = print $error|print $F[0];

    That attempt to pipe one statement to another statement like you would do when piping shell commands won't work.

    You've already been shown a couple ways to extract the datestamp.

    $error = '2014-06-04T15:24:21-05:00 syslog_dp [0x80e00099][ftp][error] + secure-backup(FBB): trans(5487)'; $time = (split /\s/, $error)[0]; print $time, "\n"; # or you could do this: ($time) = $error =~ /^(\S+)/; print $time, "\n";
      Thanks Fishmonger, that worked! I do appreciate it. However simple it was.

      I may have need of a second request though. Not sure if I should start another thread. But the purpose of this script is to watch a log file, and if it sees an error it emails me. The reason for the timestamp is because I am comparing the time, so If it's an old error I don't get alerted. The problem now is that if it's a continuous error that we are aware of, I still don't want to be alerted every 5-10min(whatever interval i decide to run the script). So I need to write the error it found to a log file. And somehow tell it "if you found this error again, and less than 30min has passed, don't send another alert". Does this make sense?

        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

Log In?
Username:
Password:

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

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

    No recent polls found