Skip to content

Commit 7689e03

Browse files
authored
Merge pull request #3830 from reubenmiller/feat-add-dmesg-plugin
feat: add dmesg log plugin
2 parents fc802e4 + 1451357 commit 7689e03

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
NOT_SUPPORTED=2
5+
6+
usage() {
7+
cat <<EOT
8+
dmesg log plugin for thin-edge.io
9+
10+
IMPORTANT: Some dmesg implementations don't support date/time filters, so it will fallback to
11+
calling dmesg without any filtering should the datetime filters fail for any reason.
12+
13+
The log plugin allows users to access kernel messages from dmesg.
14+
15+
Usage:
16+
$0 list
17+
List all log types supported by this plugin
18+
19+
$0 get <log-type> [options]
20+
Fetch logs for the given facility (<log-type>)
21+
22+
Options for "get":
23+
--since <timestamp> Show logs since the given timestamp (seconds since epoch)
24+
--until <timestamp> Show logs up to the given timestamp (seconds since epoch)
25+
--help Show this help message and exit
26+
27+
Examples:
28+
$0 list
29+
$0 get all --since 1696250000 --until 1696260000
30+
EOT
31+
}
32+
33+
is_busy_box() {
34+
if dmesg --help 2>&1 | grep -q "facility"; then
35+
return 1
36+
fi
37+
return 0
38+
}
39+
40+
list_log_types() {
41+
if ! command -V dmesg >/dev/null 2>&1; then
42+
exit "$NOT_SUPPORTED"
43+
fi
44+
45+
# Note: The dmesg implementations vary a lot on different
46+
# operating systems, so facility filtering is difficult to provide
47+
printf 'all\n' # Default type
48+
}
49+
50+
format_timestamp() {
51+
value="$1"
52+
case "$value" in
53+
''|*[!0-9]*)
54+
echo "$value"
55+
;;
56+
*)
57+
# convert unix timestamp to iso date, as dmesg does not support unix timestamps
58+
date -d @"$value" +%Y-%m-%d\ %H:%M:%S
59+
;;
60+
esac
61+
}
62+
63+
get_logs() {
64+
# Note: the log type is unused, but still enforce the cli contract
65+
if [ $# -lt 1 ]; then
66+
echo "Error: 'get' command expects a mandatory <log-type> arguments" >&2
67+
usage
68+
exit 1
69+
fi
70+
_log_type="$1"
71+
shift
72+
73+
# default to a large time period as kernel messages aren't published so frequently
74+
since="90 day ago"
75+
until="0 second ago"
76+
77+
while [ $# -gt 0 ]; do
78+
case $1 in
79+
--since)
80+
since=$(format_timestamp "$2")
81+
shift
82+
;;
83+
84+
--until)
85+
until=$(format_timestamp "$2")
86+
shift
87+
;;
88+
89+
--help)
90+
usage
91+
exit 0
92+
;;
93+
*)
94+
echo "Unknown option: $1" >&2
95+
exit 1
96+
;;
97+
esac
98+
shift
99+
done
100+
101+
# try calling with all filters, and fallback to a plain dmesg command
102+
if ! dmesg \
103+
--nopager \
104+
--time-format=iso \
105+
--color=never \
106+
--since "$since" \
107+
--until "$until" 2>/dev/null; then
108+
109+
echo "WARNING: dmesg version does not advanced flags, so calling using 'dmesg' instead" >&2
110+
dmesg
111+
fi
112+
}
113+
114+
main() {
115+
if [ $# -lt 1 ]; then
116+
usage
117+
exit 1
118+
fi
119+
120+
SUBCOMMAND="$1"
121+
shift
122+
123+
case "$SUBCOMMAND" in
124+
"list")
125+
list_log_types
126+
;;
127+
"get")
128+
get_logs "$@"
129+
;;
130+
*)
131+
echo "Error: Unknown subcommand '$SUBCOMMAND'" >&2
132+
usage
133+
exit 1
134+
;;
135+
esac
136+
}
137+
138+
main "$@"

configuration/package_manifests/nfpm.tedge.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ contents:
114114
mode: 0755
115115
packager: rpm
116116

117+
# dmesg
118+
- src: ./configuration/contrib/log-plugins/dmesg
119+
dst: /usr/share/tedge/log-plugins/
120+
file_info:
121+
mode: 0755
122+
117123
# preset diagnostic plugins
118124
- src: ./configuration/contrib/diag-plugins/01_tedge.sh
119125
dst: /usr/share/tedge/diag-plugins/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
*** Settings ***
2+
Resource ../../../resources/common.resource
3+
Library DateTime
4+
Library OperatingSystem
5+
Library String
6+
Library Cumulocity
7+
Library ThinEdgeIO
8+
9+
Suite Setup Custom Setup
10+
Test Teardown Get Logs
11+
12+
Test Tags theme:c8y theme:log
13+
14+
15+
*** Test Cases ***
16+
Log operation dmesg plugin
17+
Should Contain Supported Log Types all::dmesg
18+
${start_timestamp}= Get Current Date UTC -1 hours result_format=%Y-%m-%dT%H:%M:%S+0000
19+
${end_timestamp}= Get Current Date UTC +1 hours result_format=%Y-%m-%dT%H:%M:%S+0000
20+
${operation}= Cumulocity.Get Log File
21+
... type=all::dmesg
22+
... date_from=${start_timestamp}
23+
... date_to=${end_timestamp}
24+
... maximum_lines=100
25+
${operation}= Operation Should Be SUCCESSFUL ${operation}
26+
Log Operation Attachment File Should Not Be Empty
27+
... ${operation}
28+
29+
30+
*** Keywords ***
31+
Custom Setup
32+
${DEVICE_SN}= Setup
33+
Set Suite Variable $DEVICE_SN
34+
Device Should Exist ${DEVICE_SN}
35+
ThinEdgeIO.Service Health Status Should Be Up tedge-agent
36+
37+
Log Operation Attachment File Should Not Be Empty
38+
[Arguments] ${operation}
39+
${event_url_parts}= Split String ${operation["c8y_LogfileRequest"]["file"]} separator=/
40+
${event_id}= Set Variable ${event_url_parts}[-2]
41+
${contents}= Cumulocity.Event Should Have An Attachment
42+
... ${event_id}
43+
... encoding=utf-8
44+
... expected_size_min=1
45+
RETURN ${contents}

0 commit comments

Comments
 (0)