api:hostgroup.get
url:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/hostgroup/get
-
参数说明:The method allows to retrieve host groups according to the given parameters.该方法允许根据给定的参数检索主机组。
-
The method supports the following parameters.该方法支持以下参数
参数 | 类型 | 描述 |
---|---|---|
graphids | string/array | 只返回包含具有给定图表的主机或模板的主机组。 |
groupids | string/array | 只返回具有给定主机组ID的主机组。 |
-
获取所有主机群组
import json
import urllib2
url ="http://10.127.0.119/api_jsonrpc.php"
header = {"Content-Type":"application/json" }
data =json.dumps(
{
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend", #不加条件,输出全部
# "filter": {
# "name": [
# "Linux servers",
# "Zabbix servers",
# "Discovered hosts"
# ]
# }
},
"auth": "cb5a0ce5ffbdd1cf3a18e32117801592", #刚才过去到的auth
"id": 1
}
)
request =urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# get host list
result = urllib2.urlopen(request)
response = json.loads(result.read())
result.close()
print response
print "Number Of Hosts: ", len(response['result']) #总数
#print response
for group in response['result']:
#print group
print "Group ID:",group['groupid']," GroupName:",group['name']
-
按照名字获取主机群组
- 请求:
{
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"Linux servers",
"Zabbix servers",
"Discovered hosts"
]
}
},
"auth": "cb5a0ce5ffbdd1cf3a18e32117801592",
"id": 1
}
- 响应:
Number Of Hosts: 3
Group ID: 5 GroupName: Discovered hosts
Group ID: 2 GroupName: Linux servers
Group ID: 4 GroupName: Zabbix servers