创建主机:
# /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)
python通过zabbix的api添加主机
原创
©著作权归作者所有:来自51CTO博客作者doubf的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章