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)
@beorn7

beorn7 commented Apr 14, 2016

Copy link
Copy Markdown

Here is my try:

#!/usr/bin/python3

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:
        if kv_file.endswith('.__prom_new__'):
            continue
        annotation_regex = re.compile(r'(\s*)(SUMMARY|DESCRIPTION|RUNBOOK)\s+(.+)\n$')
        in_name = "{}/{}".format(root, kv_file)
        out_name = "{}/{}.__prom_new__".format(root, kv_file)
        with open(in_name, 'r') as input:
            with open(out_name, 'w') as output:
                in_annotations = False
                indent = ''
                for line in input:
                    m = annotation_regex.match(line)
                    if m:
                        indent = m.group(1)
                        line = "  {}{} = {},\n".format(indent, m.group(2).lower(), m.group(3))
                        if not in_annotations:
                            output.write(indent + 'ANNOTATIONS {\n')
                            in_annotations = True
                        output.write(line)
                    else:
                        if in_annotations:
                            output.write(indent + '}\n')
                            in_annotations = False
                        line = line.replace('WITH', 'LABELS')
                        output.write(line)
                if in_annotations:
                    output.write(indent + '}\n')
                    in_annotations = False
        try:
            os.rename(out_name, in_name)
        except IOError as exc:
            print(exc, file=sys.stderr)
            sys.exit(1)
sys.exit(0)

@beorn7

beorn7 commented Apr 18, 2016

Copy link
Copy Markdown

I should point out that the above is still very rough with loads of potential for improvement.

For one, it requires that SUMMARY, DESCRIPTION, and RUNBOOK are all-caps. (But as a bonus of that, it will work with mixed files that contain both old and new syntax.)

@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