Skip to content

Instantly share code, notes, and snippets.

@xbglowx
Last active April 19, 2016 08:15
Show Gist options
  • Select an option

  • Save xbglowx/d798da98ff9937e33862b285d0121bde to your computer and use it in GitHub Desktop.

Select an option

Save xbglowx/d798da98ff9937e33862b285d0121bde to your computer and use it in GitHub Desktop.
Update Prometheus alerting rules after upgrading to 0.17.0
#!/usr/bin/python
import re
import os
import sys
for root, dirs, files in os.walk('.'):
if root.find('.git') != -1 or not files:
continue
for kv_file in files:
annotation_regex = 'SUMMARY|DESCRIPTION|RUNBOOK'
with open("{}/{}".format(root, kv_file), 'r') as fh:
file_contents = []
annotations = []
for line in fh:
if re.search(annotation_regex, line):
key, value = line.split(' ', 1)
key = key.lower()
line = "{} = {},\n".format(key, value.strip('\n'))
annotations.append(" {}".format(line))
else:
line = line.replace('WITH', 'LABELS')
file_contents.append(line)
if annotations:
annotations.insert(0, 'ANNOTATIONS {\n')
annotations.append('}\n')
file_contents = file_contents + annotations
try:
with open("{}/{}.new".format(root, kv_file), 'w') as new_fh:
new_fh.write(''.join(file_contents))
new_file = "{}/{}.new".format(root, kv_file)
original_file = "{}/{}".format(root, kv_file)
os.rename(new_file, original_file)
except IOError as exc:
print >> sys.stderr, exc
sys.exit(1)
sys.exit(0)
@xbglowx

xbglowx commented Apr 19, 2016

Copy link
Copy Markdown
Author

@beorn7 thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment