-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert-alerts-to-promrules.py
executable file
·44 lines (38 loc) · 1.38 KB
/
convert-alerts-to-promrules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
import yaml
import sys
alerts = yaml.safe_load_all(sys.stdin.read())
promrules = []
for alert in alerts:
promrule = {
'apiVersion': 'monitoring.coreos.com/v1',
'kind': 'PrometheusRule',
'metadata': {
'name': alert['metadata']['name'],
'namespace': alert['metadata']['namespace'],
'labels': {
'role': 'alert-rules',
},
},
'spec': {
'groups': [{
'name': alert['metadata']['name'] + '.rules',
'rules': [{
'alert': alert['metadata']['name'],
'annotations': {
'summary': alert['metadata']['annotations']['heimdall.uswitch.com/summary']
},
'expr': alert['spec']['expr'],
'for': alert['spec']['for'],
'labels': {
'name': alert['metadata']['name'],
'namespace': alert['metadata']['namespace'],
}
}]
}]
}
}
for label_name, label_value in alert.get('metadata', []).get('labels', {}).items():
promrule['spec']['groups'][0]['rules'][0]['labels'][label_name] = label_value
promrules.append(promrule)
print(yaml.dump_all(promrules, explicit_start=True, default_flow_style=False))