Skip to content

Commit 5e4a024

Browse files
author
zhanghe5
committed
add zabbix_hostInterface_updata.py
1 parent 48273c6 commit 5e4a024

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

zabbix_hostInterface_updata.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python
2+
#coding:utf8
3+
4+
'''
5+
Created on 27.10.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("--ip",action="store",type="string",\
28+
dest="ip",default="",help="""(REQUIRED)IP address used by the interface.
29+
Can be empty if the connection is made via DNS.""")
30+
parser.add_option("--dns",action="store",type="string",\
31+
dest="dns",default="",help="""(REQUIRED)DNS name used by the interface.
32+
Can be empty if the connection is made via IP. """)
33+
parser.add_option("--main",action="store",type="int",\
34+
dest="main",default="1",help="""(REQUIRED,Default=1)Whether the interface is used as default on the host. Only one interface of some type can be set as default on a host.
35+
Possible values are:
36+
0 - not default;
37+
1 - default.""")
38+
parser.add_option("--port",action="store",type="string",\
39+
dest="port",default="10050",help="""(REQUIRED,Default=10050)Port number used by the interface. Can contain user macros.""")
40+
parser.add_option("--type",action="store",type="int",\
41+
dest="type",default="1",help="""(REQUIRED,Default=1)Interface type.
42+
Possible values are:
43+
1 - agent;
44+
2 - SNMP;
45+
3 - IPMI;
46+
4 - JMX. """)
47+
parser.add_option("--useip",action="store",type="int",\
48+
dest="useip",default="1",help="""(REQUIRED,Default=1)Whether the connection should be made via IP.
49+
Possible values are:
50+
0 - connect using host DNS name;
51+
1 - connect using host IP address.""")
52+
parser.add_option("-f","--file",dest="filename",\
53+
metavar="FILE",help="""Load values from input file.
54+
Specify - for standard input Each line of file contains whitespace delimited:
55+
<hostname>4space<ip>""")
56+
57+
options,args = parser.parse_args()
58+
59+
if not options.server:
60+
options.server = raw_input('server http:')
61+
62+
if not options.username:
63+
options.username = raw_input('Username:')
64+
65+
if not options.password:
66+
options.password = getpass()
67+
68+
return options, args
69+
70+
def errmsg(msg):
71+
sys.stderr.write(msg + "\n")
72+
sys.exit(-1)
73+
74+
if __name__ == "__main__":
75+
options, args = get_options()
76+
77+
zapi = ZabbixAPI(options.server, options.username, options.password)
78+
79+
hostname = options.hostname
80+
ip = options.ip
81+
main = options.main
82+
port = options.port
83+
type = options.type
84+
useip = options.useip
85+
dns = options.dns
86+
file = options.filename
87+
88+
if file:
89+
with open(file,"r") as f:
90+
content = f.readlines()
91+
for i in content:
92+
l = i.split(" ")
93+
n = len(l)
94+
hostname = l[0].rstrip()
95+
ip = l[1].rstrip()
96+
hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"]
97+
interfaceid = zapi.hostinterface.get({"hostids":hostid, "output":"interfaceid"})[0]["interfaceid"]
98+
print n,'\t',hostname,'\t',interfaceid
99+
try:
100+
msg = zapi.hostinterface.update({"interfaceid":interfaceid, "ip":ip})
101+
print msg
102+
except Exception as e:
103+
print str(e)
104+
else:
105+
hostid = zapi.host.get({"host":hostname, "output":"hostid"})[0]["hostid"]
106+
interfaceid = zapi.hostinterface.get({"hostids":hostid, "output":"interfaceid"})[0]["interfaceid"]
107+
print hostname,'\t',hostid,'\t',interfaceid
108+
try:
109+
msg = zapi.hostinterface.update({"interfaceid":interfaceid, "ip":ip, "main":main, "port":port, "useip":useip, "dns":dns})
110+
print msg
111+
except Exception as e:
112+
print str(e)

0 commit comments

Comments
 (0)