[root@yang python]# catget_hostgroup_list.py!/usr/bin/env python2.7coding=utf-8
import json
import urllib2based url and required header
url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
header = {"Content-Type":"application/json"}request json
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"hostgroup.get",
"params":{
"output":["groupid","name"],
},
"auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth id is what auth script returns, remeber it is string
"id":1,
})create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
print "Number Of Hosts: ", len(response['result'])
#print response
for group in response['result']:
print "Group ID:",group['groupid'],"\tGroupName:",group['name']
python脚本执行结果:
[root@yang python]# pythonget_hostgroup_list.py
Number Of Hosts: 12
Group ID: 11 Group Name: DB Schedule
Group ID: 14 Group Name: DG-WY-KD-Server
Group ID: 5 Group Name: Discovered hosts
Group ID: 7 Group Name: Hypervisors
Group ID: 2 Group Name: Linux servers
Group ID: 8 Group Name: monitored_linux
Group ID: 9 Group Name: qsmind
Group ID: 12 Group Name: qssec
Group ID: 13 Group Name: switch
Group ID: 1 Group Name: Templates
Group ID: 6 Group Name: Virtual machines
Group ID: 4 Group Name: Zabbix servers
[root@yang python]# cat get_group_one.py!/usr/bin/env python2.7coding=utf-8
import json
import urllib2based url and required header
url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
header = {"Content-Type":"application/json"}request json
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"host.get",
"params":{
"output":["hostid","name"],
"groupids":"14",
},
"auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth id is what auth script returns, remeber it is string
"id":1,
})create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
print "Number Of Hosts: ", len(response['result'])
for host in response['result']:
print "Host ID:",host['hostid'],"HostName:",host['name']
[root@yang python]# cat get_items.py!/usr/bin/env python2.7coding=utf-8
import json
import urllib2based url and required header
url = "http://1.1.1.1/zabbix/api_jsonrpc.php"
header = {"Content-Type":"application/json"}request json
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"item.get",
"params":{
"output":["itemids","key_"],
"hostids":"10146",
},
"auth":"3c0e88885a8cf8af9502b5c850b992bd", # theauth id is what auth script returns, remeber it is string
"id":1,
})create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
print "Number Of Hosts: ", len(response['result'])
for host in response['result']:
print host
#print "Host ID:",host['hostid'],"HostName:",host['name']