How to enable time stamps for logs in 'dmesg' on Gaia and SecurePlatform

Symptoms
  • Logs that are written into dmesg kernel ring buffer do not have timestamps.
Cause

Default configuration.


Solution

Table of Contents:

  • Background
  • Enable time stamps at runtime ( on-the-fly)
  • Enable time stamps at boot time ( permanently)
  • Time stamp conversion algorithm

 

Background

For troubleshooting purposes, it can be helpful to have time stamps for logs that are written into dmesg kernel ring buffer.

Important Note: The time stamps are printed in "seconds since boot". The system uptime can be helpful to calculate an absolute time stamp if needed (run the 'uptime' command).

 

Enable time stamps at runtime (on-the-fly)

  1. Run this command in Expert mode: 

    [Expert@HostName]# echo 1 > /sys/module/printk/parameters/printk_time 

  2. Check whether the logs are written with time stamps: 

    1. Send a test message:

      [Expert@HostName]# echo "hello world" > /dev/kmsg 

    2. Check the dmesg kernel ring buffer:

      [Expert@HostName]# dmesg | tail -n 5 

      Should see an output similar to this:
      ..............
      [196149.728085] hello world
      

 

Enable time stamps at boot time (permanently)

The relevant command can be added to the existing Linux start-up script /etc/rc.d/rc.local.
As a best practice, it is recommended to create a separate start-up script /etc/rc.d/rc.local.user to separate Linux OS initialization commands from user-defined commands.

  1. Verify that /etc/rc.d/rc.local start-up script contains the call for /etc/rc.d/rc.local.user script: 

    Run the following command in Expert mode: 

    [Expert@HostName]# grep -C 1 'rc.local.user' /etc/rc.d/rc.local 

    Should get the following output:
    if [ -f /etc/rc.d/rc.local.user ]; then
     . /etc/rc.d/rc.local.user
    fi
    
  2. Create the start-up script for user-defined commands: 

    [Expert@HostName]# touch /etc/rc.d/rc.local.user 

  3. Add these lines into the /etc/rc.d/rc.local.user script:
    
    #!/bin/sh
    #
    # enable time stamps in dmesg
    echo 1 > /sys/module/printk/parameters/printk_time
    
    
  4. Assign the relevant permissions to the script file: 

    [Expert@HostName]# chmod -v u=rwx,g=rx,o=rx /etc/rc.d/rc.local.user 

  5. Reboot the machine. 

    Note: Time stamps will be enabled after the kernel has correctly booted up. 

  6. Check whether the logs are written with time stamps: 

    [Expert@HostName]# dmesg | tail -n 5 

    Should see an output similar to this:
    ..............
    [196149.728085] hello world
    

 

Time stamp conversion algorithm

Important Note: The time stamps are printed in "seconds since boot" (this is the way the kernel is programmed to print the time stamps, and it can not be changed to print the time stamps in a human readable format). The system uptime can be helpful to calculate an absolute time stamp if needed (run the 'uptime' command).

The algorithm below for converting the printed time stamps to a human readable format is demonstrated on the example given above:
[196149.728085] hello world

  1. Take the log's time stamp in seconds: 

    196149.728085 seconds (round the number down/up if needed) 

  2. Divide the time stamp in seconds by 60 to get the total amount of minutes: 

    196150 : 60 = 3269.1667 minutes (round the number down/up if needed) 

  3. Divide the time stamp in minutes by 60 to get the total amount of hours: 

    3269.1667 : 60 = 54.486111666666666666666666666667 hours 

  4. Break the decimal number into 2 parts: 

    54.486111666666666666666666666667 hours = (54 hours) + (0.486111666666666666666666666667 decimal hours) 

  5. Use the time conversion charts below to convert decimal hours to minutes: 

    0.486111666666666666666666666667 decimal hours ~ 0.48 decimal hours ~ 29 minutes 

    Note:
     For more precise conversion (down to seconds), you can use various time converters available on the Internet. Just search for 'convert decimal time' in any search engine. 

  6. Hence, we get that the log was created this amount of time since boot: 

    196149.728085 seconds ~ 54 hours 29 minutes 

  7. Check the current system's uptime

    [Expert@HostName]# uptime 

  8. To get the log's real time stamp, subtract the log's time stamp in dmesg kernel ring buffer from the current system's uptime.

 

Time conversion chart (Decimal Hours --> Minutes)

Hour/100Minutes Hour/100Minutes Hour/100Minutes Hour/100Minutes
0.0110.26160.51310.7646
0.0210.27160.52310.7746
0.0320.28170.53320.7847
0.0420.29170.54320.7947
0.0530.30180.55330.8048
0.0640.31190.56340.8149
0.0740.32190.57340.8249
0.0850.33200.58350.8350
0.0950.34200.59350.8450
0.1060.35210.60360.8551
0.1170.36220.61370.8652
0.1270.37220.62370.8752
0.1380.38230.63380.8853
0.1480.39230.64380.8953
0.1590.40240.65390.9054
0.16100.41250.66400.9155
0.17100.42250.67400.9255
0.18110.43260.68410.9356
0.19110.44260.69410.9456
0.20120.45270.70420.9557
0.21130.46280.71430.9658
0.22130.47280.72430.9758
0.23140.48290.73440.9859
0.24140.49290.74440.9959
0.25150.50300.75451.0060

Time conversion chart (Minutes --> Decimal Hours)

MinutesHours/100 MinutesHours/100 MinutesHours/100 MinutesHours/100
10.02160.27310.52460.77
20.03170.28320.53470.78
30.05180.30330.55480.80
40.07190.32340.57490.82
50.08200.33350.58500.84
60.10210.35360.60510.85
70.12220.37370.62520.87
80.13230.38380.63530.89
90.15240.40390.65540.90
100.17250.42400.67550.92
110.18260.43410.68560.94
120.20270.45420.70570.95
130.22280.47430.72580.97
140.23290.48440.73590.99
150.25300.50450.75601.00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值