Detailed description of the use of python3-zabbixapi

Source: Internet
Author: User
This article details the use of python3-zabbixapi This article details the use of python3-zabbixapi

Python3 uses zabbix api in some cases .. For details, go to the zabbix official website to find an API excuse. just replace it.

#! /Usr/bin/env python #-*-coding: UTF-8-*-import urllib. requestimport jsonimport re url =' http://xxxxxxxxxxxxxxxx/api_jsonrpc.php 'Username = 'xxxxxxxxxxxxxxxxxx' password = 'xxxxxx' # log on to 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'] handle T: message = output ['error'] ['Data'] print (message) quit () return output ['result'] # login 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) # Query group ID {'groupid': '8', 'name ': 'switch'} def groups (auth): values = {"js Onrpc ":" 2.0 "," method ":" hostgroup. get "," params ": {" output ": [" groupid "," name "],}, 'auth': auth, 'id ': '1'} output = requestJson (url, values) return output # B = groups (auth) # print (B) # Query host {'hostid': '123 ', def hosts (auth): values = {"maid": "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 )) # Query host project {'key _ ': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'itemid': '000000'} def item (auth): values = {"jsonrpc": "26399 ", "method": "item. get "," params ": {" output ": [" itemids "," key _ "]," hostids ":" 10108 ",}, 'auth': aut H, 'id': '1'} output = requestJson (url, values) return output # print (item (auth) # query the historical project value '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}, 'auth': auth, 'id': '1'} output = requestJson (url, values) return output # print (His (auth, 26399) # Query trigger item values and monitoring item {'description': 'xxxxxxxxxxxxxxxxxxxx', 'hostname': 'xxxxxxxxxxxxxxxx', 'items ': [{'itemid': '000000'}], 'triggerid': '000000'} def trigger (auth, hostid): values = {"jsonrpc": "26399 ", "method": "trigger. get "," params ": {" output ": [" description ",]," filter ": {" hostid ": hostid,}," selectItems ":"", "sortfield": "hostname", "sortorder": "DESC"}, 'auth ': auth, 'id': '1 '} Output = requestJson (url, values) return output ### simple use case, which can be used to check and find the history based on the trigger. T1 = trigger (auth, host3 [msg ['content']) t2 = [] t3 = [] for I in range (len (t1 )): t5 = t1 [I] ['items '] [0] # 'items': [{'itemid': '000000'}] t6 = his (auth, t5 ['itemid']) # his (auth, 26399) t2.append (t1 [I] ['description']) # monitoring project description t3.append (round (float (t6 [0] ['lastvalue'])/1000) # Project ID value t4 = dict (zip (t2, t3) t8 = [] for k in t4: t7 = k + ":" + "{}". format (t4 [k]) + "db" t8.append (t7) t9 = "\ n ". join (t8)

The above is detailed description of the use of python3-zabbixapi details, more please pay attention to the first PHP community other related articles!

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.