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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
| #!/usr/bin/env python
# coding:utf-8
'''
######################
# Function: 此脚本的功能主要是快速添加检测项目和触发器
# Version : 1.0.0
# Writer : greysky
# Mail : 714810243@qq.com
# Date : 2016-08-14
######################
'''
import json
import urllib2
import xlrd
class api_work:
def __init__(self, url, user, passwd):
self.url = url
self.user = user
self.passwd = passwd
self.login_authid = self.zabbix_login()
def json_work(self, work_json):
zabbix_header = {"Content-Type": "application/json"}
self.work_json = work_json
used_json = json.dumps(self.work_json)
used_json_reques = urllib2.Request(self.url, used_json)
for key in zabbix_header:
used_json_reques.add_header(key, zabbix_header[key])
try:
used_json_result = urllib2.urlopen(used_json_reques)
except Exception:
print "Get failed"
else:
used_json_respones = json.loads(used_json_result.read())
used_json_group = used_json_respones['result']
used_json_result.close()
return used_json_group
def zabbix_login(self):
login_json = {"jsonrpc": "2.0",
"method": "user.login",
"params": {"user": self.user, "password": self.passwd},
"id": 0}
login_authid = self.json_work(login_json)
return login_authid
def checkhostipexist(self,hostname):
hostip_exist_json = {"jsonrpc": "2.0",
"method": "host.exists",
"params": {"host": hostname},
"auth": self.login_authid,
"id": 0}
return self.json_work(hostip_exist_json)
def gethostidbyhostip(self,hostname):
hostinfo_group = {}
hostinfo_json = {"jsonrpc": "2.0",
"method": "host.get",
"params": {"output": "extend",
"filter": {
"host": hostname
}
},
"auth": self.login_authid,
"id": 0}
for hostinfo in self.json_work(hostinfo_json):
hostinfo_group[hostinfo["hostid"]] = hostinfo["available"]
return hostinfo_group
def gethostinterfaceidbyhostid(self,hostid):
hostinterfaceinfo = {}
hostinterface_json = {"jsonrpc": "2.0",
"method": "hostinterface.get",
"params": {
"output": "extend",
"hostids": hostid
},
"auth": self.login_authid,
"id": 0}
for hostinferface in self.json_work(hostinterface_json):
hostinterfaceinfo[hostinferface["hostid"]] = hostinferface["interfaceid"]
return hostinterfaceinfo
def checkapplicationexist(self,hostid,applicationname):
applicationexist_json = {"jsonrpc": "2.0",
"method": "application.exists",
"params": {
"hostid": hostid,
"name": applicationname
},
"auth": self.login_authid,
"id": 1}
return self.json_work(applicationexist_json)
def createapplicationbyhostidname(self,hostid,applicationname):
createapplication_json = {"jsonrpc": "2.0",
"method": "application.create",
"params": {
"name": applicationname,
"hostid": hostid
},
"auth": self.login_authid,
"id": 0}
return self.json_work(createapplication_json)
def getapplicationidbyhostidname(self,hostid):
applicationinfo_group = {}
getapplicationid_json = {"jsonrpc": "2.0",
"method": "application.get",
"params": {"output": "extend",
"hostids": hostid,
},
"auth": self.login_authid,
"id": 0}
for applicationinfo in self.json_work(getapplicationid_json):
applicationinfo_group[applicationinfo["applicationid"]] = applicationinfo["name"]
return applicationinfo_group
def checkitemsbyhostidkey(self,hostid,keyinfo):
itemsinfo_json = {"jsonrpc": "2.0",
"method": "item.exists",
"params": {
"hostid": hostid,
"key_": keyinfo
},
"auth": self.login_authid,
"id": 0}
return self.json_work(itemsinfo_json)
def createitemsbyitemsnamekey(self,itemsname,interfaceid,hostid,typeinfo,vlauetypeinfo,keyinfo,applicationid):
createitems_json = {"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": itemsname,
"key_": keyinfo,
"hostid": hostid,
"type": typeinfo,
"value_type": vlauetypeinfo,
"interfaceid": interfaceid,
"applications": [applicationid],
"delay": 30},
"auth": self.login_authid,
"id": 0}
return self.json_work(createitems_json)
def checktrigger(self,expdes):
triggerinfo_json = {"jsonrpc": "2.0",
"method": "trigger.exists",
"params": {
"expression": expdes},
"auth": self.login_authid,
"id": 0}
return self.json_work(triggerinfo_json)
def createtrigger(self,triggerdesc,triggerexp,priority):
createtrigger_json = {"jsonrpc": "2.0",
"method": "trigger.create",
"params": {
"description": triggerdesc,
"expression": triggerexp,
"priority": priority
},
"auth": self.login_authid,
"id": 0}
return self.json_work(createtrigger_json)
if __name__ == "__main__":
def AddHostItems(ZABBIX_URL, ZABBIX_USER, ZABBIX_PASSWD,hostname,applicationname,itemsname,keyinfo,typeinfo,valuetypeinfo):
API_INIT = api_work(ZABBIX_URL, ZABBIX_USER, ZABBIX_PASSWD)
if API_INIT.checkhostipexist(hostname):
HOSTINFO_GROUP = API_INIT.gethostidbyhostip(hostname)
for HOSTID in HOSTINFO_GROUP.keys():
if int(HOSTINFO_GROUP[HOSTID]) < 2 :
if API_INIT.checkapplicationexist(int(HOSTID),applicationname):
APPLICATIONINFOGROUP = API_INIT.getapplicationidbyhostidname(int(HOSTID))
for APPLICATIONID in APPLICATIONINFOGROUP.keys():
if APPLICATIONINFOGROUP[APPLICATIONID] == applicationname:
APPLICATION_ID = APPLICATIONID
else:
APPLICATIONINFOGROUP = API_INIT.createapplicationbyhostidname(int(HOSTID),applicationname)
for APPLICATIONID in APPLICATIONINFOGROUP["applicationids"]:
APPLICATION_ID = APPLICATIONID
if API_INIT.checkitemsbyhostidkey(HOSTID,keyinfo):
print "%s-%s already exists."%(HOSTID,keyinfo)
else:
INTERFACEID = int(API_INIT.gethostinterfaceidbyhostid(HOSTID)[HOSTID])
return API_INIT.createitemsbyitemsnamekey(itemsname,INTERFACEID,HOSTID,typeinfo,valuetypeinfo,keyinfo,APPLICATION_ID)["itemids"]
else:
print "Host is unavailable"
def AddTrigger(ZABBIX_URL, ZABBIX_USER, ZABBIX_PASSWD,triggerdesc,triggerexp,triggerpriority):
API_INIT = api_work(ZABBIX_URL, ZABBIX_USER, ZABBIX_PASSWD)
if API_INIT.checktrigger(triggerexp):
print "Trigger already exist"
else:
API_INIT.createtrigger(triggerdesc,triggerexp,triggerpriority)
def ProvinceWork(excelfile,provinceinfo):
if provinceinfo == "xxx":
zabbix_url = "http://xxxx/zabbix/api_jsonrpc.php"
zabbix_user = "Admin"
zabbix_passwd = "xxxxx"
itemsdata = xlrd.open_workbook(excelfile)
itemstable = itemsdata.sheet_by_name(provinceinfo)
headlinenum = 1
while headlinenum < itemstable.nrows:
lineinfo = itemstable.row_values(headlinenum)
#items info
HOSTIP = lineinfo[0]
APPLICATIONNAME = lineinfo[1]
ITEMSNAME = lineinfo[2]
KEYINFO = lineinfo[3]
TYPEINFO = lineinfo[4]
VALUETYPE = lineinfo[5]
print HOSTIP,ITEMSNAME
AddHostItems(zabbix_url,zabbix_user,zabbix_passwd,HOSTIP, APPLICATIONNAME, ITEMSNAME, KEYINFO, TYPEINFO, VALUETYPE)
#trigger info
TRIGGERDESC = lineinfo[6]
TRIGGEREXP = lineinfo[7]
TRIGGERPRRIORITY = lineinfo[8]
AddTrigger(zabbix_url,zabbix_user,zabbix_passwd,TRIGGERDESC,TRIGGEREXP,TRIGGERPRRIORITY)
headlinenum += 1
EXCELFILE = '/Users/graysky/Desktop/items.xlsx'
SHEETINFO = 'xxx'
ProvinceWork(EXCELFILE,SHEETINFO)
|