Skip to content

Commit 584b474

Browse files
author
zhanghe5
committed
add hostgroup template add/remove api
1 parent 6dae40f commit 584b474

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

zabbix_hostgroup_massadd.py

Whitespace-only changes.

zabbix_hostgroup_massremove.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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("-g","--groups",action="store",type="string",\
28+
dest="groups",default="",help="Host groups to add the host to.If you want to use multiple groups,separate them with a ','.")
29+
parser.add_option("-f","--file",dest="filename",\
30+
metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>4space<groups>")
31+
32+
options,args = parser.parse_args()
33+
34+
if not options.server:
35+
options.server = raw_input('server http:')
36+
37+
if not options.username:
38+
options.username = raw_input('Username:')
39+
40+
if not options.password:
41+
options.password = getpass()
42+
43+
return options, args
44+
45+
def errmsg(msg):
46+
sys.stderr.write(msg + "\n")
47+
sys.exit(-1)
48+
49+
def get_list(lists, key):
50+
l = []
51+
for i in lists:
52+
l.append(i[key])
53+
return l
54+
55+
if __name__ == "__main__":
56+
options, args = get_options()
57+
58+
zapi = ZabbixAPI(options.server,options.username, options.password)
59+
60+
file = options.filename
61+
62+
if file:
63+
with open(file,"r") as f:
64+
content = f.readlines()
65+
for i in content:
66+
l = i.split(" ")
67+
hostname = l[0].rstrip()
68+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
69+
groups = l[1].rstrip()
70+
groups_id = zapi.hostgroup.get({"output": "groupid","filter": {"name":groups.split(",")}})
71+
groupids = get_list(groups_id, "groupid")
72+
try:
73+
print zapi.hostgroup.massremove({"groupids":groupids,"hostids":hostid})
74+
except Exception as e:
75+
print str(e)
76+
else:
77+
groups = options.groups
78+
hostname = options.hostname
79+
groups_id = zapi.hostgroup.get({"output": "groupid","filter": {"name":groups.split(",")}})
80+
groupids = get_list(groups_id, "groupid")
81+
hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
82+
try:
83+
print zapi.hostgroup.massremove({"groupids":groupids,"hostids":hostid})
84+
except Exception as e:
85+
print str(e)

zabbix_template_massadd.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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="the templates to be updated.If you want to use multiple groups,separate them with a ','.")
29+
parser.add_option("-f","--file",dest="filename",\
30+
metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>4space<templates>")
31+
32+
options,args = parser.parse_args()
33+
34+
if not options.server:
35+
options.server = raw_input('server http:')
36+
37+
if not options.username:
38+
options.username = raw_input('Username:')
39+
40+
if not options.password:
41+
options.password = getpass()
42+
43+
return options, args
44+
45+
def errmsg(msg):
46+
sys.stderr.write(msg + "\n")
47+
sys.exit(-1)
48+
49+
if __name__ == "__main__":
50+
options, args = get_options()
51+
52+
zapi = ZabbixAPI(options.server,options.username, options.password)
53+
54+
file = options.filename
55+
56+
if file:
57+
with open(file,"r") as f:
58+
content = f.readlines()
59+
for i in content:
60+
l = i.split(" ")
61+
hostname = l[0].rstrip()
62+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
63+
templates = l[1].rstrip()
64+
templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}})
65+
try:
66+
print zapi.template.massadd({"templates":templates_id,"hosts":hostid})
67+
except Exception as e:
68+
print str(e)
69+
else:
70+
templates = options.templates
71+
hostname = options.hostname
72+
templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}})
73+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
74+
try:
75+
print zapi.template.massadd({"templates":templates_id,"hosts":hostid})
76+
except Exception as e:
77+
print str(e)

zabbix_template_massremove.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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="Host template to be updated.If you want to use multiple groups,separate them with a ','.")
29+
parser.add_option("-f","--file",dest="filename",\
30+
metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>4space<templates>")
31+
32+
options,args = parser.parse_args()
33+
34+
if not options.server:
35+
options.server = raw_input('server http:')
36+
37+
if not options.username:
38+
options.username = raw_input('Username:')
39+
40+
if not options.password:
41+
options.password = getpass()
42+
43+
return options, args
44+
45+
def errmsg(msg):
46+
sys.stderr.write(msg + "\n")
47+
sys.exit(-1)
48+
49+
def get_list(lists, key):
50+
l = []
51+
for i in lists:
52+
l.append(i[key])
53+
return l
54+
55+
if __name__ == "__main__":
56+
options, args = get_options()
57+
58+
zapi = ZabbixAPI(options.server,options.username, options.password)
59+
60+
file = options.filename
61+
62+
if file:
63+
with open(file,"r") as f:
64+
content = f.readlines()
65+
for i in content:
66+
l = i.split(" ")
67+
hostname = l[0].rstrip()
68+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
69+
templates = l[1].rstrip()
70+
templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}})
71+
templateids = get_list(templates_id, "templateid")
72+
try:
73+
print zapi.template.massremove({"templateids":templateids,"hostids":hostid})
74+
except Exception as e:
75+
print str(e)
76+
else:
77+
templates = options.templates
78+
hostname = options.hostname
79+
templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}})
80+
templateids = get_list(templates_id, "templateid")
81+
hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
82+
try:
83+
print zapi.template.massremove({"templateids":templateids,"hostids":hostid})
84+
except Exception as e:
85+
print str(e)

0 commit comments

Comments
 (0)