Update to sample Perl code in FAQ ID: F0070

Patrick Salsbury salsbury at sculptors.com
Fri Sep 19 05:42:35 CEST 2003


Hi, all. Just joined the list as I wanted to point out something I noticed
in FAQ ID: F0070 at
http://www.nagios.org/faqs/viewfaq.php?faq_id=70&expand=true&showdesc=true

First off, and somewhat oddly, the browser is eating a couple of bits of
angle-bracketed code, even though it's inside <pre></pre> tags. Not sure
why that is, but it happens in both Galeon and Mozilla. Looking at the
page source you can see the '<Nagios-Logfile>' and '<LOG>' parts plain as
day (Both browsers are actually helpful enough to highlight them for me
since they think they're HTML tags.)

I also found a reference to a subroutine of some sort which doesn't appear
to ship with the fairly vanilla Perl on my Linux box (v5.8.0 built for
i386-linux-thread-multi). Not sure if it's a CPAN module, or someone's own
function from their personal library that they just forgot about, or maybe
even a weird robo-translation error that crept in by the webserver or FAQ
database. In any event, it seems that the call to 'concat' doesn't work,
but changing it to 'open' works just fine.

I also threw in a '-w' to get more checking on the code, and Perl suggested
a few other minor changes. I added some comments, which needn't keep my
initials in them, but do keep that nifty 1-liner for the command line
handy, and I added a bit of checking/complaining if the user calls it
without an input file to parse. My diffs are below. 

I've been eyes-deep in Nagios for a week or so, with some ramping up over
the past few weeks. I've actually had my eye on it since way back in
the early 'NetSaint' days. At that point, I was working for a
Networking/Security Consulting firm doing large Micromuse Netcool
installations for a bunch of (now extinct) ISPs with some seriously massive
networks. 

Even back in early 2001, NetSaint looked pretty amazing, and it already did
some things that customers were asking us for (such as text-based alerts
that could go to WAP or an old monochrome terminal) and Netcool developers were
saying "No" to. (I think they were a bit too trapped by their own GUI.)

I'd been playing around with CVS builds over the last year, but didn't have
the time to devote to the thick & chewy docs, but I've just recently joined
a high-speed wireless ISP startup in the SF Bay area, and I suspect that
Nagios is going to play a great part in helping us. 

I'm usually swamped with mail and slow with replies, so you may not hear
much from me, here. Or you might. I don't really know, yet. But I wanted to
at least notify folks so the FAQ could be corrected and the code would work
for others. (Might want to just link to a plaintext version of the file,
rather than including it in the HTML doc. I dunno...)

Anyway, You've got a new member of the Nagios family, and I wanted to thank
you all for the great work! It's really a fantastic product, even if it
*is* a bitch to learn at first. (Netcool was no better.) I actually think
your docs and examples are very well done, and you don't seem to require a
training class to even get started...just some perserverance. 


I'm off to learn some more... :-)


Pat
	   ___________________Think For Yourself____________________
	 Patrick G. Salsbury - http://reality.sculptors.com/~salsbury/
	      Contribute to the "Laws We'd Like To See" project:
		   http://reality.sculptors.com/cgi-bin/fom
	   ---------------------------------------------------------
	      The Internet...it's not just for porn anymore. ;^)


Here's my diff:
>diff -u3 convert-nagios-logtime.pl.old convert-nagios-logtime.pl



--- convert-nagios-logtime.pl.old      2003-09-18 19:54:47.000000000 -0700
+++ convert-nagios-logtime.pl   2003-09-18 18:01:17.000000000 -0700
@@ -1,19 +1,28 @@
-#!/usr/bin/perl
+#!/usr/bin/perl -w
+
+# Convert Nagios timestamps into human-readable format. - PGS 09-18-03
+# From the FAQ at: 
+# http://www.nagios.org/faqs/viewfaq.php?faq_id=70&expand=true&showdesc=true
+#
+# Alternatively, if you want to display the log from the command line use;
+# perl -pe 's/(\d+)/localtime($1)/e' nagios.log
+
 use Time::Local;
 
-if (!@ARGV[0]) {
-    print "Usage: $0 \n";
+if (!$ARGV[0]) {
+    print "Usage: $0 <Nagios-Logfile>\n";
     print "\n";
     print "prints the specified Nagios-logfile with timestamps\n";
     print "converted to human readable time\n";
 }
 
-concat(LOG, at ARGV[0]);
+open(LOG,$ARGV[0]) || die "Couldn't open logfile: $!\n";
 while (! eof(LOG)) {
-    $line = ;
+    $line = <LOG>;
     ($part1, $part2) = split(/\s+/, $line, 2);
     $part1 =~ s/\[//;
     $part1 =~ s/\]//;
     print "[", scalar localtime($part1), "] $part2";
 };
 close(LOG);
+

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 249 bytes
Desc: not available
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20030918/a6dce9e1/attachment.sig>


More information about the Developers mailing list