feat: add hooks

This commit is contained in:
Adrien Waksberg 2020-08-09 08:02:10 +02:00 committed by Adrien Waksberg
parent 44ce4e6002
commit b7ab98837a
7 changed files with 135 additions and 0 deletions

View file

@ -5,6 +5,10 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased]
### Feat
- add hooks
### Fixed
- add full python3 support

View file

@ -130,6 +130,16 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
timeout: 10
```
* `sensu_hooks` - array with the hook definitions
```
- name: restart-apache
namespace:
- default
command: /etc/init.d/apache2 restart
timeout: 10
```
* `sensu_filters` - array with the filter definitions
```

View file

@ -54,3 +54,4 @@ sensu_filters: []
sensu_users: []
sensu_cluster_roles: []
sensu_mutators: []
sensu_hooks: []

95
library/sensugo_hook.py Normal file
View file

@ -0,0 +1,95 @@
#!/usr/bin/python
from ansible.module_utils.basic import *
from ansible.module_utils.sensu_api import *
class SensuHook:
def __init__(self, api, name, namespace):
self.api = api
self.name = name
self.namespace = namespace
self.exists = False
def get_data(self):
status_code, data = self.api.get('namespaces/{}/hooks/{}'.format(self.namespace, self.name))
if status_code == 200:
self.exists = True
return data
return {}
def has_changed(self, options):
data = self.get_data()
data.pop('metadata')
for option, value in data.items():
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,
'namespace': self.namespace
}
})
self.api.put(
'namespaces/{}/hooks/{}'.format(self.namespace, self.name),
options
)
def delete(self):
self.api.delete(
'namespaces/{}/hooks/{}'.format(self.namespace, self.name)
)
def main():
fields = {
'name': { 'type': 'str', 'required': True },
'namespaces': { 'type': 'list', 'default': ['default'] },
'command': { 'type': 'str', 'required': True },
'timeout': { 'type': 'int', 'default': 10 },
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
'api_user': { 'type': 'str', 'default': 'admin' },
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!', 'no_log': True },
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
}
module = AnsibleModule(argument_spec=fields)
changed = False
options = {
'command': module.params['command'],
'timeout': module.params['timeout']
}
api = SensuApi(
module.params['api_url'],
module.params['api_user'],
module.params['api_password']
)
api.auth()
for namespace in module.params['namespaces']:
hook = SensuHook(
api,
module.params['name'],
namespace
)
hook.get_data()
if module.params['state'] == 'present':
if not hook.exists or hook.has_changed(options):
hook.create(options)
changed = True
elif hook.exists:
hook.delete()
changed = True
module.exit_json(changed=changed)
if __name__ == '__main__':
main()

View file

@ -52,6 +52,12 @@
namespaces:
- production
- dev
sensu_hooks:
- name: restart-apache
command: /etc/init.d/apache2 restart
namespaces:
- production
- dev
sensu_checks:
- name: ping
command: ping -c 1 127.0.0.1

View file

@ -58,3 +58,7 @@ def test_sensu_check(host):
cmd = host.run('sensuctl filter list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('state_changed.*\\s+allow\\s+\\(event.check.occurrences == 1\\)', cmd.stdout)
cmd = host.run('sensuctl hook list --namespace %s' % namespace)
assert cmd.succeeded
assert re.search('restart-apache.*\\s+/etc/init.d/apache2 restart\\s+10\\s+false', cmd.stdout)

View file

@ -162,6 +162,21 @@
run_once: true
tags: sensu
- name: manage hooks
sensugo_hook:
name: '{{ item.name }}'
namespaces: '{{ item.namespaces|default(["default"]) }}'
command: '{{ item.command }}'
timeout: '{{ item.timeout|default(10) }}'
api_url: '{{ sensu_api_url }}'
api_user: '{{ sensu_api_user }}'
api_password: '{{ sensu_api_password }}'
loop: '{{ sensu_hooks }}'
loop_control:
label: '{{ item.name }}'
run_once: true
tags: sensu
- name: manage checks
sensugo_check:
name: '{{ item.name }}'