Skip to content

Commit 338f3b8

Browse files
author
zhanghe5
committed
add zabbix_host_get.py zabbix_history_get.py
1 parent 2f463bc commit 338f3b8

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

zabbix_history_get.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 time
14+
15+
def get_options():
16+
usage = "usage: %prog [options]"
17+
OptionParser = optparse.OptionParser
18+
parser = OptionParser(usage)
19+
20+
parser.add_option("-s","--server",action="store",type="string",\
21+
dest="server",help="(REQUIRED)Zabbix Server URL.")
22+
parser.add_option("-u", "--username", action="store", type="string",\
23+
dest="username",help="(REQUIRED)Username (Will prompt if not given).")
24+
parser.add_option("-p", "--password", action="store", type="string",\
25+
dest="password",help="(REQUIRED)Password (Will prompt if not given).")
26+
parser.add_option("-H","--hostid",action="store",type="string",\
27+
dest="hostid",help="(REQUIRED)Return only items that belong to the given hosts.")
28+
parser.add_option("-G","--groupid",action="store",type="string",\
29+
dest="groupid",help="(REQUIRED)Return only items that are used in the given graphs.")
30+
parser.add_option("-P","--graphid",action="store",type="string",\
31+
dest="graphid",help="(REQUIRED)Return only items that are used in the given graphs.")
32+
parser.add_option("-f","--time_from",action="store",type="string",\
33+
dest="time_from",help="(REQUIRED,time format:YYYY-MM-DD HH:MM:SS)Return only values that have been received after or at the given time.")
34+
parser.add_option("-t","--time_till",action="store",type="string",\
35+
dest="time_till",help="(REQUIRED,time format:YYYY-MM-DD HH:MM:SS)Return only values that have been received before or at the given time.")
36+
37+
options,args = parser.parse_args()
38+
39+
if not options.server:
40+
options.server = raw_input('server http:')
41+
42+
if not options.username:
43+
options.username = raw_input('Username:')
44+
45+
if not options.password:
46+
options.password = getpass()
47+
48+
return options, args
49+
50+
def errmsg(msg):
51+
sys.stderr.write(msg + "\n")
52+
sys.exit(-1)
53+
54+
if __name__ == "__main__":
55+
options, args = get_options()
56+
57+
zapi = ZabbixAPI(options.server,options.username, options.password)
58+
59+
hostid = options.hostid
60+
groupid = options.groupid
61+
graphid = options.graphid
62+
time_from = int(time.mktime(time.strptime(options.time_from,'%Y-%m-%d %H:%M:%S')))
63+
time_till = int(time.mktime(time.strptime(options.time_till,'%Y-%m-%d %H:%M:%S')))
64+
65+
item = zapi.item.get({"output": ["itemid", "name"],"hostids":hostid, "graphids":graphid, "groupids":groupid})
66+
print item
67+
for key in item:
68+
print key['name']
69+
print zapi.history.get({"output":["value", "clock"], "history":0, "itemids":key['itemid'], "sortfield":"clock", "sortorder": "DESC", "time_from":time_from, "time_till":time_till})

zabbix_host_get.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("-f","--file",dest="filename",\
28+
metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>")
29+
30+
options,args = parser.parse_args()
31+
32+
if not options.server:
33+
options.server = raw_input('server http:')
34+
35+
if not options.username:
36+
options.username = raw_input('Username:')
37+
38+
if not options.password:
39+
options.password = getpass()
40+
41+
return options, args
42+
43+
def errmsg(msg):
44+
sys.stderr.write(msg + "\n")
45+
sys.exit(-1)
46+
47+
if __name__ == "__main__":
48+
options, args = get_options()
49+
50+
zapi = ZabbixAPI(options.server,options.username, options.password)
51+
52+
hostname = options.hostname
53+
file = options.filename
54+
55+
if file:
56+
with open(file,"r") as f:
57+
host_list = f.readlines()
58+
for hostname in host_list:
59+
hostname = hostname.rstrip()
60+
try:
61+
hostinfo = zapi.host.get({"filter":{"host":hostname},"output":"hostid", "selectGroups": "extend", "selectParentTemplates": ["templateid","name"]})[0]
62+
hostid = hostinfo["hostid"]
63+
host_group_list = []
64+
host_template_list = []
65+
for l in hostinfo["groups"]:
66+
host_group_list.append(l["name"])
67+
for t in hostinfo["parentTemplates"]:
68+
host_template_list.append(t["name"])
69+
#print "host %s exist, hostid : %s, group: %s, template: %s " % (hostname, hostid, host_group_list, host_template_list)
70+
print "host %s exist, hostid : %s, group: %s" % (hostname, hostid, host_group_list)
71+
except:
72+
print "host not exist: %s" %hostname
73+
else:
74+
try:
75+
hostinfo = zapi.host.get({"filter":{"host":hostname},"output":"hostid", "selectGroups": "extend", "selectParentTemplates": ["templateid","name"]})[0]
76+
hostid = hostinfo["hostid"]
77+
host_group_list = []
78+
host_template_list = []
79+
for l in hostinfo["groups"]:
80+
host_group_list.append(l["name"])
81+
for t in hostinfo["parentTemplates"]:
82+
host_template_list.append(t["name"])
83+
print "host %s exist, hostid : %s, group: %s, template: %s " % (hostname, hostid, host_group_list, host_template_list)
84+
except:
85+
print "host not exist: %s" %hostname

0 commit comments

Comments
 (0)