Commit 237dec41 authored by Sigmund Augdal's avatar Sigmund Augdal

Added new subcommand noicy-nodes to find the hosts which as generated most events last N days

parent f40205f8
#!/usr/bin/env python3
import argparse
from collections import defaultdict
import getpass
import json
import os
import requests
import sys
import time
base_dir = os.path.dirname(os.path.realpath(__file__))
......@@ -118,6 +120,29 @@ def get_hosts():
print(url, host['name'])
def noicy_nodes(args):
events_from = time.time() - args.days * 24 * 3600
counts = defaultdict(int)
events = invoke('event.get', time_from=events_from,
selectHosts=['hostid', 'name'], value=1)
for event in events:
if 'hosts' in event:
if len(event['hosts']) == 1:
counts[event['hosts'][0]['name']] += 1
elif len(event['hosts']) > 1:
print("Warning: Event with multiple hosts")
print(event)
else:
print("Warning: Empty hosts array")
print(event)
else:
print("Warning: Event without hosts")
print(event)
for host, count in sorted(counts.items(), key=lambda x: x[1]):
print("{}: {}".format(host, count))
def parse_args():
parser = argparse.ArgumentParser('Zabbix cmd line client')
subparsers = parser.add_subparsers(help='Command', dest='command')
......@@ -142,6 +167,10 @@ def parse_args():
parser_get_hosts = subparsers.add_parser('get-hosts', help='Get hosts')
noicy = subparsers.add_parser('noicy-nodes', help="Find hosts with most events")
noicy.add_argument('--days', default=7, type=int,
help="query for events last DAYS days")
return parser.parse_args()
......@@ -161,6 +190,8 @@ def main():
return get_unreachable()
elif args.command == 'get-hosts':
return get_hosts()
elif args.command == 'noicy-nodes':
return noicy_nodes(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