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
| #!/usr/bin/python
#coding:utf-8
from zabbix_api import ZabbixAPI
import json
server="http://172.16.206.130/zabbix"
username="Admin"
password="zabbix"
zapi = ZabbixAPI(server=server,path="",log_level=0)
zapi.login(username,password)
info=[{"name": "ECS0001", "ip": "172.16.206.128", "bandwidthout": "1M", "internet_ip": "", "team": "Tank", "id": "i-23zmvm8uz"},{"name
": "ECS0007","ip": "172.16.206.130", "bandwidthout": "1M", "internet_ip": "120.54.6.78", "team": "Tesla", "id": "i-23s02ihno"}]
macro_list = []
for I in info:
visible_name = I['name']
band = I['bandwidthout']
# print visible_name,band
if band != "0M":
##计算出网络带宽宏的值
network_threshold = str(int(int(band.strip('M'))*0.8*1000)) + 'K'
# print network_threshold
##通过主机可见名查找hostid
hostinfo=zapi.host.get({"output":"extend","filter":{"name":visible_name}})
hostid=hostinfo[0]['hostid']
##通过hostid获取主机的所有宏
host_macro_get=zapi.usermacro.get({"output":"extend","hostids":hostid})
if len(host_macro_get) ==0:
host_macro_create=zapi.usermacro.create({"hostid":hostid,"macro":"{$NETWORK_THRESHOLD}","value":network_threshold})
print "host %s has no macro,but create a new macro" % visible_name
else:
for X in host_macro_get:
macro = X['macro']
macro_list.append(macro)
if "{$NETWORK_THRESHOLD}" not in macro_list:
##通过hostid创建主机宏
host_macro_create=zapi.usermacro.create({"hostid":hostid,"macro":"{$NETWORK_THRESHOLD}","value":network_threshold})
print "host $s add a macro" % visible_name
else:
##获取主机上{$NETWORK_THRESHOLD}宏的值
for Y in host_macro_get:
if Y['macro'] == '{$NETWORK_THRESHOLD}':
current_network_threshold_value = Y['value']
macro_id = Y['hostmacroid']
if network_threshold == current_network_threshold_value:
print "The current macro value on host %s is latest" %visible_name
else:
##更新NETWORK_THRESHOLD宏的值
host_macro_update=zapi.usermacro.update({"hostmacroid":macro_id,"value":network_threshold})
print "macro value on host %s updated" %visible_name
else:
print "The host %s has no internet_ip" % visible_name
|