Commit d191ce22 authored by Sigmund Augdal's avatar Sigmund Augdal

Added new subcommand check-kind to find hosts in zabbix that are not found in kind

parent 237dec41
......@@ -143,6 +143,65 @@ def noicy_nodes(args):
print("{}: {}".format(host, count))
def query_kind(session, var, query):
resp = session.get('https://kind.uninett.no/api/utstyr_tjeneste.json',
params={var: query})
if resp.status_code == 404:
return "Not Found"
if resp.status_code >= 400:
return "Error"
data = resp.json()
status = []
for utstyr in data['utstyr']:
if utstyr['status'] == 'Installert':
return 'OK'
status.append(utstyr['status'])
for tjeneste in data['tjenester']:
if tjeneste['status'] == 'Produksjon':
return 'OK'
status.append(tjeneste['status'])
return status[0]
def check_kind(args):
session = requests.Session()
hosts = invoke('host.get', selectMacros=['macro', 'value'],
selectGroups=['name'])
statuses = defaultdict(list)
groupstatuses = defaultdict(lambda: defaultdict(list))
for host in hosts:
kind_query = host['host']
kind_var = 'navn'
for macro in host['macros']:
if macro['macro'] == '{$KINDID}':
kind_query = macro['value']
kind_var = 'id'
status = query_kind(session, kind_var, kind_query)
displayName = host['name']
for hostgroup in host['groups']:
groupstatuses[status][hostgroup['name']].append(displayName)
statuses[status].append(displayName)
if args.by_host_group:
for status, groups in groupstatuses.items():
if status == 'OK' and not args.include_ok:
continue
print("Kind status: {status}".format(status=status))
for group, hosts in groups.items():
print(" * {group}".format(group=group))
for host in hosts:
print(" - {host}".format(host=host))
else:
for status, hosts in statuses.items():
if status == 'OK' and not args.include_ok:
continue
print("Kind status: {status} - {hosts}".format(status=status, hosts=len(hosts)))
if args.stat:
continue
for host in hosts:
print(" - {host}".format(host=host))
def parse_args():
parser = argparse.ArgumentParser('Zabbix cmd line client')
subparsers = parser.add_subparsers(help='Command', dest='command')
......@@ -171,6 +230,11 @@ def parse_args():
noicy.add_argument('--days', default=7, type=int,
help="query for events last DAYS days")
kind = subparsers.add_parser('check-kind', help="Find hosts that are not registerted in kind")
kind.add_argument('--include-ok', action='store_true', help="Include hosts that are considered properly registered")
kind.add_argument('--stat', action='store_true', help="Show only counts, not list of hosts")
kind.add_argument('--by-host-group', action='store_true', help="List nodes by host group")
return parser.parse_args()
......@@ -192,6 +256,8 @@ def main():
return get_hosts()
elif args.command == 'noicy-nodes':
return noicy_nodes(args)
elif args.command == 'check-kind':
return check_kind(args)
else:
raise NotImplementedError('Unknown command: {command}'.format(command=args.command))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment