Skip to content

Instantly share code, notes, and snippets.

@aspotton
Created November 19, 2015 17:26
Show Gist options
  • Save aspotton/5aa9021f5a3a3823c267 to your computer and use it in GitHub Desktop.
Save aspotton/5aa9021f5a3a3823c267 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# syslog captures great imformation but the timestamps are very cryptic
# This script is handy because it prints syslog messages with translated, human readable timestamps
# Script originally from: https://linuxaria.com/article/how-to-make-dmesg-timestamp-human-readable
#
use strict;
use warnings;
my @dmesg_new = ();
my $dmesg = "/bin/dmesg";
my @dmesg_old = `$dmesg`;
my $now = time();
my $uptime = `cat /proc/uptime | cut -d"." -f1`;
my $t_now = $now - $uptime;
sub format_time {
my @time = localtime $_[0];
$time[4]+=1; # Adjust Month
$time[5]+=1900; # Adjust Year
return sprintf '%4i-%02i-%02i %02i:%02i:%02i', @time[reverse 0..5];
}
foreach my $line ( @dmesg_old )
{
chomp( $line );
if( $line =~ m/\[\s*(\d+)\.(\d+)\](.*)/i )
{
# now - uptime + sekunden
my $t_time = format_time( $t_now + $1 );
push( @dmesg_new , "[$t_time] $3" );
}
}
print join( "\n", @dmesg_new );
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment