Use of Python3--zabbixapi

Source: Internet
Author: User

Python3 some cases using the Zabbix API: Specifically, you can go to Zabbix official website to find API excuses, replace it.

#!/usr/bin/env python# -*- coding: utf-8 -*-import urllib.requestimport  jsonimport reurl =  ' http://xxxxxxxxxxxxxxxx/api_jsonrpc.php ' username =  ' Xxxxxxxxxxxxxxx ' password =  ' xxxxxx ' #  landing def requestjson (url, values):     data = json.dumps (values). Encode (' Utf-8 ')     req =  Urllib.request.Request (url, data, {' content-type ':  ' Application/json-rpc '})      response = urllib.request.urlopen (Req, data)     a =  Response.read (). Decode (encoding= ' Utf-8 ')     output = json.loads (a)      #    print output    try:         message = output[' Result ']    except:         message = output[' ERROR ' [' Data ']        print (message)          quit ()     return output[' result ']# #登陆的APIdef   Authenticate (Url, username, password):     values = {' jsonrpc ':  ' 2.0 ',               ' method ':  ' User.login ',               ' params ':  {                    ' user ': username,                    ' Password ': password               },                ' id ':  ' 0 '               }    idvalue =  The value of Requestjson (url, values)     return idvalue# auth auth =  Authenticate (Url, username, password) # #查询组ID  {' groupid ':  ' 8 ',  ' name ':  ' Switch '} Def groups (auth):    values = {          "JSONRPC":  "2.0",         "method":  "Hostgroup.get",          "params": {              "Output":  ["GroupID",  "name"],         },         ' auth ': auth,          ' id ':  ' 1 '     }    output =  Requestjson (url, values)      return output# b = groups (auth) # print (b) # #查询主机   {' HostID ':  ' 10108 ', def hosts (auth):    values = {          "Jsonrpc":  "2.0",         "method":  "Host.get",          "params": {              "Output":  ["GroupID",  "name"],              "Groupids":  "8",         },          ' auth ': auth,          ' id ':  ' 1 '     }    output = requestjson (url,  Values)     return outputhost = hosts (auth) host1 = []host2 =  []for i in&nbsP;range (Len (host)):     host1.append (host[i][' name ')     host2.append ( host[i][' HostID ') host3 = dict (Zip (host1, host2)) # #查询主机项目  {' key_ ':  ' Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ',  ' itemid ':  ' 26399 '}def item (auth):     values = {         "Jsonrpc":  "2.0",          "Method":  "Item.get",         " Params ": {            " output ":  [" Itemids ", " Key_ "],            " Hostids ":   "10108",        },          ' auth ': auth,         ' id ':  ' 1 '      }    output = requesTjson (url, values)     return output# print (item (AUTH)) # #查询项目的历史值     ' Lastvalue ':  ' -14760.0000 ' Def his (auth, itemids):     values =  {         "Jsonrpc":  "2.0",          "Method":  "Item.get",         "params":  {              "Output":  "Extend",              "History": 0,              "Itemids": itemids,              "SortField":  "Itemid",              "SortOrder":  "DESC",              "Limit":  1 &Nbsp;      },         ' auth ':  auth,          ' id ':  ' 1 '     }     output = requestjson (url, values)     return output# print ( His (auth,26399) # #查询触发项目值和监控项目   {' description ':  ' xxxxxxxxxxxxxxx ',  ' hostname ':  ' Xxxxxxxxxxxxxxx ',  ' items ':  [{' itemid ':  ' 26399 '}],  ' triggerid ':  ' 17030 '}def  Trigger (Auth, hostid):    values = {          "Jsonrpc":  "2.0",         "method":  "Trigger.get" ,         "params": {              "Output": [                  "DEscription ",            ],              "Filter": {                  "HostID": hostid,             },              "Selectitems":  ",            " SortField ": " hostname ",            " sortorder ": " DESC "        },          ' auth ': auth,         ' id ':  ' 1 '      }    output = requestjson (url, values)     return  output## #简单使用案例, can be traced, based on the trigger to find history. &Nbsp;       t1 = trigger (auth, host3[msg[' Content '])          t2 = []        t3  = []        for i in range (len (t1)):             t5 = t1[i][' Items '][0]   ##    ' items ':  [{' itemid ':  ' 26399 '}]             t6 = his (auth, t5[' itemid ')   ##   his (auth, 26399)             t2.append (t1[i][' description ')   # #监控项目描述             t3.append (Round ( Float (t6[0][' lastvalue '))  / 1000)   # #项目ID   value          t4 =&nbsP;dict (Zip (t2, t3))         t8 = []         for k in t4:             t7 = k +  ":"  +  "{}". Format (T4[k])  +  "DB"              t8.append (T7)          t9 =  "\ n". Join (T8)


This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1904261

Use of Python3--zabbixapi

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.