jgfh 发表于 2016-9-1 10:37:49

Zabbix-2.0.8利用excel表格快速添加检测项目和触发器

    在日常的监控运维中,对于添加检测项目和触发器基本上都是使用前台和鼠标来创建,如果是在模板中添加或机器数量较少的情况下,工作量还是可以接受。如果监控服务器上k级别,且大部分需要定制的时候,手动添加将是一个让人抓狂的工作。      最近公司有监控新需求啦,新需求啦。每次听到这个,我都很郁闷,公司服务器1500+,因绝大部分的新需求都是需要定制的,而且因为历史原因,模板的套用也是问题(有大量复用导致定制话模板无法加入同一台服务器),每次添加都是一个痛苦的过程,而本文就是在这个情况下诞生的。
前期准备:
    1.收集需求:对监控的内容进行收集规整,并以一下形式进行保存(请使用excel表格)

IP
应用集项目键值探测方式保存数据格式触发器名称触发表达式报警等级










注:探测方式和报错数据格式请参照zabbix官方api。
    2.预装环境:python 模块xlrd
    3.修改脚本部分内容:

      1.EXCELFILE :excel文件的存放地址和文件名

      2.SHEETINFO:excel的表格名(可以存放不同的任务)

      3.zabbix的url,用户,密码

    4.运行以下脚本即可


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)
      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["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["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["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": ,
                              "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) < 2 :
                  if API_INIT.checkapplicationexist(int(HOSTID),applicationname):
                        APPLICATIONINFOGROUP =API_INIT.getapplicationidbyhostidname(int(HOSTID))
                        for APPLICATIONID in APPLICATIONINFOGROUP.keys():
                            if APPLICATIONINFOGROUP == 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))
                        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
            APPLICATIONNAME = lineinfo
            ITEMSNAME = lineinfo
            KEYINFO = lineinfo
            TYPEINFO = lineinfo
            VALUETYPE = lineinfo
            print HOSTIP,ITEMSNAME
            AddHostItems(zabbix_url,zabbix_user,zabbix_passwd,HOSTIP, APPLICATIONNAME, ITEMSNAME, KEYINFO, TYPEINFO, VALUETYPE)
            #trigger info
            TRIGGERDESC = lineinfo
            TRIGGEREXP = lineinfo
            TRIGGERPRRIORITY = lineinfo
            AddTrigger(zabbix_url,zabbix_user,zabbix_passwd,TRIGGERDESC,TRIGGEREXP,TRIGGERPRRIORITY)
            headlinenum += 1
    EXCELFILE = '/Users/graysky/Desktop/items.xlsx'
    SHEETINFO = 'xxx'
    ProvinceWork(EXCELFILE,SHEETINFO)





欢迎各位多多讨论

页: [1]
查看完整版本: Zabbix-2.0.8利用excel表格快速添加检测项目和触发器