2,创建host,通过zabbix-api脚本
Posted pwcc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2,创建host,通过zabbix-api脚本相关的知识,希望对你有一定的参考价值。
1,创建zabbix api 脚本
[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]#
2,在同路径创建文本文件,文本内输入IP和hostname
[root@web3 zabbix]# cat create_host.txt 192.168.0.11,web1 192.168.0.12,web2 [root@web3 zabbix]#
3,执行脚本
[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]#
以上是关于2,创建host,通过zabbix-api脚本的主要内容,如果未能解决你的问题,请参考以下文章
通过python封装zabbix-api批量修改模板中触发器名称