代码之家  ›  专栏  ›  技术社区  ›  nagylzs

Zabbix API 3.4-由于循环要求,无法创建主机

  •  2
  • nagylzs  · 技术社区  · 7 年前

    在主机.创建API调用,我需要给出主机的接口。以下是相关文件: https://www.zabbix.com/documentation/3.4/manual/api/reference/host/create -需要interfaces参数。如果我试图给出一个空列表:

    zapi = ZabbixAPI(cfg.url)
    zapi.login(cfg.user, cfg.password) # I'm using an administrator user here!
    host = zapi.host.create(
        host=cfg.host_name,
        description=cfg.host_description,
        inventory_mode=1,  # auto host inventory population
        status=0,  # monitored host
        groups=[host_group_id],
        interfaces=[],  # active agent, no interface???
    )
    

    然后我得到这个错误:

    pyzabbix.ZabbixAPIException: ('Error -32500: Application error., No permissions to referred object or it does not exist!', -32500)
    

    我可以使用相同的用户和zabbix web界面创建主机,所以我想问题出在界面上。所以我试着先创建一个接口。然而主机接口.创建方法需要hostid参数。 https://www.zabbix.com/documentation/3.4/manual/api/reference/hostinterface/create 必须 给我一个hostid。

    这是catch 22-为了创建一个主机,我需要一个主机接口。但是要创建一个主机接口,我需要一个主机。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Simone Zabberoni    7 年前

    host create api也将创建hostinterface,您需要根据 the documentation

    例如,在调用api之前添加以下内容:

    interfaces = []
    interfaces.append( {
      'type' : 2,
      'main' : 1,
      'useip': 1,
      'ip' : '1.2.3.4',
      'dns' : "",
      'port' : '161'
    } )
    

        2
  •  1
  •   Adail Horst    7 年前

    引用的文档没有明确显示,但在Zabbix中,一个主机需要: -一个或多个接口(活动主机也需要)

    因此,对于您的代码工作,您需要更改为以下内容:

    zapi = ZabbixAPI(cfg.url)
    zapi.login(cfg.user, cfg.password) # I'm using an administrator user here!
    host = zapi.host.create(
        host=cfg.host_name,
        description=cfg.host_description,
        inventory_mode=1,  # auto host inventory population
        status=0,  # monitored host
        groups=[host_group_id],
        interfaces=[ {"type": "1",
                 "main": "1",
                 "useip": "1", 
                 "ip": "127.0.0.1",
                 "dns": "mydns", # can be blank
                 "port": "10051"}],  
    )
    

    在您的示例中是“主动主机”,但在Zabbix中,主动/被动的概念是针对项目,而不是针对主机。因此,它可能(也不是很罕见)有主机与被动和主动iten在同一时间。