Skip to content

Commit d0cd1a1

Browse files
author
Michael Jennings
committed
New check: check_cmd_dmesg()
This check, requested by Mike Dubman <[email protected]>, both addresses a user suggestion/critique *and* provides a working example of how to write checks that wrap the built-in check_cmd_output() function. With the recently added improvements to the aforementioned check, wrappers can now provide customized and/or user-defined messages for each match string. check_cmd_dmesg() takes advantage of this capability to allow the user to detect certain error messages that might appear in "dmesg" output and display a unique check failure message for each one. The syntax for check_cmd_dmesg() (and check_cmd_output() too) is: check_cmd_dmesg [-r rc] [-t tm] [-e cmd] [-M msg] [-m mstr] [...] where: -r rc Expect cmd to return rc; fail if it doesn't -t tm Time out cmd after tm seconds -e cmd Use "cmd" instead of "dmesg" as the command to run -M msg Use "msg" as the error message for corresponding mstr -m mstr Match string (most likely negated) to check "cmd" output You can specify "-M msg" and "-m mstr" multiple times to perform multiple matches. If specified and non-empty, the 1st msg corresponds to the 1st mstr, the 2nd msg to the 2nd mstr, and so on. Values supplied for msg can use replaceables %m for mstr, %c for cmd, %l for the line number of the match (for negated mstr only), and %L for the entire matching line (again, for negated match strings only). If multiple match strings are given, %f can be used to show how many match strings failed. In the case of check_cmd_dmesg(), the user will typically want to supply a negated match string (e.g., "-m '!/segfault at/'") since they'll almost always be looking for a string that *shouldn't* appear. (In fact, perhaps check_cmd_dmesg() should add the ! if it's not there? Thoughts?) TODO: Cache dmesg output so it's only called once?
1 parent 3dd8295 commit d0cd1a1

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

scripts/lbnl_cmd.nhc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function check_cmd_output() {
1212
local LINENO=0 MATCH_CNT=0 NEG_MATCH_CNT=0 i
1313
local -a CMD_LIST LINES MATCHES MATCHED MSGS
1414

15-
MATCHES=( )
1615
OPTIND=1
1716
while getopts ":C:M:O:e:m:r:t:" OPTION ; do
1817
case "$OPTION" in
@@ -81,6 +80,7 @@ function check_cmd_output() {
8180
MSG=${MSGS[$i]:-\"%m\" matched at line %l of \"%c\"}
8281
MSG=${MSG//\%m/${MATCHES[$i]:1}}
8382
MSG=${MSG//\%l/$((LINENO+1))}
83+
MSG=${MSG//\%L/${LINES[$LINENO]//[\"\`\$]}}
8484
MSG=${MSG//\%c/$CMD}
8585
die 1 "$CHECKNAME: $MSG."
8686
return 1
@@ -182,3 +182,25 @@ function check_cmd_status() {
182182
fi
183183
return 0
184184
}
185+
186+
# Check dmesg output for matching lines. Fairly simple wrapper for check_cmd_output() (above).
187+
function check_cmd_dmesg() {
188+
local CMD_TIMEOUT CMD_RETVAL CMD="dmesg"
189+
local -a MATCHES MSGS
190+
191+
OPTIND=1
192+
while getopts ":M:e:m:r:t:" OPTION ; do
193+
case "$OPTION" in
194+
M) MSGS[${#MSGS[*]}]=-M ; MSGS[${#MSGS[*]}]="$OPTARG" ;;
195+
e) CMD="$OPTARG" ;;
196+
m) MATCHES[${#MATCHES[*]}]=-m ; MATCHES[${#MATCHES[*]}]="$OPTARG" ;;
197+
r) CMD_RETVAL="$OPTARG" ;;
198+
t) CMD_TIMEOUT="$OPTARG" ;;
199+
:) die 1 "$FUNCNAME: Option -$OPTARG requires an argument." ; return 1 ;;
200+
\?) die 1 "$FUNCNAME: Invalid option: -$OPTARG" ; return 1 ;;
201+
esac
202+
done
203+
shift $((OPTIND-1))
204+
205+
check_cmd_output -C $FUNCNAME ${CMD_RETVAL:+-r $CMD_RETVAL} ${CMD_TIMEOUT:+-t $CMD_TIMEOUT} -e "$CMD" "${MSGS[@]}" "${MATCHES[@]}"
206+
}

0 commit comments

Comments
 (0)