Zabbix-2.0.8利用excel表格快速添加检测项目和触发器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.运行以下脚本即可
#!/usr/bin/env python # coding:utf-8 ‘‘‘ ###################### # Function: 此脚本的功能主要是快速添加检测项目和触发器 # Version : 1.0.0 # Writer : greysky # Mail : [email protected] # 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)
欢迎各位多多讨论
本文出自 “闲着的工作狂” 博客,请务必保留此出处http://graysky.blog.51cto.com/3776928/1844780
以上是关于Zabbix-2.0.8利用excel表格快速添加检测项目和触发器的主要内容,如果未能解决你的问题,请参考以下文章