From f440a8956d7f05c3c32f867d5373458943bcbffc Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Sat, 2 Apr 2022 17:22:49 +0200 Subject: [PATCH] Make use of alert priorities If all inverter on a weblog device are down, alert with Opsgenie prio P1, otherwise stick to the Opsgenie default P3. --- weblogpro/pvmon2opsgenie.sh | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/weblogpro/pvmon2opsgenie.sh b/weblogpro/pvmon2opsgenie.sh index d1384b9..382ac7c 100755 --- a/weblogpro/pvmon2opsgenie.sh +++ b/weblogpro/pvmon2opsgenie.sh @@ -22,11 +22,13 @@ mc2="http://admin:ist02@192.168.1.3/html/de/onlineOverWr.html" ${checkdaylight} || exit 3 -# createAlert +# createAlert createAlert() { local alias="${1}" local message="${2}" - local description="${3}" + local priority="${3}" + local description="${4}" + local date="$(date)" if [ -z "${alias}" ]; then echo "ERROR: Empty alert alias" @@ -38,6 +40,10 @@ createAlert() { return fi + if [ -z "${priority}" ]; then + priority="P3" + fi + curl -o /dev/null -s \ -X POST https://${API_HOST}/v2/alerts \ -H "Content-Type: application/json" \ @@ -46,7 +52,9 @@ createAlert() { "{ \"message\":\"${message}\", \"alias\":\"${alias}\", - \"description\":\"${description}\" + \"priority\":\"${priority}\", + \"description\":\"${description}\", + \"note\":\"Localtime ${date}\" }" } @@ -86,7 +94,7 @@ parseMcLine() { echo "OK" return fi - + echo "ERROR: WR${wradresse} ${wrleistung}W Serial:${wrserial}" return 1 } @@ -160,6 +168,24 @@ checkAlertState() { return 0 } +# calcPriority +# returns an Opsgenie priority string +# If all devies fail return P1, otherwise stick with P3. +calcPriority() { + local outfile="${1}" + local failures="${2}" + local priority="P3" + local numWR="$(grep -c cLink ${outfile})" + # to be able to count with grep, we've to have our pattern ones per line + local numFailed=$(echo "${failures}"| tr ' ' '\n' | grep -c 'WR') + + if [ "${numWR}" -eq "${numFailed}" ]; then + priority="P1" + fi + + echo "${priority}" +} + ### main loop for dev in mc1 mc2; do outfile="/tmp/${dev}.html" @@ -174,8 +200,9 @@ for dev in mc1 mc2; do # handle failures and alerting if ! [ -z "${failures}" ]; then + priority=$(calcPriority "${outfile}" "${failures}") checkAlertState "${statefile}" "FAILED" && \ - createAlert "${dev}" "PV ${dev}" "${failures}" + createAlert "${dev}" "PV Ossendorf ${dev}" "${priority}" "${failures}" continue fi -- 2.39.2