FUN WITH LINUX

Converting UNIX-Timestamp into a readable time-format

19 November 2014

Assume we have a nagios-logfile(nagios.log) like this:

[1416415259] PASSIVE HOST CHECK: server1;0;PING OK - Packet loss = 0%, RTA = 0.04 ms
[1416415259] PASSIVE HOST CHECK: server2;0;PING OK - Packet loss = 0%, RTA = 0.04 ms
[1416415259] EXTERNAL COMMAND: PROCESS_HOST_CHECK_RESULT;server3;0;PING OK - Packet loss = 0%, RTA = 0.15 ms

Then we can convert the UNIX-Timestamp using the following command:

perl -n -e 'if ($_ =~ m/^\[(\d+)\] (.*)$/g){ print scalar localtime($1) . " $2" ."\n"; }' nagios.log

This would be our output then:

Wed Nov 19 17:40:59 2014 PASSIVE HOST CHECK: server1;0;PING OK - Packet loss = 0%, RTA = 0.04 ms
Wed Nov 19 17:40:59 2014 PASSIVE HOST CHECK: server2;0;PING OK - Packet loss = 0%, RTA = 0.04 ms
Wed Nov 19 17:40:59 2014 EXTERNAL COMMAND: PROCESS_HOST_CHECK_RESULT;server3;0;PING OK - Packet loss = 0%, RTA = 0.15 ms
[ Linux  Nagios  Bash  Perl  One-Liner  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti