View FAQ

[ Return To FAQ Index | Search The FAQs ]

FAQ Database : Nagios : Developer Information

Title:How can I convert the timestamp values in the log file to a friendlier format?
FAQ ID:F0070
Submitted By:Andreas Wassatsch, Martin Ryan and Steve Alston 
Last Updated:10/12/2003


User would like to view the raw log files with human-readable timestamps.



The following Perl script will print out the contents of a Nagios log file with a human-readable time format:

# nagios-log-print
# taken from the faqs section FAQID: F0070 on the website

if (!@ARGV[0]) {
    print "Usage: $0 <logfile name>\n";
    print "\n";
    print "prints the specified Nagios-logfile with timestamps\n";
    print "converted to human readable time\n";
    exit 1;

open(LOG,@ARGV[0]) || die "no Nagios log file named >>>>>>@ARGV[0]<<<<<<";
while (<LOG>) {
    ($part1, $part2) = split(/\s+/, $_, 2);
    $part1 =~ s/\[//;
    $part1 =~ s/\]//;
    print "[", scalar localtime($part1), "]\;$part2\n";

Alternatively, if you want to display the log from the command line use;

perl -pe 's/(\d+)/localtime($1)/e' nagios.log


Related FAQs:
F0068What timestamp format is used in data and log files?
Keywords:log timestamp format