码迷,mamicode.com
首页 > Windows程序 > 详细

zabbix-2.0.8利用组和键值快速添加筛选(api操作)

时间:2016-08-16 22:15:06      阅读:581      评论:0      收藏:0      [点我收藏+]

标签:zabbix快速操作

利用zabbix api快速添加筛选


功能描述:该脚本可以利用组名和组中host中含有的key快速生成筛选
使用前提:需要对key设置图形
适用版本:zabbix 2.0.8(实际测试),理论支持2.0~2.4系列(未验证)
操作注意:需要将url,user,passwd,组名称,已经创建图形的key,筛选名称填入脚本,python运行即可

#!/usr/bin/env python
# coding:utf-8
‘‘‘
######################
# Function: 此脚本的功能主要是快速添加筛选
# Version : V1.0
# Writer  : graysky
# Mail    : 714810243@qq.com
# Date    : 2016-08-14
######################
‘‘‘

import json
import urllib2
import math
import sys

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 CheckScreenExist(self,screen_name):
        screenexist_json = {"jsonrpc": "2.0",
                            "method": "screen.exists",
                            "params": {"name": screen_name},
                            "auth": self.zabbix_login(),
                            "id": 0}
        return self.json_work(screenexist_json)

    def Get_Groupinfo_by_Groupname(self,groupname):
        groupinfo_group = {}
        groupinfo_json = {"jsonrpc": "2.0",
                        "method": "hostgroup.get",
                        "params": {"output": "extend",
                                   "filter": {"name": groupname}},
                        "auth": self.zabbix_login(),
                        "id": 0}
        for groupinfo in self.json_work(groupinfo_json):
            groupinfo_group[groupinfo["groupid"]] = groupinfo["name"]
        return groupinfo_group

    def Get_Itemsinfo_by_GroupidKeyname(self,groupid,keyname):
        itemsinfo_group = {}
        itemsinfo_json = {"jsonrpc": "2.0",
                          "method": "item.get",
                          "params": {"output": "extend",
                                     "groupids": groupid,
                                     "search": {"key_": keyname},},
                          "auth": self.zabbix_login(),
                          "id": 0}
        for itemsinfo in self.json_work(itemsinfo_json):
            itemsinfo_group[itemsinfo["itemid"]] = itemsinfo["name"]
        return itemsinfo_group

    def Get_Graphid_by_itemsid(self,itemsid):
        graphid_group = {}
        graphid_json = {"jsonrpc": "2.0",
                        "method": "graphitem.get",
                        "params": {"output": "extend",
                                   "expandData": 1,
                                   "itemids": itemsid},
                        "auth": self.zabbix_login(),
                        "id": 0}
        for graphinfo in self.json_work(graphid_json):
            graphid_group[graphinfo["graphid"]] = graphinfo["key_"]
        return graphid_group

    def Create_Screen(self,screenname,linesnum):
        screen_json = {"jsonrpc": "2.0",
                       "method": "screen.create",
                       "params": {"name": screenname,
                                  "hsize": 2, "vsize": linesnum},
                       "auth": self.zabbix_login(),
                       "id": 0}
        createresponse = self.json_work(screen_json)
        return createresponse["screenids"]

    def Create_Screen_include(self,screen_id,resource_id,xnum,ynum):
        createscreeninclude_json = {"jsonrpc": "2.0",
                                    "method": "screenitem.create",
                                    "params": {"screenid": screen_id,
                                               "resourcetype": 0,
                                               "resourceid": resource_id,
                                               "width": 500,
                                               "height": 100,
                                               "rowspan": 0,
                                               "colspan": 0,
                                               "x": xnum,
                                               "y": ynum},
                                    "auth": self.login_authid,
                                    "id": 0}
        self.json_work(createscreeninclude_json)

if __name__ == "__main__":
    def getgraphid(groupname,keyname):
        graphidgroup = []
        groupid_group = api_init.Get_Groupinfo_by_Groupname(groupname)
        for groupid in groupid_group.keys():
            itemsinfo_group = api_init.Get_Itemsinfo_by_GroupidKeyname(groupid,keyname)
            for itemsid in itemsinfo_group.keys():
                for graphid in api_init.Get_Graphid_by_itemsid(itemsid).keys():
                    graphidgroup.append(graphid)
        return graphidgroup

    def createscreen(screenname,group_name,key_name):
        if not api_init.CheckScreenExist(screenname):
            graphidgroup = getgraphid(group_name,key_name)
            linenum = math.ceil(len(graphidgroup) / 2.0)
            screenid_group = api_init.Create_Screen(screenname,linenum)
            for screenid in screenid_group:
                lines = 0
                num = 0
                while lines < linenum:
                    api_init.Create_Screen_include(int(screenid), int(graphidgroup[num]), lines, 0)
                    num += 1
                    api_init.Create_Screen_include(int(screenid), int(graphidgroup[num]), lines, 1)
                    num += 1
                    lines += 1

    zabbix_url = "http://zabbixurl/api_jsonrpc.php"
    zabbix_user = "zabbixuser"
    zabbix_passwd = "zabbixpasswd"

    api_init = api_work(zabbix_url, zabbix_user, zabbix_passwd)
    keyname = ‘xxxx‘
    groupname = ‘xxxx‘
    screenname = ‘xxxxx‘

    createscreen(screenname,groupname,keyname)


由于个人编程水平有限,如果各位发现更好的方法或是含有bug请及时联系我


                                                                    欢迎各位多多交流和转载,转载还请保留出处,>.<


本文出自 “闲着的工作狂” 博客,请务必保留此出处http://graysky.blog.51cto.com/3776928/1839310

zabbix-2.0.8利用组和键值快速添加筛选(api操作)

标签:zabbix快速操作

原文地址:http://graysky.blog.51cto.com/3776928/1839310

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!