-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_logwarn.in
106 lines (97 loc) · 2.99 KB
/
check_logwarn.in
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!@BASH_SHELL@
# Constants
NAME="check_logwarn"
STATE_OK='0'
STATE_WARNING='1'
STATE_CRITICAL='2'
STATE_UNKNOWN='3'
TMPFILEPAT="/tmp/${NAME}.XXXXXX"
prefix="@prefix@"
exec_prefix="@exec_prefix@"
LOGWARN="@bindir@/logwarn"
BASH_SHELL="@BASH_SHELL@"
CAT="@CAT@"
SED="@SED@"
RM="@RM@"
DEFAULT_FILTER="${SED}"' -e '\''1s/^\(.*\)$/Log errors: \1/g'\'
# Create temporary files for stdout and stderr output
STDOUT_FILE=`mktemp -q "${TMPFILEPAT}"`
if [ $? -ne 0 ]; then
echo "Can't create temporary file ${TMPFILEPAT}"
exit ${STATE_UNKNOWN}
fi
trap "${RM} -f ${STDOUT_FILE}" 0 2 3 5 10 13 15
STDERR_FILE=`mktemp -q "${TMPFILEPAT}"`
if [ $? -ne 0 ]; then
echo "Can't create temporary file ${TMPFILEPAT}"
exit ${STATE_UNKNOWN}
fi
trap "${RM} -f ${STDOUT_FILE} ${STDERR_FILE}" 0 2 3 5 10 13 15
# Scan args for '-h', '-C', or '-F'; all other stuff goes to logwarn(1)
declare -a LOGWARN_ARGS
PLUGIN_ARGS=("$@")
FILTER_COMMAND="${DEFAULT_FILTER}"
MATCH_RETURN="${STATE_WARNING}"
while [ "${#PLUGIN_ARGS[@]}" -gt 0 ]; do
KEYS=("${!PLUGIN_ARGS[@]}")
FIRST_KEY="${KEYS[0]}"
case "${PLUGIN_ARGS[$FIRST_KEY]}" in
-h)
"${LOGWARN}" -h 2>&1 \
| sed -e 's/^ log/ check_log/g' \
-e 's/^\(.*\[-L maxlines]\)\(.*\)$/\1 [-F command]\2/g' \
-e 's/^\(.*\[-ac\)\(.*\)$/\1C\2/g' \
-e '/^ -c/a\
\ -C Return Nagios level CRITICAL (instead of WARNING) if matches are found' \
-e '/^ -f/a\
\ -F Pipe logwarn(1) output through shell command (default: "'"${DEFAULT_FILTER//\\/\\\\}"'")'
exit 0
;;
-F)
NEXT_KEY=`expr $FIRST_KEY + 1`
FILTER_COMMAND="${PLUGIN_ARGS[$NEXT_KEY]}"
unset PLUGIN_ARGS[$FIRST_KEY]
unset PLUGIN_ARGS[$NEXT_KEY]
;;
-C)
MATCH_RETURN="${STATE_CRITICAL}"
unset PLUGIN_ARGS[$FIRST_KEY]
;;
--)
LOGWARN_ARGS=( "${LOGWARN_ARGS[@]}" "${PLUGIN_ARGS[@]}" )
break
;;
*)
LOGWARN_ARGS=( "${LOGWARN_ARGS[@]}" "${PLUGIN_ARGS[$FIRST_KEY]}" )
unset PLUGIN_ARGS[$FIRST_KEY]
;;
esac
done
# Run logwarn
"${LOGWARN}" "${LOGWARN_ARGS[@]}" >"${STDOUT_FILE}" 2>"${STDERR_FILE}"
LOGWARN_EXIT="$?"
# If there was an error, output the stderr output
if [ ${LOGWARN_EXIT} -gt 1 ]; then
echo -n "UNKNOWN: logwarn(1) error: "
"${CAT}" "${STDERR_FILE}" | "${SED}" 's/^logwarn: //g'
exit ${STATE_UNKNOWN}
fi
# If logwarn returned zero, there were no errors
if [ "${LOGWARN_EXIT}" -eq 0 ]; then
echo "OK: No log errors found"
exit ${STATE_OK}
fi
# Filter output?
if [ -n "${FILTER_COMMAND}" ]; then
"${BASH_SHELL}" -c "${FILTER_COMMAND}" < "${STDOUT_FILE}" > "${STDERR_FILE}" 2>&1
FILTER_RESULT="$?"
if [ "${FILTER_RESULT}" -ne 0 ]; then
echo -n "UNKNOWN: \`-F' command failed: "
"${CAT}" "${STDERR_FILE}"
exit ${STATE_UNKNOWN}
fi
mv "${STDERR_FILE}" "${STDOUT_FILE}"
fi
# There were errors; include them in our output
"${CAT}" "${STDOUT_FILE}"
exit ${MATCH_RETURN}