2 # Checks inverter state as repoted by a meteocontrol weblog
3 # device HTML interface, reports issues via Atlassian
4 # Opsgenie. Written with the intention to run via cron on
5 # a LTE Router with either OpenWRT or Teltonika RutOS.
6 # Requires curl other stock sh/cut/cat/test are enough.
8 # createAlert <alias> <message> <description>
12 local description="${3}"
14 if [ -z "${alias}" ]; then
15 echo "ERROR: Empty alert alias"
19 if [ -z "${message}" ]; then
20 echo "ERROR: Empty alert message"
24 curl -o /dev/null -s \
25 -X POST https://${API_HOST}/v2/alerts \
26 -H "Content-Type: application/json" \
27 -H "Authorization: GenieKey ${API_KEY}" \
30 \"message\":\"${message}\",
31 \"alias\":\"${alias}\",
32 \"description\":\"${description}\"
40 if [ -z "${alias}" ]; then
41 echo "ERROR: Empty alert alias"
45 curl -o /dev/null -s \
46 -X POST https://${API_HOST}/v2/alerts/${alias}/close?identifierType=alias \
47 -H "Authorization: GenieKey ${API_KEY}" \
48 -H "Content-Type: application/json" \
53 # parseMcLine <meteocontrol html page output line>
57 local wrleistung="$(echo ${input} | cut -d'>' -f 13 | cut -d'<' -f 1)"
58 local wradresse="$(echo ${input} | cut -d'>' -f 4 | cut -d'<' -f 1)"
59 local wrserial="$(echo ${input} | cut -d'>' -f 7 | cut -d'<' -f 1)"
61 # known bad case e.g. during the night
62 if [ "${wrleistung}" = "---" ]; then
63 echo "No Power Value at WR${wradresse} Serial:${wrserial}"
68 if [ "${wrleistung}" -gt 0 ]; then
73 echo "ERROR: WR${wradresse} ${wrleistung}W Serial:${wrserial}"
77 # parseMcOutput <meteocontrol status html output page>
81 if ! [ -f "${file}" ]; then
82 echo "ERROR: Supplied input ${file} is not a regular file"
86 grep "cLink" "${file}" | while read line; do
87 result="$(parseMcLine \"${line}\")"
89 if [ "${result}" = "OK" ]; then
93 echo -n "${result} -- "
97 # getMCstates <outfile> <status-url>
102 if [ -z "${outfile}" ]; then
103 echo "ERROR: no meteocontrol tmp file provided"
107 if [ -z "${url}" ]; then
108 echo "ERROR: no meteocontrol URL provided"
112 # remove eventual remains of prior runs
115 # ready to try to download the data from meteocontrol
116 if ! curl -s -o "${outfile}" "${url}"; then
117 echo "ERROR: could not download ${url} into ${outfile}"
121 # verify we have content to parse in our downloaded file
122 if ! grep -c -q cLink "${outfile}"; then
123 echo "ERROR: no matching cLink lines found in output file ${outfile}"
128 # checkAlertState <statefile> <new-status>
129 # return 0 on alert state change
130 # return 1 if nothing changed
132 local statefile="${1}"
133 local newstatus="${2}"
134 local oldstatus="null"
136 test -f "${statefile}" && oldstatus="$(cat ${statefile})"
137 if [ "${oldstatus}" = "${newstatus}" ]; then
141 # update state file on state change
142 echo "${newstatus}" > "${statefile}"
146 ### main configuration
147 API_HOST="api.eu.opsgenie.com"
150 # adjust meteocontrol default password and IPs
151 mc1="http://admin:ist02@192.168.1.2/html/de/onlineOverWr.html"
152 mc2="http://admin:ist02@192.168.1.3/html/de/onlineOverWr.html"
155 for dev in mc1 mc2; do
156 outfile="/tmp/${dev}.html"
157 statefile="/tmp/pvstate-${dev}"
158 url=$(eval echo \${$dev})
160 # download the html overview page from meteocontrol device
161 getMCstates "${outfile}" "${url}"
163 # parse the overview page and collect failures
164 failures=$(parseMcOutput "${outfile}")
166 # handle failures and alerting
167 if ! [ -z "${failures}" ]; then
168 checkAlertState "${statefile}" "FAILED" && \
169 createAlert "${dev}" "PV ${dev}" "${failures}"
173 # update alert state close alert on state change
174 checkAlertState "${statefile}" "OK" && \