zabbix--api学习之路--get_hostgroup获取

Posted zdoubly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix--api学习之路--get_hostgroup获取相关的知识,希望对你有一定的参考价值。

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

以上是关于zabbix--api学习之路--get_hostgroup获取的主要内容,如果未能解决你的问题,请参考以下文章

7Python全栈之路系列之Django表单

zabbix api如何获取最新的报警

zabbix api 登录

调用zabbix的api接口导出主机配置信息

zabbix批量添加主机监控-zabbix api调用

zabbix api 设置维护模式