通过Zabbix API 为主机添加监控模板
脚本内容如下;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
#-*- coding:utf8 -*-
import json,sys,argparse
from zabbix_api import ZabbixAPI
server = "http://172.16.206.128/zabbix"
username = "Admin"
password = "zabbix"
zapi = ZabbixAPI(server=server, path="", log_level=0)
zapi.login(username, password)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", help="host name")
parser.add_argument("-t", "--templates", help="template name")
# 解析所传入的参数
args = parser.parse_args()
if not args.host:
args.host = raw_input('host: ')
if not args.templates:
args.templates = raw_input('templates: ')
return args
def get_host_id(host):
get_host_id = zapi.host.get(
{
"output": "hostid",
"filter": {
"host":host.split(",")
}
}
)
host_id = []
host_id.append( for I in get_host_id])
return host_id
def get_templates_id(templates):
templates_id = zapi.template.get(
{
"output": "templateid",
"filter": {
"host":templates.split(",")
}
}
)
return templates_id
def template_massadd(template_id,host_id):
template_add = zapi.template.massadd(
{
"templates": template_id,
"hosts": host_id
}
)
return "host add template success"
if __name__ == "__main__":
args = get_args()
host_id = get_host_id(args.host)
template_id = get_templates_id(args.templates)
if len(host_id) == len(args.host.split(',')):
if len(template_id) == len(args.templates.split(',')):
print template_massadd(template_id,host_id)
else:
print "template not exist"
else:
print "host not exist"
脚本需要传递两个参数:host(主机名)和templates(模板名称),主机可以为多个,用逗号“,”隔开,模板也可以为多个,同样用逗号“,”隔开。
用法:
1
2
python template_massadd.py --host="abc,client" --"templates"="Template OS Linux,Templates App MySQL"
host add template success
页:
[1]