Day: December 30, 2010

Nagios log file with normal date/time format

Nagios log file with normal date/time format

Introduction –

Nagios log file print the time in epoch time format. To convert that nagios log with normal time/date format you can use the following perl script.

Please download the script from here- http://www.indiangnu.org/wp-content/uploads/2010/nagios-log-pl.txt

* How to use it –

root@me:~# cat /etc/nagios/var/nagios.log | grep -i  NOTIFICATION | /root/nagmon/nagios-log.pl

* Pasting the code here…

root@me:~#  /root/nagmon/nagios-log.pl
#!/usr/bin/perl

# Author - IndianGNU.org
# Read nagios.log file and show log with
# converted date field in human-readable format
# 

$numArgs = $#ARGV + 1;

if ( $ARGV[0] eq "-h")
{
	print " * Usage: cat   like '/usr/local/nagios/var/nagios.log' | $0 \n"; exit 1;
}

sub epochtime
{
  my $epoch_time = shift;
  ($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

  # correct the date and month for humans
  $year = 1900 + $year;
  $month++;

  return sprintf("%02d/%02d/%02d %02d:%02d:%02d", $year, $month, $day, $hour, $min, $sec);
}

while (<>)
{
  my $epoch = substr $_, 1, 10;
  my $remainder = substr $_, 13;
  my $human_date = &epochtime($epoch);
  printf("[%s] %s", $human_date, $remainder);
}
exit;

Thanks you,
Arun