您的位置:首页 > 大数据 > 人工智能

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

2016-03-29 09:20 1346 查看
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)

Run this command in Expert mode:

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


Check whether the logs are written with time stamps:

Send a test message:

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


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.

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


Create the start-up script for user-defined commands:

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


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


Assign the relevant permissions to the script file:

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


Reboot the machine.

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

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


Take the log's time stamp in seconds:

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

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)

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

3269.1667 : 60 = 54.486111666666666666666666666667 hours


Break the decimal number into 2 parts:

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


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.

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

196149.728085 seconds ~ 54 hours 29 minutes


Check the current system's uptime:

[Expert@HostName]# uptime


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/100MinutesHour/100MinutesHour/100MinutesHour/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/100MinutesHours/100MinutesHours/100MinutesHours/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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: