]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
Make use of alert priorities
authorSven Hoexter <sven@stormbind.net>
Sat, 2 Apr 2022 15:22:49 +0000 (17:22 +0200)
committerSven Hoexter <sven@stormbind.net>
Sat, 2 Apr 2022 15:22:49 +0000 (17:22 +0200)
If all inverter on a weblog device are down, alert with Opsgenie prio P1,
otherwise stick to the Opsgenie default P3.

weblogpro/pvmon2opsgenie.sh

index d1384b9084f04f01af9e6ab48570ef59bbe3ee00..382ac7cce715045a0ea063ff45198d11ad0bdecb 100755 (executable)
@@ -22,11 +22,13 @@ mc2="http://admin:ist02@192.168.1.3/html/de/onlineOverWr.html"
 ${checkdaylight} || exit 3
 
 
-# createAlert <alias> <message> <description>
+# createAlert <alias> <message> <priority> <description>
 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 <outfile> <failures>
+# 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