2017년 12월 9일 토요일

How to make Linux dmesg seconds.nanoseconds to human readable


dmesg 를 통해서 Linux 에 많은 HW 에러등을 찾을때 도움이 되곤합니다.
그러나 라즈베리파이나 우분투 Linux 등지에서 dmesg 를 보면
부팅후 [seconds.nanoseconds] 형식이여서 언제 어떤 이벤트가 있었는지 알기 어렵습니다.

It is helpful to find many HW errors in Linux through dmesg.
But if you look at dmesg in Raspberry or Ubuntu Linux After booting, it is in the format of [seconds.nanoseconds], which makes it difficult to know when an event occurred.


[    7.537158] sd 0:0:0:0: [sda] Read Capacity(10) failed: Result: hostbyte=0x01 driverbyte=0x00
[    7.537177] sd 0:0:0:0: [sda] Sense not available.
[    7.537331] sd 0:0:0:0: [sda] Write Protect is off
[    7.537350] sd 0:0:0:0: [sda] Mode Sense: d4 80 75 b3
[    7.537461] sd 0:0:0:0: [sda] No Caching mode page found
[    7.537475] sd 0:0:0:0: [sda] Assuming drive cache: write through
[    7.539243] sd 0:0:0:0: [sda] Read Capacity(10) failed: Result: hostbyte=0x01 driverbyte=0x00
[    7.539264] sd 0:0:0:0: [sda] Sense not available.
[    7.539445] sd 0:0:0:0: [sda] Attached SCSI disk
[    8.766528] usb 1-1.5: new high-speed USB device number 5 using dwc_otg
[    8.899103] usb 1-1.5: New USB device found, idVendor=13fd, idProduct=3940
[    8.899121] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    8.899131] usb 1-1.5: Product: External
[    8.899141] usb 1-1.5: Manufacturer: ipTIME
[    8.899152] usb 1-1.5: SerialNumber: S25YJ9CC128314
[    8.900403] usb-storage 1-1.5:1.0: USB Mass Storage device detected
[    8.900966] scsi host0: usb-storage 1-1.5:1.0
[    9.927979] scsi 0:0:0:0: Direct-Access     ipTIME   External         0309 PQ: 0 ANSI: 6
[    9.930214] sd 0:0:0:0: Attached scsi generic sg0 type 0
[    9.931797] sd 0:0:0:0: [sda] Spinning up disk...
[   12.806512] .
[   12.806823] ready
[   12.807177] sd 0:0:0:0: [sda] 1250263727 512-byte logical blocks: (640 GB/596 GiB)
[   12.807868] sd 0:0:0:0: [sda] Write Protect is off
[   12.807888] sd 0:0:0:0: [sda] Mode Sense: 1f 00 10 08
[   12.808565] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, supports DPO and FUA
[   12.836220]  sda: sda1
[   12.840260] sd 0:0:0:0: [sda] Attached SCSI disk
[   13.264948] EXT4-fs (sda1): recovery complete
[   13.264983] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
[   14.684784] smsc95xx 1-1.1:1.0 eth0: hardware isn't capable of remote wakeup
[   14.974647] tun: Universal TUN/TAP device driver, 1.6
[   14.974674] tun: (C) 1999-2004 Max Krasnyansky 
[   15.340674] Adding 102396k swap on /var/swap.  Priority:-1 extents:1 across:102396k SSFS
[   18.788724] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1
[   20.743454] random: crng init done

아래 perl 스크립트로 손쉽게 사람이 알아볼수 있는 시간으로 바꿀수 있습니다.
The following perl script makes it easy to change the time you can recognize.

dmesg.pl


#!/usr/bin/perl
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";

./dmesg.pl 의결과 Below is result of dmesg.pl


[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Read Capacity(10) failed: Result: hostbyte=0x01 driverbyte=0x00
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Sense not available.
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Write Protect is off
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Mode Sense: d4 80 75 b3
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] No Caching mode page found
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Assuming drive cache: write through
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Read Capacity(10) failed: Result: hostbyte=0x01 driverbyte=0x00
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Sense not available.
[2017-12-04 16:02:56]  sd 0:0:0:0: [sda] Attached SCSI disk
[2017-12-04 16:02:57]  usb 1-1.5: new high-speed USB device number 5 using dwc_otg
[2017-12-04 16:02:57]  usb 1-1.5: New USB device found, idVendor=13fd, idProduct=3940
[2017-12-04 16:02:57]  usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[2017-12-04 16:02:57]  usb 1-1.5: Product: External
[2017-12-04 16:02:57]  usb 1-1.5: Manufacturer: ipTIME
[2017-12-04 16:02:57]  usb 1-1.5: SerialNumber: S25YJ9CC128314
[2017-12-04 16:02:57]  usb-storage 1-1.5:1.0: USB Mass Storage device detected
[2017-12-04 16:02:57]  scsi host0: usb-storage 1-1.5:1.0
[2017-12-04 16:02:58]  scsi 0:0:0:0: Direct-Access     ipTIME   External         0309 PQ: 0 ANSI: 6
[2017-12-04 16:02:58]  sd 0:0:0:0: Attached scsi generic sg0 type 0
[2017-12-04 16:02:58]  sd 0:0:0:0: [sda] Spinning up disk...
[2017-12-04 16:03:01]  .
[2017-12-04 16:03:01]  ready
[2017-12-04 16:03:01]  sd 0:0:0:0: [sda] 1250263727 512-byte logical blocks: (640 GB/596 GiB)
[2017-12-04 16:03:01]  sd 0:0:0:0: [sda] Write Protect is off
[2017-12-04 16:03:01]  sd 0:0:0:0: [sda] Mode Sense: 1f 00 10 08
[2017-12-04 16:03:01]  sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, supports DPO and FUA
[2017-12-04 16:03:01]   sda: sda1
[2017-12-04 16:03:01]  sd 0:0:0:0: [sda] Attached SCSI disk
[2017-12-04 16:03:02]  EXT4-fs (sda1): recovery complete
[2017-12-04 16:03:02]  EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
[2017-12-04 16:03:03]  smsc95xx 1-1.1:1.0 eth0: hardware isn't capable of remote wakeup
[2017-12-04 16:03:03]  tun: Universal TUN/TAP device driver, 1.6
[2017-12-04 16:03:03]  tun: (C) 1999-2004 Max Krasnyansky 
[2017-12-04 16:03:04]  Adding 102396k swap on /var/swap.  Priority:-1 extents:1 across:102396k SSFS
[2017-12-04 16:03:07]  smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1
[2017-12-04 16:03:09]  random: crng init done

댓글 없음: