aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar JustAnotherArchivist2021-12-10 13:06:27 +0000
committerGravatar JustAnotherArchivist2021-12-10 13:06:27 +0000
commit1ca7b9856fb53c5703e4a878e346c18ae9a5d7cf (patch)
tree922bdbdc0a98b7b57e8fc5d00fa57dc72f29da91
parentFix usermask length calculation (diff)
signature
Work around Grafana's new alerting system not actually sending alerts (changes) but only alert statuses
-rw-r--r--contrib/modules/grafana.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/contrib/modules/grafana.py b/contrib/modules/grafana.py
index 2fde817..feda3e8 100644
--- a/contrib/modules/grafana.py
+++ b/contrib/modules/grafana.py
@@ -1,11 +1,30 @@
+import datetime
import json
+FIVE_MINUTES = datetime.timedelta(seconds = 300)
+
+
+def parse_datetime(s):
+ assert s.endswith('Z')
+ if '.' in s:
+ s = s.split('.', 1)[0] + '+00:00'
+ else:
+ s = s[:-1] + '+00:00'
+ return datetime.datetime.fromisoformat(s)
+
+
async def process(request):
+ now = datetime.datetime.now(datetime.timezone.utc)
obj = json.loads(await request.text())
- evalMatchesStr = (': ' + ', '.join(f'{x["metric"]} = {x["value"]}' for x in obj['evalMatches'])) if 'evalMatches' in obj and obj['evalMatches'] else ''
- if 'message' in obj:
- message = obj['message'].replace('\n', ' ')
- return f'{obj["title"]} - {message}{evalMatchesStr}'
+ assert obj['version'] == '1'
+ alerts = []
+ for a in obj['alerts']:
+ startTime = parse_datetime(a['startsAt'])
+ endTime = parse_datetime(a['endsAt'])
+ if now - startTime < FIVE_MINUTES or now - endTime < FIVE_MINUTES:
+ alerts.append(f'{a["labels"]["alertname"]} {a["status"]}')
+ if alerts:
+ return f'[{obj["status"]}] {", ".join(alerts)}'
else:
- return f'{obj["title"]}{evalMatchesStr}'
+ return f'[{obj["status"]}]'