Created
January 6, 2016 02:30
-
-
Save kaidokert/6f403e0177b09650fda7 to your computer and use it in GitHub Desktop.
Dump all defined alerts in Librato to a cleaned and ordered Yaml file, to use in Salt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import collections | |
import yaml | |
import librato | |
# ht to http://stackoverflow.com/a/16782282/295231 | |
# http://stackoverflow.com/a/20720773/295231 | |
class UnsortableList(list): | |
def sort(self, *args, **kwargs): | |
pass | |
class UnsortableOrderedDict(collections.OrderedDict): | |
def items(self, *args, **kwargs): | |
return UnsortableList( | |
collections.OrderedDict.items(self, *args, **kwargs)) | |
def unicode_representer(dumper, uni): | |
return yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni) | |
class KludgeDumper(yaml.Dumper): | |
def __init__(self, *args, **kwargs): | |
super(KludgeDumper, self).__init__(*args, **kwargs) | |
self.add_representer(unicode, unicode_representer) | |
self.add_representer(UnsortableOrderedDict, | |
yaml.representer.SafeRepresenter.represent_dict) | |
def del_if_default(dic, attrib, value): | |
if dic[attrib] == value: | |
del dic[attrib] | |
def clean_condition(condition): | |
result = UnsortableOrderedDict({}) | |
for k in ( 'metric_name', 'summary_function', 'condition_type', | |
'threshold', 'source'): | |
result[k] = condition[k] | |
if not result['source']: | |
del result['source'] | |
return result | |
def clean_alert(alert_obj): | |
alert_defaults = { | |
'rearm_seconds': 600, | |
'attributes': {}, | |
'active': True | |
} | |
alert = alert_obj.__dict__ | |
result = UnsortableOrderedDict({'name': alert['name']}) | |
if alert['description']: | |
result.update({'description': alert['description']}) | |
conditions = [clean_condition(x.__dict__) for x in alert['conditions']] | |
for k in ('_id', 'name', 'connection', 'services', 'version', | |
'description', 'conditions'): | |
alert.pop(k, None) | |
for k, v in alert_defaults.items(): | |
del_if_default(alert, k, v) | |
result.update(alert) | |
result.update({'conditions': conditions}) | |
return result | |
def dump_tree(api, output): | |
list_alerts = [] | |
for alert in api.list_alerts(): | |
list_alerts.append(clean_alert(alert)) | |
sorted_list = sorted(list_alerts, key=lambda k: k['name']) | |
tree = {'alerts': sorted_list} | |
print yaml.dump(tree, stream=output, Dumper=KludgeDumper, | |
default_flow_style=False, | |
allow_unicode=True) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('user', help='Librato login name [email protected]') | |
parser.add_argument('apitoken', help='Librato API token') | |
parser.add_argument('--output', type=argparse.FileType('w', 0)) | |
args = parser.parse_args() | |
api = librato.connect(args.user, args.apitoken) | |
dump_tree(api, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment