-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathdmesg.sh
executable file
·43 lines (37 loc) · 1.18 KB
/
dmesg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
#
# Show human readable time in kernel messages.
#
# VERSION :1.0.1
# DATE :2015-06-06
# AUTHOR :Viktor Szépe <[email protected]>
# URL :https://github.com/szepeviktor/debian-server-tools
# LICENSE :The MIT License (MIT)
# BASH-VERSION :4.2+
# LOCATION :/usr/local/sbin/dmesg.sh
# Depreceted, use: dmesg -T
# Detect pipe
if [ -t 0 ]; then
echo "Usage: dmesg|dmesg.sh" 1>&2
exit 1
fi
NOW="$(date "+%s")"
UPTIME="$(cut -d "." -f 1 /proc/uptime)"
declare -i BOOT="$(( NOW - UPTIME ))"
# Cannot determine boot time
[ -z "$BOOT" ] && exit 2
while read -r KLINE; do
# Split time and message
# For example: [3348067.465252] kernel: Message
MESSAGE="$(sed 's/^\[\s*\([[:digit:]]\+\)\.[[:digit:]]\+\] \(.*\)$/\1 \2/' <<< "$KLINE")"
MSG_TIME="${MESSAGE%% *}"
MSG_TEXT="${MESSAGE#* }"
if [ -z "$MSG_TIME" ] || [ -n "${MSG_TIME//[0-9]/}" ]; then
# Message timestamp not found or not a number
echo "??????????????? ${KLINE}"
else
# Replace timestamp with: Dec 31 23:59:59
TIME_STRING="$(date -d "@$(( BOOT + MSG_TIME ))" "+%b %d %H:%M:%S")"
echo "${TIME_STRING} ${MSG_TEXT}"
fi
done