Solved Can we add date and time (timestamp) to dmesg(8)?

nxjoseph@

Developer
Hello,

I want to know if it's possible to timestamp the dmesg(8) output and if so, how to do it? I've looked at the manpage and tried out the tunable mentioned, but it just printed out some numbers before every line of messages that humans can't read, at least me.

Does dmesg(8) get produced by syslogd? I also tried using -T syslogd flag, but I didn't see any effect of it, the flag is appears to be network related, i don't know.
 
In /etc/sysctl.conf if you add kern.msgbuf_show_timestamp=1 It will display a "sort of" timestamp.

This is how it looks:
Code:
...
[6] igb0: link state changed to UP
[6] lo0: link state changed to UP
[6] igb0: link state changed to DOWN
[10] igb0: link state changed to UP
[15] ums0 on uhub2
[15] ums0: <Logitech Wireless Receiver, class 0/0, rev 1.10/3.02, addr 1> on usbus2
....

numbers between brackets are in seconds.
Probably not what you expect but it's better than nothing.
 
Today i've had noticed that /var/log/messages have date and time added before every line. Doesn't dmesg(8) just read and show /var/log/messages? Who does add this date and time to the log file? syslogd(8) probably because it seems to use the file, checked with lsof(8).

Code:
% lsof /var/log/messages
COMMAND   PID USER FD   TYPE               DEVICE SIZE/OFF NODE NAME
syslogd 62504 root 12w  VREG 685565275,3855736514   218291  134 /var/log/messages

/var/log/messages
Code:
Aug 18 23:25:22 freebsd kernel: [12] re0: link state changed to UP
Aug 18 23:25:43 freebsd kernel: [33] re0: link state changed to DOWN
Aug 18 23:25:47 freebsd kernel: [37] re0: link state changed to UP

dmesg(8)
Code:
% dmesg | tail -3
[12] re0: link state changed to UP
[33] re0: link state changed to DOWN
[37] re0: link state changed to UP
 
It's the opposite: dmesg is the output of kernel messages, and they are held in a memory buffer. Sometime during boot, roughly when syslog is enabled, the accumulated dmesg messages are dumped into /var/log/messages; after that, kernel messages go to both the memory buffer (still readable with dmesg) and /var/log/messages. This is what Alain already said above.

I don't think having an absolute time/date in dmesg would work, because when the kernel starts, the absolute time is not known yet. That is usually acquired somewhat later, when the hardware clock is read (if present), and/or when ntp starts up (another complicated topic). See get null's message above: A relative timestamp (seconds since boot) is possible, and available with a sysctl, and the default on some Linux versions.

And whenever I say "/var/log/messages" above, I mean "whatever location is configured in syslog", which can be interestingly complicated.
 
See get null's message above: A relative timestamp (seconds since boot) is possible, and available with a sysctl, and the default on some Linux versions.
Isn't that supposed to go into /boot/loader.conf as per man dmesg?
Code:
kern.msgbuf_show_timestamp: 0
             If set to 0, no timestamps are added.  If set to 1, then a
             1-second granularity timestamp will be added to most lines in the
             message buffer.  If set to 2, then a microsecond granularity
             timestamp will be added.  This may also be set as a boot
             loader(8) tunable.  The timestamps are placed at the start of
             most lines that the kernel generates.  Some multi-line messages
             will have only the first line tagged with a timestamp.
 
If it's not enough to have the seconds as timestamps, but you must have the absolute time for each line, you could directly after the machine is booted produce a file from the recent dmesg output dmesg > mydmesg.txt, and write a small script, that calculates backwards the absolute timestamps by substracting the according seconds from the file's creation time.
 
Back
Top