码迷,mamicode.com
首页 > Windows程序 > 详细

zabbix_api 接口调用

时间:2021-05-24 17:05:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:%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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!