ansible-role-sensu/library/sensugo_check.py

125 lines
3.4 KiB
Python
Raw Permalink Normal View History

2019-01-01 19:44:04 +00:00
#!/usr/bin/python
from ansible.module_utils.basic import *
from ansible.module_utils.sensu_api import *
class SensuCheck:
2019-05-09 09:08:36 +00:00
def __init__(self, api, name, namespace, labels):
2019-01-01 19:44:04 +00:00
self.api = api
self.name = name
self.namespace = namespace
2019-05-09 09:08:36 +00:00
self.labels = labels
2019-01-01 19:44:04 +00:00
self.exist = False
def get_data(self):
status_code, data = self.api.get('namespaces/{}/checks/{}'.format(self.namespace, self.name))
if status_code == 200:
self.exist = True
2019-05-09 09:08:36 +00:00
return data
return {}
def labels_has_changed(self, new_labels, old_labels):
if old_labels is None or len(new_labels) != len(old_labels):
return True
for old_label, old_value in old_labels.iteritems():
2019-05-14 09:59:40 +00:00
if old_label in new_labels and new_labels[old_label] == old_value:
2019-05-09 09:08:36 +00:00
continue
return True
return False
2019-01-01 19:44:04 +00:00
def has_changed(self, options):
2019-05-09 09:08:36 +00:00
data = self.get_data()
if self.labels_has_changed(self.labels, data['metadata'].get('labels')):
return True
2019-05-14 09:59:40 +00:00
data.pop('metadata')
for option, value in data.iteritems():
2019-01-01 19:44:04 +00:00
if not option in options:
if value:
return True
elif options[option] != value:
return True
return False
def create(self, options):
options.update({
'metadata': {
'name': self.name,
2019-05-09 09:08:36 +00:00
'namespace': self.namespace,
'labels': self.labels
2019-01-01 19:44:04 +00:00
}
})
self.api.put(
'namespaces/{}/checks/{}'.format(self.namespace, self.name),
options
)
def delete(self):
self.api.delete(
'namespaces/{}/checks/{}'.format(self.namespace, self.name)
)
def main():
fields = {
'name': { 'type': 'str', 'required': True },
'namespaces': { 'type': 'list', 'default': ['default'] },
2019-05-09 09:08:36 +00:00
'labels': { 'type': 'dict', 'default': {} },
2019-01-01 19:44:04 +00:00
'command': { 'type': 'str', 'required': True },
'handlers': { 'type': 'list', 'default': [] },
'subscriptions': { 'type': 'list', 'required': True },
'interval': { 'type': 'int', 'default': 60 },
'options': { 'type': 'dict', 'default': {} },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
}
module = AnsibleModule(argument_spec=fields)
changed = False
options = {
'command': module.params['command'],
'handlers': module.params['handlers'],
'subscriptions': module.params['subscriptions'],
'interval': module.params['interval'],
'publish': True
}
options.update(module.params['options'])
if 'cron' in options:
options.pop('interval')
api = SensuApi(
module.params['api_url'],
module.params['api_user'],
module.params['api_password']
)
api.auth()
for namespace in module.params['namespaces']:
check = SensuCheck(
api,
module.params['name'],
2019-05-09 09:08:36 +00:00
namespace,
module.params['labels']
)
check.get_data()
2019-01-01 19:44:04 +00:00
if module.params['state'] == 'present':
if not check.exist or check.has_changed(options):
check.create(options)
changed = True
elif check.exist:
check.delete()
changed = True
2019-01-01 19:44:04 +00:00
module.exit_json(changed=changed)
if __name__ == '__main__':
main()