首页 > 编程语言 > 详细

3, python get_zabbix_proxy_id 脚本

时间:2020-06-12 22:21:24      阅读:55      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python
#coding:utf-8

import requests
import json

url = "http://192.168.99.14/zabbix/api_jsonrpc.php"
headers = {"Content-Type": "application/json-rpc"}

def login_zabbix():
    data = {
        "jsonrpc":"2.0",
        "method":"user.login",
        "id":1,
        "auth":None,
        "params": {
                "user": "Admin",
                "password": "zabbix"
        }
    }

    r = requests.post(url, data = json.dumps(data), headers = headers)

    _content = json.loads(r.content)
    return _content[‘result‘]

def create_hostgoup():
    _auth = login_zabbix()
    data = {
        "jsonrpc": "2.0",
        "method": "hostgroup.create",
        "params": {
            "name": "reboot"
        },
        "auth": _auth,
        "id": 1
    }

    r = requests.post(url, data=json.dumps(data), headers=headers)
    _content = json.loads(r.content)
    print _content

def get_goupid():
    _auth = login_zabbix()
    data = {
        "jsonrpc": "2.0",
        "method": "hostgroup.get",
        "params": {
            "output": "extend",
            "filter": {
                "name": [
                    "reboot"
                ]
            }
        },
        "auth": _auth,
        "id": 1
    }
    r = requests.post(url, data=json.dumps(data), headers=headers)
    _content = json.loads(r.content)
    return _content[‘result‘][0][‘groupid‘]


def get_templateid():
    _auth = login_zabbix()
    data = {
        "jsonrpc": "2.0",
        "method": "template.get",
        "params": {
            "output": "extend",
            "filter": {
                "host": [
                    "Template OS Linux",
                ]
            }
        },
        "auth": _auth,
        "id": 1
    }
    r = requests.post(url, data=json.dumps(data), headers=headers)
    _content = json.loads(r.content)
    return _content[‘result‘][0][‘templateid‘]

def create_host(_host_list):
    _auth = login_zabbix()
    _groupid = get_goupid()
    _templdateid = get_templateid()


    for _host in _host_list:
        data = {
            "jsonrpc": "2.0",
            "method": "host.create",
            "params": {
                "host": _host[‘host‘],
                "interfaces": [
                    {
                        "type": 1,
                        "main": 1,
                        "useip": 1,
                        "ip": _host[‘ip‘],
                        "dns": "",
                        "port": "10050"
                    }
                ],
                "groups": [
                    {
                        "groupid": _groupid
                    }
                ],
                "templates": [
                    {
                        "templateid": _templdateid
                    }
                ],
                "inventory_mode": 0,
                "inventory": {
                    "macaddress_a": "01234",
                    "macaddress_b": "56768"
                }
            },
            "auth": _auth,
            "id": 1
        }
        r = requests.post(url, data=json.dumps(data), headers=headers)
        _content = json.loads(r.content)
        print _content[‘result‘][‘hostids‘]


if __name__ == "__main__":
    _host_list = [
        {"ip": "192.168.99.10", "host": "reboot-devops-02"},
        {"ip": "192.168.99.11", "host": "reboot-ms-web-01"},
        {"ip": "192.168.99.12", "host": "reboot-ms-web-02"},
        {"ip": "192.168.99.13", "host": "reboot-ms-web-03"}
    ]
    create_host(_host_list)

  

3, python get_zabbix_proxy_id 脚本

原文:https://www.cnblogs.com/k8s-pod/p/13110341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!