python通过zabbix的api添加主机
  uvM09mQNI0hF 2023年11月19日 21 0
创建主机:
# /usr/bin/env python3
# -*- coding:utf-8 -*-
import json
import requests
import sys

class ZabbixTools:
    #post_headers = {'Content-Type': 'application/json'}
    def __init__(self):
        self.url = 'http://47.74.181.146/zabbix/api_jsonrpc.php'
        self.header = {"Content-Type":"application/json"}
    #登录认证
    def user_login(self):
        post_data = {
            "jsonrpc" : "2.0",
            "method" : "user.login",
            "params" : {
                "user" : "xxxxxxxxx",
                "password" : "xxxxxx"
                    },
            "id" : 1
        }
        response = requests.post(self.url, data = json.dumps(post_data), headers = self.header)
        response = json.loads(response.text)
        self.authID = response["result"]
        #print(self.authID)
        return self.authID

    def host_get(self, hostName, hostIp):
        post_data = {
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": [
                    "hostid",
                    "host"
                ],
                "selectInterfaces": [
                    "interfaceid",
                    "ip"
                ]
            },
            "id": 1,
            "auth": self.user_login(),  #这是第一步获取的身份验证令牌
        }
        response = requests.post(self.url, data = json.dumps(post_data), headers = self.header)
        response = json.loads(response.text)
        #print(response)
        host_list = []
        ip_list = []
        for i in response['result']:
            host_list.append(i['host'])
            ip_list.append(i['interfaces'][0]['ip'])
        if hostName in host_list or hostIp in ip_list:
            print("hostname or ip is exist")
            #print(hostName, hostIp)
            sys.exit()
        return host_list
    
    def host_create(self, hostName, hostIp):

        post_data = {
            "jsonrpc": "2.0",
            "method": "host.create",
            "params": {
                "host": hostName,
                "interfaces": [
                    {
                        "type": 1,
                        "main": 1,
                        "useip": 1,
                        "ip": hostIp,
                        "dns": "",
                        "port": "10050"
                    }
                ],
                "groups": [
                    {
                        "groupid": "2"  #填写第3步获取的组ID  Linux servers组
                    }
                ],
                "templates": [
                    {
                        "templateid": "10001"#填写第4步获取的模板ID   Template OS Linux            
                        #"templateid": "10102",  #Template App SSH Service
                        #"templateid": "10186"   #Template Module ICMP Ping               
                    }
                ],
                "macros": [
                    {
                        "macro": "{$USER_ID}",
                        "value": "123321"
                    }
                ],
                "inventory_mode": 0,
                "inventory": {
                "macaddress_a": "01234",
                "macaddress_b": "56768"
                }
            },
            "auth": self.user_login(),
            "id": 1
        }
        
        response = requests.post(self.url, data = json.dumps(post_data), headers = self.header)
        response = json.loads(response.text)
        return response['result']['hostids'] 
    def num_args(*args):
        if len(args) != 2:
            print("请输入2个参数:hostname + hostIP")
            print("eg: python zabbix_host_create.py \"test\" \"111.111.111.111\"")
            sys.exit()

if __name__ == "__main__":
    # 实例化ZabbixTools对象   
    add = ZabbixTools()
    #add.host_get("test4545", "133.133.133.156")
    #add.host_create("test4545", "133.133.133.156")
    #add.host_get(sys.argv[1])
    add.num_args()
    add.host_get(sys.argv[1], sys.argv[2])
    add.host_create(sys.argv[1], sys.argv[2])




获取token
# -*- coding:utf-8 -*-
import json
import requests

url = 'http://zabbix.51ake.net/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
    "jsonrpc" : "2.0",
    "method" : "user.login",
    "params" : {
        "user" : "xxxxxxxx",
        "password" : "xxxxxxxxx"
    },
    "id" : 1
}

response = requests.post(url, data = json.dumps(post_data), headers = post_headers)
response = json.loads(response.text)
authID = response["result"]
print(authID)


#ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
#print(ret.text)
#retjson = json.loads(ret.text)
#retjson = retjson["result"]
#print(retjson)
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
  SzVG4h2uFmuP   2023年12月06日   15   0   0 监控zabbix
  SzVG4h2uFmuP   2023年12月12日   21   0   0 zabbix
  SzVG4h2uFmuP   2023年12月09日   36   0   0 zabbix
uvM09mQNI0hF