设为首页 收藏本站
查看: 1388|回复: 0

[经验分享] Zabbix-2.0.8利用excel表格快速添加检测项目和触发器

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-1 10:37:49 | 显示全部楼层 |阅读模式
    在日常的监控运维中,对于添加检测项目和触发器基本上都是使用前台和鼠标来创建,如果是在模板中添加或机器数量较少的情况下,工作量还是可以接受。如果监控服务器上k级别,且大部分需要定制的时候,手动添加将是一个让人抓狂的工作。        最近公司有监控新需求啦,新需求啦 j_0004.jpg 。每次听到这个,我都很郁闷,公司服务器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[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)




欢迎各位多多讨论


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-266102-1-1.html 上篇帖子: zabbix-2.0.8日常巡检-检测项目状态 下篇帖子: zabbix 安装 weathermap excel表格 触发器 检测 项目
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表