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)

欢迎各位多多讨论

时间: 2024-08-10 09:02:40

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

C++中的MFC创建Excel表格和添加数据到Excel表格中的方法

方法1: CStdioFile  File; CString str; File.Open("D\\1.xls",CFile::modeCreate|CFile::modeReadWrite);//如果文件事先不存在的话,就需要CFile::modeCreate,否则就不需要 //关键字:地点 ,  处理类型,  事件开始时间 ,  事件结束时间 ,  处理人 ,  处理时间 ,  上报类型   ,事件录像名称 ,事件图片名称 , 备注 str.Format("%s%c%s%

利用Excel表格中的宏,轻松提取首字母

方法/步骤 1.启动Excel 2003(其它版本请仿照操作),打开相应的工作表:   2.执行“工具→宏→Visual Basic编辑器”命令(或者直接按“Alt+F11”组合键),进入Visual Basic编辑状态:   3.执行“插入→模块”命令,插入一个新模块.再双击插入的模块,进入模块代码编辑状态:   4.将代码输入其中: Function pinyin(p As String) As String i = Asc(p) Select Case i Case -20319 To -

用EXCEL做快速傅立葉轉換_FFT in Excel

转载来自:http://yufan-fansbook.blogspot.tw/2013/09/excel-fft-fast-fourier-transform02.html [Excel]-用EXCEL做快速傅立葉轉換_FFT in Excel(Fast Fourier Transform in Excel)_02 [Excel]-用EXCEL做快速傅立葉轉換_FFT in Excel(Fast Fourier Transform in Excel)_02 第二步:建立想要做快速傅立葉分析的信號

跨平台信息获取小工具第三版本(增加了继承、多线程、异常处理模块、excel表格内容剔除空格)

# coding=utf-8 import threadingimport paramikoimport osimport timeimport xlrdimport xlwtimport openpyxl all_row = []threads = [] class read_excel(object): def __init__(self, num): #threading.Thread.__init__(self) #self.threadID = threadID self.num =

python读取excel表格生成sql语句 第一版

由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd linux下 sudo pip install xlrd 主要是适用于db2数据库 excel 表结构 其中 number是不正确的字段类型 不知道同事为啥这么设置.这里程序里有纠错,这个程序就是将sql语句拼好. __author__ = 'c3t' # coding:utf-8 import xlr

怎样可以把excel表格转换成word文档

在处理一些文档时,有时会遇到需要将excel表格内容全部放到word文档中,通常只是需要excel的部分数据内容时,直接通过复制,然后粘贴到word文档中即可,但是如果需要将excel表格所以内容都转换成word,那么复制操作就比较麻烦了,那么怎样可以快速将excel表格转换成word文档呢? 通过文档的转换工具,可以将excel格式直接转为word文档,而且是将excel工作簿中的所有表格同时进行转换. 首先在转换器中选择文件转word的转换类型,通过这个选项可以把我们常见的excel,ppt

使用JXL对Excel表格进行简单的操作

前段时间由于项目的需求,要求使用JXL,做完之后做下记录,方便以后查看! 首先我们要先下载JXL的JAR包:http://download.csdn.net/detail/u013352832/7777047 将JXL.JAR 包导入到项目中即可直接只用! 直接上代码 使用JXL来操作Excel表格,我们应该首先判断一下Excel表格是否存在 1 File file = new File(user.getZkfp()); 2 // 判断zkfp.xls文件是否存在,不存在则创建zkfp.xls文

利用java反射机制实现读取excel表格中的数据

如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.List<Book>等,所以需要使用泛型机制去实现.下面会给出代码,可能会稍微复杂一点,但注释很清晰,希望大家耐心阅读. 在上代码之前简单说一下思路: 1.excel表格必须有表头,且表头中各列的值要与实体类的属性相同: 2.先读取表头信息,然后获取表头列数,接着确定需要使用的set方法的名称,并存到数

利用json生成excel表格

起因: 之前利用反射生成excel导出,这个组件本来挺好用的,结果,坑爹的本地研发没有问题,生产环境却有问题.不知道什么原因直接导致服务重启,还重新加载类,直接导致jvm的永久区内存溢出. 异常: java.lang.OutOfMemoryError: PermGen space 说明: Perm空间被占满.无法为新的class分配存储空间而引发的异常.这个异常以前是没有的,但是在Java反射大量使用的今天这个异常比较常见了.主要原因就是大量动态反射生成的类不断被加载,最终导致Perm区被占满.