Detailed use of PYTHON3-ZABBIXAPI

Source: Internet
Author: User
This article details the use of PYTHON3-ZABBIXAPI

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 Re url = ' Http://xxxxxxxxxxxxxxxx/api_ jsonrpc.php ' username = ' xxxxxxxxxxxxxxx ' password = ' xxxxxx ' # login def requestjson (URL, values): data = Json.dumps (values ). Encode (' utf-8 ') req = urllib.request.Request (URL, data, {' Content-type ': ' APPLICATION/JSON-RPC '}) Response = Urlli B.request.urlopen (req, data) A = Response.read (). Decode (encoding= ' utf-8 ') output = Json.loads (a) # print OUTPU        T 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 = Requestjson (URL, values) return Idvalue # auth Value auth = authentiCate (URL, username, password) # #查询组ID {' GroupID ': ' 8 ', ' name ': ' Switch '}def groups (auth): values = {"Jsonrpc": "2.0", "Method": "Hostgroup.get", "params": {"Output": ["GroupID", "Name"],}, ' A Uth ': auth, ' id ': ' 1 '} output = Requestjson (URL, values) return output # b = Groups (auth) # print (b) # #查询 Host {' 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 output host = hosts (auth) host1 = []HOST2 = []for i in 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", "M     Ethod ":" Item.get ",   "params": {"Output": ["Itemids", "Key_"], "hostids": "10108",}, ' auth ': auth, ' ID ': ' 1 '} output = Requestjson (URL, values) return output # Print (item (auth)) # #查询项目的历史值 ' lastvalue ': '-1            4760.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}, ' auth ': auth, ' id ': ' 1 '} output = Reque Stjson (URL, values) return output # print (his (auth,26399)) # #查询触发项目值和监控项目 {' description ': ' xxxxxxxxxxxxxxx ', ' Hostnam        E ': ' 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": "Hostn Ame "," SortOrder ":" DESC "}, ' auth ': auth, ' id ': ' 1 '} output = Requestjson (URL, val        UES) Return output # # #简单使用案例, can be traced, based on the trigger to find history. 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.appen D (t1[i][' description ') # #监控项目描述 T3.append (Round (float (t6[0][' lastvalue '))/+) # #项目ID value T4 = dic T (Zip (t2, t3)) T8 = [] for k in t4:t7 = k + ":" + "{}". Format (T4[k]) + "DB" T8.append (t7) T9 = "\ n". Join (T8)
Related Article

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.