Skip to content
Snippets Groups Projects
Commit 26728296 authored by Erik Reid's avatar Erik Reid
Browse files

test env skeleton

parent 55bf5f85
No related branches found
No related tags found
No related merge requests found
import json
import logging
import re
import click
import jsonschema
from brian_polling_manager import sensu
logger = logging.getLogger(__name__)
_DEFAULT_CONFIG = {
'inventory': [
'http://test-inventory-provider01.geant.org:8080',
'http://test-inventory-provider02.geant.org:8080'
],
'sensu': {
'api-base': [
'https://test-poller-sensu-agent01.geant.org:8080',
'https://test-poller-sensu-agent02.geant.org:8080',
'https://test-poller-sensu-agent03.geant.org:8080'
],
'token': '696a815c-607e-4090-81de-58988c83033e'
}
}
CONFIG_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'sensu': {
'type': 'object',
'properties': {
'api-base': {
'type': 'array',
'items': {'type': 'string'},
'minItems': 1
},
'token': {'type': 'string'}
}
}
},
'type': 'object',
'properties': {
'inventory': {
'type': 'array',
'items': {'type': 'string'},
'minItems': 1
},
'sensu': {'$ref': '#/definitions/sensu'}
},
'required': ['inventory', 'sensu'],
'additionalProperties': False
}
def _validate_config(ctx, param, value):
"""
loads, validates and returns configuration parameters
:param ctx:
:param param:
:param value: filename (string)
:return: a dict containing configuration parameters
"""
if value is None:
config = _DEFAULT_CONFIG
else:
try:
with open(value) as f:
config = json.loads(f.read())
except (json.JSONDecodeError, jsonschema.ValidationError) as e:
raise click.BadParameter(str(e))
try:
jsonschema.validate(config, CONFIG_SCHEMA)
except jsonschema.ValidationError as e:
raise click.BadParameter(str(e))
return config
@click.command()
@click.option(
'--config',
default=None,
type=click.STRING,
callback=_validate_config,
help='configuration filename')
def main(config):
def _categorize_ifc_check(check):
name = check['metadata']['name']
m = re.match(r'^check-([^-]+\.geant\.net)-(.+)$', name)
if not m:
return None
return {
'hostname': m.group(1),
'interface': m.group(2),
'check': check
}
ifc_checks = {}
for check in filter(None, map(_categorize_ifc_check, sensu.load_all_checks(config['sensu']))):
host_checks = ifc_checks.setdefault(check['hostname'], {})
if check['interface'] in host_checks:
other_check = host_checks[check['interface']]
other_name = other_check['metadata']['name']
this_name = check['metadata']['name']
logger.warning(f'found both {this_name} and {other_name}')
host_checks[check['interface']] = check['check']
for host, interfaces in ifc_checks.items():
print(host)
for ifc, check in interfaces.items():
name = check['metadata']['name']
print(f'\t{ifc}: {name}')
if __name__ == '__main__':
main()
\ No newline at end of file
"""
Sensu api utilities
"""
import random
import requests
def load_all_checks(params):
url = random.choice(params['api-base'])
# url = params['api-base'][0]
r = requests.get(
f'{url}/api/core/v2/namespaces/default/checks',
headers={
'Authorization': f'Key {params["token"]}',
'Accepts': 'application/json',
})
r.raise_for_status()
for check in r.json():
yield check
#
# def snmp_counter_checks(all_checks):
# for c in all_checks:
#
# assert c['command']
# items = c['command'].split()
# if len(items) < 6:
# continue
# if not items.pop(0).endswith('counter2influx.sh'):
# continue
#
# yield {
# 'hostname': items[3],
# 'interface_name': items[4],
# 'interval': c['interval'],
# 'name': c['metadata']['name']
# }
#
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment