2,创建host,通过zabbix-api脚本
2021-03-05 16:29
标签:name result dict user url 创建 group sts output 1,创建zabbix api 脚本 2,在同路径创建文本文件,文本内输入IP和hostname 3,执行脚本 2,创建host,通过zabbix-api脚本 标签:name result dict user url 创建 group sts output 原文地址:https://www.cnblogs.com/k8s-pod/p/12892200.html[root@web3 zabbix]# cat test.py
#! /usr/bin/python3
# -*- coding:utf-8 -*-
import requests
import json
url = "http://192.168.0.11/zabbix/api_jsonrpc.php"
headers = {"Content-Type": "application/json-rpc"}
def login_zabbix():
data = {
"jsonrpc":"2.0",
"method":"user.login",
"id":1,
"params": {
"user": "Admin",
"password": "zabbix"
}
}
dict_to_str = json.dumps(data)
r = requests.post(url, data = dict_to_str, headers = headers)
content_str = r.text
str_to_dict = json.loads(content_str)
return str_to_dict[‘result‘]
def create_hostgoup():
_auth = login_zabbix()
data = {
"jsonrpc": "2.0",
"method": "hostgroup.create",
"params": {
"name": "web1_group1"
},
"auth": _auth,
"id": 1
}
dict_to_str = json.dumps(data)
r = requests.post(url, data=dict_to_str, headers=headers)
content_str = r.text
str_to_dict = json.loads(content_str)
print(str_to_dict)
def get_groupid():
_auth = login_zabbix()
data = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"web1_group1"
]
}
},
"auth": _auth,
"id": 1
}
dict_to_str = json.dumps(data)
r = requests.post(url, data=dict_to_str, headers=headers)
content_str = r.text
str_to_dict = json.loads(content_str)
return str_to_dict[‘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
}
dict_to_str = json.dumps(data)
r = requests.post(url, data=dict_to_str, headers=headers)
content_str = r.text
str_to_dict = json.loads(content_str)
return str_to_dict[‘result‘][0][‘templateid‘]
def create_host(dict_list):
create_hostgoup()
_auth = login_zabbix()
_groupid = get_groupid()
_templdateid = get_templateid()
for _host in dict_list:
data = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": _host[‘hostname‘],
"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
}
dict_to_str = json.dumps(data)
r = requests.post(url, data=dict_to_str, headers=headers)
content_str = r.text
str_to_dict = json.loads(content_str)
print(str_to_dict[‘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"}
# ]
hostiplist = {}
dict_list = []
host_ip_name_list = [‘ip‘,‘hostname‘]
with open(‘create_host.txt‘,‘r‘) as f:
for line in f:
#print(line,type(line))
list_a = line.split()
#print(list_a,type(list_a))
list_a_to_str = ‘,‘.join(list_a)
#print(list_a_to_str,type(list_a_to_str))
a = list_a_to_str.split(‘,‘)
#print(a,type(a))
list_to_dict = dict(zip(host_ip_name_list,a))
print(list_to_dict)
dict_list.append(list_to_dict)
create_host(dict_list)
[root@web3 zabbix]#
[root@web3 zabbix]# cat create_host.txt
192.168.0.11,web1
192.168.0.12,web2
[root@web3 zabbix]#
[root@web3 zabbix]# ./test.py
{‘ip‘: ‘192.168.0.11‘, ‘hostname‘: ‘web1‘}
{‘ip‘: ‘192.168.0.12‘, ‘hostname‘: ‘web2‘}
{‘jsonrpc‘: ‘2.0‘, ‘result‘: {‘groupids‘: [‘9‘]}, ‘id‘: 1}
[‘10108‘]
[‘10109‘]
[root@web3 zabbix]#
上一篇:创建进程API
文章标题:2,创建host,通过zabbix-api脚本
文章链接:http://soscw.com/index.php/essay/60532.html