feat: add labels in checks
This commit is contained in:
parent
8bcc51a08c
commit
910b491ba7
5 changed files with 32 additions and 7 deletions
|
@ -5,6 +5,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
- feat: add labels in checks
|
||||||
- fix: create roles before users
|
- fix: create roles before users
|
||||||
- feat: add a debug mode with sensu_no_log variable
|
- feat: add a debug mode with sensu_no_log variable
|
||||||
- feat: add mutators
|
- feat: add mutators
|
||||||
|
|
|
@ -94,6 +94,8 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
|
||||||
- name: load
|
- name: load
|
||||||
namespace:
|
namespace:
|
||||||
- default
|
- default
|
||||||
|
labels:
|
||||||
|
criticity: high
|
||||||
command: /usr/bin/load -w 2 -c 5
|
command: /usr/bin/load -w 2 -c 5
|
||||||
handlers:
|
handlers:
|
||||||
- mailer
|
- mailer
|
||||||
|
|
|
@ -4,21 +4,37 @@ from ansible.module_utils.basic import *
|
||||||
from ansible.module_utils.sensu_api import *
|
from ansible.module_utils.sensu_api import *
|
||||||
|
|
||||||
class SensuCheck:
|
class SensuCheck:
|
||||||
def __init__(self, api, name, namespace):
|
def __init__(self, api, name, namespace, labels):
|
||||||
self.api = api
|
self.api = api
|
||||||
self.name = name
|
self.name = name
|
||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
|
self.labels = labels
|
||||||
self.exist = False
|
self.exist = False
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
status_code, data = self.api.get('namespaces/{}/checks/{}'.format(self.namespace, self.name))
|
status_code, data = self.api.get('namespaces/{}/checks/{}'.format(self.namespace, self.name))
|
||||||
if status_code == 200:
|
if status_code == 200:
|
||||||
self.exist = True
|
self.exist = True
|
||||||
self.options = data
|
return data
|
||||||
self.options.pop('metadata')
|
|
||||||
|
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):
|
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 not option in options:
|
||||||
if value:
|
if value:
|
||||||
return True
|
return True
|
||||||
|
@ -31,7 +47,8 @@ class SensuCheck:
|
||||||
options.update({
|
options.update({
|
||||||
'metadata': {
|
'metadata': {
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'namespace': self.namespace
|
'namespace': self.namespace,
|
||||||
|
'labels': self.labels
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -50,6 +67,7 @@ def main():
|
||||||
fields = {
|
fields = {
|
||||||
'name': { 'type': 'str', 'required': True },
|
'name': { 'type': 'str', 'required': True },
|
||||||
'namespaces': { 'type': 'list', 'default': ['default'] },
|
'namespaces': { 'type': 'list', 'default': ['default'] },
|
||||||
|
'labels': { 'type': 'dict', 'default': {} },
|
||||||
'command': { 'type': 'str', 'required': True },
|
'command': { 'type': 'str', 'required': True },
|
||||||
'handlers': { 'type': 'list', 'default': [] },
|
'handlers': { 'type': 'list', 'default': [] },
|
||||||
'subscriptions': { 'type': 'list', 'required': True },
|
'subscriptions': { 'type': 'list', 'required': True },
|
||||||
|
@ -85,7 +103,8 @@ def main():
|
||||||
check = SensuCheck(
|
check = SensuCheck(
|
||||||
api,
|
api,
|
||||||
module.params['name'],
|
module.params['name'],
|
||||||
namespace
|
namespace,
|
||||||
|
module.params['labels']
|
||||||
)
|
)
|
||||||
check.get_data()
|
check.get_data()
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,7 @@
|
||||||
sensugo_check:
|
sensugo_check:
|
||||||
name: '{{ item.name }}'
|
name: '{{ item.name }}'
|
||||||
namespaces: '{{ item.namespaces|default(["default"]) }}'
|
namespaces: '{{ item.namespaces|default(["default"]) }}'
|
||||||
|
labels: '{{ item.labels|default({}) }}'
|
||||||
command: '{{ item.command }}'
|
command: '{{ item.command }}'
|
||||||
handlers: '{{ item.handlers|default([]) }}'
|
handlers: '{{ item.handlers|default([]) }}'
|
||||||
subscriptions: '{{ item.subscriptions }}'
|
subscriptions: '{{ item.subscriptions }}'
|
||||||
|
|
|
@ -51,6 +51,8 @@
|
||||||
sensu_checks:
|
sensu_checks:
|
||||||
- name: ping
|
- name: ping
|
||||||
command: ping -c 1 127.0.0.1
|
command: ping -c 1 127.0.0.1
|
||||||
|
labels:
|
||||||
|
ping_warning: '200'
|
||||||
subscriptions:
|
subscriptions:
|
||||||
- linux
|
- linux
|
||||||
namespaces:
|
namespaces:
|
||||||
|
|
Loading…
Reference in a new issue