zabbix-2.0.8 quickly add filters using groups and key values (API actions)

Source: Internet
Author: User

Quickly add filters with the Zabbix API


Function Description: The script can quickly generate a filter using the group name and key contained in the host in the group
Usage Prerequisites: You need to set a graphic on key
Applicable version: Zabbix 2.0.8 (actual test), Theory support 2.0~2.4 series (not verified)
Operation Note: You need to fill in the script with the URL,USER,PASSWD, the group name, the key that has created the graph, the filter name, and Python to run

#!/usr/bin/env python# coding:utf-8 ' ####################### function:  The main feature of this script is to quickly add a filter #  version : v1.0# writer  : graysky# mail    :  [email protected]# date    : 2016-08-14###################### "" import jsonimport urllib2import mathimport sysclass api_work:     def __init__ (SELF,&NBSP;URL,&NBSP;USER,&NBSP;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 (), &NBSP;&NBSP;&NBSP;&NBsp;                          "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": &NBsp;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) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;   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) &Nbsp;                    num += 1                     lines += 1    zabbix_url =  " http://zabbixurl/api_jsonrpc.php "    zabbix_user = " Zabbixuser "     zabbix_passwd =  "ZABBIXPASSWD"     api_init = api_work ( ZABBIX_URL,&NBSP;ZABBIX_USER,&NBSP;ZABBIX_PASSWD)     keyname =  ' xxxx '      groupname =  ' xxxx '     screenname =  ' xxxxx '      createscreen (Screenname,groupname,keyname)


Due to the limited level of personal programming, if you find a better method or contain a bug, please contact me immediately.


Welcome you to Exchange and reprint, reprint also please retain the source,>.<


This article is from the "Busy Workaholic" blog, make sure to keep this source http://graysky.blog.51cto.com/3776928/1839310

zabbix-2.0.8 quickly add filters using groups and key values (API actions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.