Skip to content

Instantly share code, notes, and snippets.

@sokada1221
Created November 4, 2019 21:00
Show Gist options
  • Save sokada1221/6cf03ec61a6d7a64d7947bf39f9379d1 to your computer and use it in GitHub Desktop.
Save sokada1221/6cf03ec61a6d7a64d7947bf39f9379d1 to your computer and use it in GitHub Desktop.
Helper script to add the for duration to the annotations
import os
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
# yaml.preserve_quotes = True
directory = "."
for filename in os.listdir(directory):
if filename.endswith(".yml"):
with open(filename, "r") as fp:
print('Working with: ' + filename)
data = yaml.load(fp)
if ('groups' in data) and (data['groups'] is not None) and ('rules' in data['groups'][0]) and (data['groups'][0]['rules'] is not None):
for alert in data['groups'][0]['rules']:
if 'for' in alert:
alert['annotations']['for'] = alert['for']
if 'summary' in alert['annotations'].ca.items:
alert['annotations'].ca.items['for'] = alert['annotations'].ca.items.pop('summary')
# print(alert['annotations'])
with open(filename, "w") as wfp:
yaml.width = 4096
yaml.dump(data, wfp)
# yaml.dump(data, sys.stdout)
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment