Skip to content

Commit 498834c

Browse files
author
zhanghe5
committed
add zabbix_host_massupdate.py
1 parent afbe808 commit 498834c

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

zabbix_host_massupdate.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env python
2+
# coding:utf8
3+
4+
'''
5+
Created on 03.06.2015
6+
'''
7+
8+
import optparse
9+
import sys
10+
import traceback
11+
from getpass import getpass
12+
from core import ZabbixAPI
13+
import sys
14+
15+
16+
def get_options():
17+
usage = "usage: %prog [options]"
18+
OptionParser = optparse.OptionParser
19+
parser = OptionParser(usage)
20+
21+
parser.add_option("-s", "--server", action="store", type="string", \
22+
dest="server", help="(REQUIRED)Zabbix Server URL.")
23+
parser.add_option("-u", "--username", action="store", type="string", \
24+
dest="username", help="(REQUIRED)Username (Will prompt if not given).")
25+
parser.add_option("-p", "--password", action="store", type="string", \
26+
dest="password", help="(REQUIRED)Password (Will prompt if not given).")
27+
parser.add_option("-H", "--hostname", action="store", type="string", \
28+
dest="hostname", help="(REQUIRED)hostname for hosts.If you want to use multiple groups,separate them with a ','.")
29+
parser.add_option("-t", "--templates", action="store", type="string", \
30+
dest="templates", default="",
31+
help="Templates to replace the currently linked templates.If you want to use multiple groups,separate them with a ','.")
32+
parser.add_option("-g", "--groups", action="store", type="string", \
33+
dest="groups", default="",
34+
help="Host groups to replace the current host groups the host belongs to.If you want to use multiple groups,separate them with a ','.")
35+
parser.add_option("-f", "--file", dest="filename", \
36+
metavar="FILE",
37+
help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>4space<groups>4space<templates>")
38+
39+
options, args = parser.parse_args()
40+
41+
if not options.server:
42+
options.server = raw_input('server http:')
43+
44+
if not options.username:
45+
options.username = raw_input('Username:')
46+
47+
if not options.password:
48+
options.password = getpass()
49+
50+
return options, args
51+
52+
53+
def errmsg(msg):
54+
sys.stderr.write(msg + "\n")
55+
sys.exit(-1)
56+
57+
58+
def get_list(lists, key):
59+
l = []
60+
for i in lists:
61+
l.append(i[key])
62+
return l
63+
64+
65+
def get_api(**kwargs):
66+
print kwargs
67+
try:
68+
print zapi.host.massupdate(kwargs)
69+
except Exception as e:
70+
print str(e)
71+
72+
73+
if __name__ == "__main__":
74+
options, args = get_options()
75+
76+
zapi = ZabbixAPI(options.server, options.username, options.password)
77+
78+
file = options.filename
79+
80+
if file:
81+
with open(file, "r") as f:
82+
content = f.readlines()
83+
for i in content:
84+
l = i.split(" ")
85+
hostname = l[0].rstrip()
86+
hosts = hostname.split(',')
87+
hostid = zapi.host.get({"filter": {"host": hosts}})
88+
try:
89+
templates = l[2].rstrip()
90+
templates_id = zapi.template.get({"output": "templateid", "filter": {"host": templates.split(",")}})
91+
except:
92+
templates_id = ""
93+
try:
94+
groups = l[1].rstrip()
95+
groups_id = zapi.hostgroup.get({"output": "groupid", "filter": {"name": groups.split(",")}})
96+
except:
97+
groups_id = ""
98+
if templates_id and groups_id:
99+
get_api(templates=templates_id, groups=groups_id, hosts=hostid)
100+
elif templates_id and not groups_id:
101+
get_api(templates=templates_id, hosts=hostid)
102+
elif not templates_id and groups_id:
103+
get_api(groups=groups_id, hosts=hostid)
104+
else:
105+
templates = options.templates
106+
hostname = options.hostname
107+
groups = options.groups
108+
hosts = hostname.split(',')
109+
hostid = zapi.host.get({"filter": {"host": hosts}})
110+
if templates:
111+
templates_id = zapi.template.get({"output": "templateid", "filter": {"host": templates.split(",")}})
112+
else:
113+
templates_id = ""
114+
if groups:
115+
groups_id = zapi.hostgroup.get({"output": "groupid", "filter": {"name": groups.split(",")}})
116+
else:
117+
groups_id = ""
118+
if templates_id and groups_id:
119+
get_api(templates=templates_id, groups=groups_id, hosts=hostid)
120+
elif templates_id and not groups_id:
121+
get_api(templates=templates_id, hosts=hostid)
122+
elif not templates_id and groups_id:
123+
get_api(groups=groups_id, hosts=hostid)

0 commit comments

Comments
 (0)