|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -FHEM_DIR="/opt/fhem" |
4 | | -CONFIGTYPE="${CONFIGTYPE:-"fhem.cfg"}" |
5 | | -TELNETPORT="${TELNETPORT:-7072}" |
6 | | -STATE=0 |
| 3 | +#--- Constants ------------------------------------------------------------------------------------------------------- |
7 | 4 |
|
8 | | -RUNNING_INSTANCES=$(pgrep -f "/bin/sh -c /health-check.sh" | wc -l) |
| 5 | +declare -r PID_FILE="/var/run/health-check.pid" |
| 6 | +declare -r URL_FILE="/tmp/health-check.urls" |
| 7 | +declare -r RESULT_FILE="/tmp/health-check.result" |
9 | 8 |
|
10 | | -if [ "${RUNNING_INSTANCES}" -gt "1" ]; then |
11 | | - echo "Instance already running, aborting another one" |
12 | | - exit 1 |
13 | | -fi |
14 | 9 |
|
15 | | -if [ "${CONFIGTYPE}" != "configDB" ] && [ -s "${FHEM_DIR}/${CONFIGTYPE}" ] && [ -z "$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^define .* telnet ${TELNETPORT}")" ]; then |
16 | | - TELNETPORT="$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P '^define .* telnet ' | head -1 | cut -d ' ' -f 4)" |
| 10 | +#--- Internal global ------------------------------------------------------------------------------------------------- |
17 | 11 |
|
18 | | - if [ -z "${TELNETPORT}" ]; then |
19 | | - echo "Telnet(undefined): FAILED;" |
20 | | - exit 1 |
| 12 | +declare -i gRetVal=0; |
| 13 | +declare gRetMessage=""; |
| 14 | +declare -i gSuccessCnt=0; |
| 15 | +declare -i gFailedCnt=0; |
| 16 | + |
| 17 | + |
| 18 | +#====================================================================================================================- |
| 19 | +#--- Functions ------------------------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +# Handler function for actually stopping this script. Called either by |
| 22 | +# - "exit" was called somewhere in the script |
| 23 | +# - SIGTERM is received |
| 24 | +# |
| 25 | +# Usage: trapExitHandler |
| 26 | +# Global vars: gRetMessage |
| 27 | +# gSuccessCnt |
| 28 | +# gFailedCnt |
| 29 | +# RESULT_FILE |
| 30 | +# PID_FILE |
| 31 | +# |
| 32 | +function trapExitHandler() { |
| 33 | + local -i exitVal=$? # when "exit" was called, this holds the return value |
| 34 | + trap - SIGTERM EXIT # Avoid multiple calls to handler |
| 35 | + echo "$gRetMessage" |
| 36 | + if (($exitVal == 0)) ; then |
| 37 | + echo -n "ok ($gSuccessCnt successful, $gFailedCnt failed)" > $RESULT_FILE |
| 38 | + else |
| 39 | + echo -n "ERROR ($gSuccessCnt successful, $gFailedCnt failed)" > $RESULT_FILE |
21 | 40 | fi |
22 | | -fi |
23 | | - |
24 | | -FHEMWEB=$( cd /opt/fhem; perl fhem.pl ${TELNETPORT} "jsonlist2 TYPE=FHEMWEB:FILTER=TEMPORARY!=1:FILTER=DockerHealthCheck!=0" 2>/dev/null ) |
25 | | -if [ $? -ne 0 ] || [ -z "${FHEMWEB}" ]; then |
26 | | - RETURN="Telnet(${TELNETPORT}): FAILED;" |
27 | | - STATE=1 |
28 | | -else |
29 | | - RETURN="Telnet(${TELNETPORT}): OK;" |
30 | | - |
31 | | - LEN=$( echo ${FHEMWEB} | jq -r '.Results | length' ) |
32 | | - i=0 |
33 | | - until [ "$i" == "${LEN}" ]; do |
34 | | - NAME=$( echo ${FHEMWEB} | jq -r ".Results[$i].Internals.NAME" ) |
35 | | - PORT=$( echo ${FHEMWEB} | jq -r ".Results[$i].Internals.PORT" ) |
36 | | - WEBNAME=$( echo ${FHEMWEB} | jq -r ".Results[$i].Attributes.webname" ) |
37 | | - [[ -z "${WEBNAME}" ]] && WEBNAME="fhem" |
38 | | - HTTPS=$( echo ${FHEMWEB} | jq -r ".Results[$i].Attributes.HTTPS" ) |
39 | | - [[ -n "${HTTPS}" && "${HTTPS}" == "1" ]] && PROTO=https || PROTO=http |
40 | | - |
41 | | - FHEMWEB_STATE=$( curl \ |
42 | | - --silent \ |
43 | | - --insecure \ |
44 | | - --output /dev/null \ |
45 | | - --write-out "%{http_code}" \ |
46 | | - --user-agent 'FHEM-Docker/1.0 Health Check' \ |
47 | | - "${PROTO}://localhost:${PORT}/${WEBNAME}/healthcheck" ) |
48 | | - if [ $? -ne 0 ] || |
49 | | - [ -z "${FHEMWEB_STATE}" ] || |
50 | | - [ "${FHEMWEB_STATE}" == "000" ] || |
51 | | - [ "${FHEMWEB_STATE:0:1}" == "5" ]; then |
52 | | - RETURN="${RETURN} ${NAME}(${PORT}): FAILED;" |
53 | | - STATE=1 |
54 | | - else |
55 | | - RETURN="${RETURN} ${NAME}(${PORT}): OK;" |
56 | | - fi |
57 | | - (( i++ )) |
58 | | - done |
59 | | - |
60 | | - # Update docker module data |
61 | | - if [ -s /image_info ]; then |
62 | | - RET=$( cd /opt/fhem; perl fhem.pl ${TELNETPORT} "{ DockerImageInfo_HealthCheck();; }" 2>/dev/null ) |
63 | | - [ -n "${RET}" ] && RETURN="${RETURN} DockerImageInfo:${RET};" || RETURN="${RETURN} DockerImageInfo:OK;" |
| 41 | + rm -f $PID_FILE |
| 42 | + exit $exitVal |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +#====================================================================================================================- |
| 47 | +#--- Main script ----------------------------------------------------------------------------------------------------- |
| 48 | + |
| 49 | +[ -e $PID_FILE ] && { echo "Instance already running, aborting another one" ; exit 1; } # run before installing traphandler! |
| 50 | +trap trapExitHandler SIGTERM EXIT |
| 51 | + |
| 52 | +echo "$$" > $PID_FILE |
| 53 | + |
| 54 | +[ -e $URL_FILE ] || { gRetMessage="Cannot read url file $URL_FILE" ; exit 1; } |
| 55 | + |
| 56 | +while IFS= read -r fhemUrl; do |
| 57 | + fhemwebState=$( curl \ |
| 58 | + --silent \ |
| 59 | + --insecure \ |
| 60 | + --output /dev/null \ |
| 61 | + --write-out "%{http_code}" \ |
| 62 | + --user-agent 'FHEM-Docker/1.0 Health Check' \ |
| 63 | + "${fhemUrl}" ) |
| 64 | + if [ $? -ne 0 ] || |
| 65 | + [ -z "${fhemwebState}" ] || |
| 66 | + [ "${fhemwebState}" == "000" ] || |
| 67 | + [ "${fhemwebState:0:1}" == "5" ]; then |
| 68 | + gRetMessage="$gRetMessage $fhemUrl: FAILED ($fhemwebState);" |
| 69 | + gRetVal=1 |
| 70 | + ((gFailedCnt++)) |
| 71 | + else |
| 72 | + gRetMessage="$gRetMessage $fhemUrl: OK;" |
| 73 | + ((gSuccessCnt++)) |
64 | 74 | fi |
| 75 | +done < $URL_FILE |
65 | 76 |
|
66 | | -fi |
| 77 | +exit $gRetVal |
67 | 78 |
|
68 | | -echo -n ${RETURN} |
69 | | -exit ${STATE} |
|
0 commit comments