Commit 2e22daaf authored by Sigmund Augdal's avatar Sigmund Augdal

Added subcommand to find empty host groups and templates

parent d7621af1
......@@ -206,6 +206,42 @@ def check_kind(args):
print(" - {host}".format(host=host))
def find_empty(args):
print("Empty host groups")
for hostgroup in invoke('hostgroup.get', selectHosts='count', selectTemplates='count'):
if hostgroup['hosts'] == 0 and hostgroup['templates'] == 0:
print(" - {group}".format(group=hostgroup['name']))
print("Unused templates")
templates = {}
for template in invoke('template.get', selectHosts='count', selectParentTemplates=['templateid']):
template['children'] = {t['templateid'] for t in template['parentTemplates']}
template['hosts'] = int(template['hosts'])
templates[template['templateid']] = template
def propagate(templateid):
template = templates[templateid]
for ct in template['children']:
templates[ct]['hosts'] = template['hosts']
propagate(ct)
for templateid, template in templates.items():
if template['hosts'] > 0:
propagate(templateid)
for template in templates.values():
if template['hosts'] == 0:
print(" - {template}".format(template=template['name']))
print("Hosts without items")
for host in invoke('host.get', selectItems='count', selectHttpTests='count'):
if host['items'] == 0 and host['httpTests'] == 0:
print(" - {host}".format(host=host['name']))
print("Templates without items")
for template in invoke('template.get', selectItems='count', selectHttpTests='count',
selectDiscoveries='count'):
if template['items'] == 0 and template['httpTests'] == 0 and template['discoveries'] == 0:
print(" - {template}".format(template=template['name']))
def parse_args():
parser = argparse.ArgumentParser('Zabbix cmd line client')
subparsers = parser.add_subparsers(help='Command', dest='command')
......@@ -241,6 +277,8 @@ def parse_args():
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")
find_empty = subparsers.add_parser('find-empty', help="Find empty host groups")
return parser.parse_args()
......@@ -264,6 +302,8 @@ def main():
return noicy_nodes(args)
elif args.command == 'check-kind':
return check_kind(args)
elif args.command == 'find-empty':
return find_empty(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