Browse Source

Work around Grafana's new alerting system not actually sending alerts (changes) but only alert statuses

master
JustAnotherArchivist 2 years ago
parent
commit
1ca7b9856f
1 changed files with 24 additions and 5 deletions
  1. +24
    -5
      contrib/modules/grafana.py

+ 24
- 5
contrib/modules/grafana.py View File

@@ -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"]}]'

Loading…
Cancel
Save