#!/bin/bash | |
CODE_OK=0 | |
CODE_UNKNOWN=1 | |
CODE_WARNING=2 | |
CODE_CRITICAL=3 | |
WARNING_THRESHOLD="80%" | |
CRITICAL_THRESHOLD="100%" | |
DEBUG=0 | |
while getopts ":C:S:c:w:dh" OPT; do | |
case "${OPT}" in | |
C) | |
CHIP="${OPTARG}" | |
;; | |
S) | |
SENSORS="${OPTARG}" | |
;; | |
c) | |
CRITICAL_THRESHOLD="${OPTARG}" | |
;; | |
w) | |
WARNING_THRESHOLD="${OPTARG}" | |
;; | |
d) | |
DEBUG=1 | |
;; | |
h) | |
cat <<__USAGE_END__ | |
$0 [-C <chip>] [-S <sensors>] [-w <warn>] [-c <crit>] [-d] [-h] | |
-C <chip> - to read sensors on certain chip | |
-S <sensors> - to read sensors matching certain regexp | |
-w <warning> - warning rate threshold (80% by default) | |
-c <critical> - critical rate threshild (100% by default) | |
-d - to print debug info | |
-h - you've just done it :) | |
__USAGE_END__ | |
>&2 | |
exit ${CODE_UNKNOWN} | |
;; | |
\?) | |
echo "Invalid option: -${OPTARG}, see -h" >&2 | |
exit ${CODE_UNKNOWN} | |
;; | |
:) | |
echo "Option -${OPTARG} requires some argument, see -h" >&2 | |
exit ${CODE_UNKNOWN} | |
;; | |
esac | |
done | |
if [ -z "${CHIP}" ]; then | |
DATA=$(/bin/sensors -u) | |
else | |
DATA=$(/bin/sensors -u "${CHIP}") | |
fi | |
if [ $? -ne 0 ]; then | |
echo "Can't get sensors' data" >&2 | |
exit ${CODE_UNKNOWN} | |
fi | |
echo "${DATA}" | awk \ | |
-v CriticalT="${CRITICAL_THRESHOLD}" \ | |
-v WarningT="${WARNING_THRESHOLD}" \ | |
-v Sensors="${SENSORS}" \ | |
-v CodeOK="${CODE_OK}" \ | |
-v CodeUnknown="${CODE_UNKNOWN}" \ | |
-v CodeWarning="${CODE_WARNING}" \ | |
-v CodeCritical="${CODE_CRITICAL}" \ | |
-v Debug=${DEBUG} \ | |
' | |
function Croak(Sensor, Input, Threshold) { | |
printf(\ | |
"%s sensor shows %0.3f whilst its threshold is %0.3f (%0.0f%)\n", \ | |
Sensor, Input, Threshold, Input / Threshold * 100 \ | |
) | |
} | |
function Finish(Sensor, Input, Threshold, Alarm) { | |
if(Debug) { | |
printf( \ | |
"Finishing the %s sensor: input is %0.3f threshold is %0.3f alarm flag is %0.3f\n", \ | |
Sensor, Input, Threshold, Alarm \ | |
) | |
} | |
if(length(Sensors)) { | |
if(Sensor !~ Sensors) { | |
return | |
} | |
} | |
if(Alarm > 0) { | |
printf("%s sensor in alarm state\n", Sensor) | |
exit CodeCritical | |
} | |
if(match(CriticalT, /^([[:digit:]]+)%$/, matched)) { | |
if ((length(Threshold) > 0) && (Input / Threshold * 100 >= matched[1])) { | |
Croak(Sensor, Input, Threshold) | |
exit CodeCritical | |
} | |
} else { | |
if (Input >= CriticalT) { | |
Croak(Sensor, Input, CriticalT) | |
exit CodeCritical | |
} | |
} | |
if(match(WarningT, /^([[:digit:]]+)%$/, matched)) { | |
if ((length(Threshold) > 0) && (Input / Threshold * 100 >= matched[1])) { | |
Croak(Sensor, Input, Threshold) | |
exit CodeWarning | |
} | |
} else { | |
if (Input >= WarningT) { | |
Croak(Sensor, Input, WarningT) | |
exit CodeWarning | |
} | |
} | |
} | |
// { | |
if(match($0, /^([^:]+):$/, matched)) { | |
SensorNew = matched[1] | |
if(length(Sensor) > 0) { | |
Finish(Sensor, Input, Threshold, Alarm) | |
} | |
Sensor = SensorNew | |
Input = "" | |
Threshold = "" | |
Alarm = "" | |
} | |
if(match($0, /^[[:space:]]+([^:]+): ([0-9\.\-]+)$/, matched)) { | |
Parameter = matched[1] | |
Value = matched[2] | |
if(Parameter ~ /_input$/) { | |
Input = Value | |
} | |
if(Parameter ~ /_(max|crit|emergency)$/) { | |
if (Value > Threshold) { | |
Threshold = Value | |
} | |
} | |
if(Parameter ~ /_alarm$/) { | |
Alarm = Value | |
} | |
} | |
} | |
END { | |
if(length(Sensor) > 0) { | |
Finish(Sensor, Input, Threshold, Alarm) | |
} | |
}' | |
STATUS="${?}" | |
if [ "${STATUS}" -lt 4 ]; then | |
exit "${STATUS}" | |
else | |
exit "${CODE_UNKNOWN}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment