-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_homeassistant.sh
executable file
·110 lines (97 loc) · 2.47 KB
/
check_homeassistant.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
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
106
107
108
109
#!/bin/bash
# Author: Wim van Ravesteijn
# Description: Nagios plugin to check Home Assistant via Observer page
# Website: https://github.com/wimvr/nagios-check_homeassistant
VERSION="0.1"
HOST=''
PORT=4357
TIMEOUT=8
STATUS_OK=0
STATUS_WARNING=1
STATUS_CRITICAL=2
STATUS_UNKNOWN=3
function usage {
echo "Usage:"
echo ""
echo " $0 -H <hostname> [-p <port>]"
echo ""
echo "Options:"
echo "-H hostname"
echo " Host name where Home Assistant runs"
echo "-p port"
echo " Port number (default: 4357)"
echo "-t timeout"
echo " Timeout in seconds when requesting status page (default: 5)"
echo "-h"
echo " Print detailed help"
echo "-V"
echo " Print version"
}
while getopts "H:p:t:hV" args; do
case $args in
H) HOST=$OPTARG ;;
p) PORT=$OPTARG ;;
t) TIMEOUT=$OPTARG ;;
V)
echo "`basename $0` version ${VERSION}"
exit $STATUS_UNKNOWN
;;
*)
usage
exit $STATUS_UNKNOWN
;;
esac
done
if [ -z "$HOST" ]; then
usage
echo ""
echo "Missing -H option"
exit $STATUS_UNKNOWN
fi
re='^[0-9]+$'
if ! [[ $PORT =~ $re ]]; then
usage
echo ""
echo "Missing or invalid -p option"
exit $STATUS_UNKNOWN
fi
if ! [[ $TIMEOUT =~ $re ]]; then
usage
echo ""
echo "Missing or invalid -t option"
exit $STATUS_UNKNOWN
fi
PAGE=`wget -O - --quiet --timeout=$TIMEOUT http://${HOST}:${PORT}/`
if [ $? -ne 0 ]; then
echo "CRITICAL: No response received while requesting status"
exit $STATUS_CRITICAL
fi
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[1]/td[1]/text()' -`" == " Supervisor: " ]; then
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[1]/td[2]/text()' -`" != " Connected " ]; then
echo "CRITICAL: supervisor not connected"
exit $STATUS_CRITICAL
fi
else
echo "UNKNOWN: unrecognised observer page"
exit $STATUS_UNKNOWN
fi
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[2]/td[1]/text()' -`" == " Supported: " ]; then
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[2]/td[2]/text()' -`" != " Supported " ]; then
echo "CRITICAL: not supported"
exit $STATUS_CRITICAL
fi
else
echo "UNKNOWN: unrecognised observer page"
exit $STATUS_UNKNOWN
fi
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[3]/td[1]/text()' -`" == " Healthy: " ]; then
if [ "`echo $PAGE | xmllint --html --xpath '//table/tr[3]/td[2]/text()' -`" != " Healthy " ]; then
echo "CRITICAL: not healthy"
exit $STATUS_CRITICAL
fi
else
echo "UNKNOWN: unrecognised observer page"
exit $STATUS_UNKNOWN
fi
echo "OK: observer reports Home Assistant up and running"
exit $STATUS_OK