-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix-discovered-chart.py
85 lines (72 loc) · 3.84 KB
/
zabbix-discovered-chart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
from pyzabbix import ZabbixAPI
COLOR_LIST = ['4D4D4D', '5DA5DA', 'FAA43A', '60BD68',
'F17CB0', 'B2912F', 'B276B2', 'DECF3F', 'F15854']
GRAPH_TYPE = {'normal': 0,
'stacked': 1,
'pie': 2,
'exploded': 3}
def get_color(i):
return COLOR_LIST[i % len(COLOR_LIST)]
def query(args):
zapi = ZabbixAPI(args.host)
zapi.login(args.login, args.password)
hosts = zapi.host.get(hostids=[args.hostid])
if not hosts:
print ("Provided host ID is invalid")
itemprototypes = zapi.itemprototype.get(itemids=args.itemprototypeid)
if not itemprototypes:
print ("Provided item prototypes ID is invalid")
print("Identifier host {0}".format(hosts[0]['name']))
print("Identifier item prototype {0}".format(itemprototypes[0]['name']))
items = zapi.item.get(hostids=[args.hostid],
selectItemDiscovery='extend')
discovered_items = [x for x in items
if x['itemDiscovery'] and
x['itemDiscovery'].get('parent_itemid') == str(args.itemprototypeid)]
if not discovered_items:
print("Unable found items for hostid = {0} ({1}) ".format(hosts[0]['name'], args.hostid) +
"and parent_itemid = {0}".format(args.itemprototypeid))
print("Are you sure that the discovery rule is already created items?")
discovery_ids = [x['itemDiscovery']['parent_itemid'] for x in items if x['itemDiscovery']]
discovered_items = zapi.itemprototype.get(itemids=list(discovery_ids))
print ("Found only following discovery ID for {0}:".format(hosts[0]['name']))
for ditem in discovered_items:
print("\t{id} - {name}".format(id=ditem['itemid'], name=ditem['name']))
return
gitems = [{"itemid": item['itemid'], "color": get_color(i)}
for i, item in enumerate(discovered_items)]
graph = zapi.graph.get(hostids=[args.hostid], search={'name': args.name})
if graph:
print("Going to update graph ID {0}".format(graph[0]['graphid']))
res = zapi.graph.update(graphid=graph[0]['graphid'],
width=args.width,
height=args.height,
gitems=gitems,
graphtype=GRAPH_TYPE[args.graphtype])
print("Graph update success" if res else "Something fail")
return
print("Going to create graph")
res = zapi.graph.create(name=args.name,
width=args.width,
height=args.height,
gitems=gitems)
print("Graph create success" if res else "Something fail")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Add grouped graph for LDD items")
parser.add_argument('host',
help='URI of zabbix server eg. http://tacjana.jawne.info.pl/zabbix/')
parser.add_argument('login', help='username of zabbix server user')
parser.add_argument('password', help='password for zabbix server user')
parser.add_argument('--name', help='Name of added graph', default="Memory usage per user")
parser.add_argument('itemprototypeid', type=int,
help="ID of the item prototype from which the items has been created")
parser.add_argument('hostid', type=int,
help="ID of the host for which items the graph has been created")
parser.add_argument('--height', type=int, default=200,
help="--Height of the graph in pixels.")
parser.add_argument('--width', type=int, default=900,
help="--Width of the graph in pixels.")
parser.add_argument('--graphtype', choices=GRAPH_TYPE.keys(), default='normal',
help="Graph's layout type (default: normal)")
query(parser.parse_args())