feat: add labels in checks

This commit is contained in:
Adrien Waksberg 2019-05-09 11:08:36 +02:00
parent 8bcc51a08c
commit 910b491ba7
5 changed files with 32 additions and 7 deletions

View file

@ -5,6 +5,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased]
- feat: add labels in checks
- fix: create roles before users
- feat: add a debug mode with sensu_no_log variable
- feat: add mutators

View file

@ -94,6 +94,8 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
- name: load
namespace:
- default
labels:
criticity: high
command: /usr/bin/load -w 2 -c 5
handlers:
- mailer

View file

@ -4,21 +4,37 @@ from ansible.module_utils.basic import *
from ansible.module_utils.sensu_api import *
class SensuCheck:
def __init__(self, api, name, namespace):
def __init__(self, api, name, namespace, labels):
self.api = api
self.name = name
self.namespace = namespace
self.labels = labels
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
self.options = data
self.options.pop('metadata')
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():
if old_label in new_labels and new_labels[old_label] != old_value:
continue
return True
return False
def has_changed(self, options):
for option, value in self.options.iteritems():
data = self.get_data()
if self.labels_has_changed(self.labels, data['metadata'].get('labels')):
return True
for option, value in data:
if not option in options:
if value:
return True
@ -31,7 +47,8 @@ class SensuCheck:
options.update({
'metadata': {
'name': self.name,
'namespace': self.namespace
'namespace': self.namespace,
'labels': self.labels
}
})
@ -50,6 +67,7 @@ def main():
fields = {
'name': { 'type': 'str', 'required': True },
'namespaces': { 'type': 'list', 'default': ['default'] },
'labels': { 'type': 'dict', 'default': {} },
'command': { 'type': 'str', 'required': True },
'handlers': { 'type': 'list', 'default': [] },
'subscriptions': { 'type': 'list', 'required': True },
@ -85,7 +103,8 @@ def main():
check = SensuCheck(
api,
module.params['name'],
namespace
namespace,
module.params['labels']
)
check.get_data()

View file

@ -141,6 +141,7 @@
sensugo_check:
name: '{{ item.name }}'
namespaces: '{{ item.namespaces|default(["default"]) }}'
labels: '{{ item.labels|default({}) }}'
command: '{{ item.command }}'
handlers: '{{ item.handlers|default([]) }}'
subscriptions: '{{ item.subscriptions }}'

View file

@ -51,6 +51,8 @@
sensu_checks:
- name: ping
command: ping -c 1 127.0.0.1
labels:
ping_warning: '200'
subscriptions:
- linux
namespaces: