Skip to content

Commit 2f463bc

Browse files
author
zhanghe5
committed
add zabbix_host_massadd.py zabbix_host_massremove.py
1 parent 584b474 commit 2f463bc

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed

zabbix_host_massadd.py

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

zabbix_host_massremove.py

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

0 commit comments

Comments
 (0)