zabbix_api 接口调用

2021-05-27 22:01

阅读:429

标签:%s   get   ram   pid   print   python   接口调用   http   json   

import requests
from  requests import request
import json
from collections import OrderedDict
url = ‘https://zabbix-xn.test.bestpay.net/api_jsonrpc.php‘
headers = {"Content-Type": "application/json-rpc"}
def log_zabbix():
    data = {

        "jsonrpc": "2.0",
        "method": "user.login",
        "id": 1,
        "params": {
            "user": "quemengqiao",
            "password": "Password6!"
        }
    }
    dict_to_str = json.dumps(data)
    res = requests.post(url,data = dict_to_str, headers=headers)
    content_str = res.text
    print(content_str)
    str_to_dict = json.loads(content_str)
    print(str_to_dict)
    return str_to_dict[‘result‘]
print(log_zabbix())
def host_get_id():
    _auth = log_zabbix()
    print(_auth)
    data = {
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": ["host"]
            },
            "auth":"%s" % _auth,
            "id": 1
    }
    dict_to_str = json.dumps(data)
    res = requests.post(url,data=dict_to_str,headers=headers)
    content_str = res.text
    str_to_dict = json.loads(content_str)
    print(str_to_dict)
    host_id_ip=(str_to_dict[‘result‘])
    print((host_id_ip))
    aaa=json.dumps(host_id_ip)
    for i in  host_id_ip:
        ip_list ="172.17.244.168"
        ccc = dict(i)
        if ip_list == ccc[‘host‘]:
            print(ccc[‘hostid‘])
            print(ccc[‘host‘])
            return ccc[‘hostid‘]
print(host_get_id())

def group_add():
    _auth=log_zabbix()
    _host_id = host_get_id()
    print(_host_id)
    data = {
            "jsonrpc": "2.0",
            "method": "hostgroup.massadd",
            "params": {
                "groups":[
                    {
                    "groupid":"18",
                }
                ],
                "hosts":[
                    {
                    "hostid":"%s" %_host_id
                    }
                ]
            },
            "auth":"%s" %_auth,
            "id": 1
    }
    dict_to_str = json.dumps(data)
    res = requests.post(url, data=dict_to_str, headers=headers)
    content_str = res.text
    str_to_dict = json.loads(content_str)
    host_id_ip = (str_to_dict[‘result‘])
    print(host_id_ip)
group_add()

  

zabbix_api 接口调用

标签:%s   get   ram   pid   print   python   接口调用   http   json   

原文地址:https://www.cnblogs.com/quemengqio/p/14788440.html


评论


亲,登录后才可以留言!