如何在 pyzabbix 中使用 trigger.adddependencies

how to use trigger.adddependencies in pyzabbix

我是 python 和编码的新手,我正在尝试使用 pyzabbix 添加触发器依赖项,但出现了一些错误。

当我运行

zapi.trigger.addDependencies(triggerid, dependsOnTriggerid)

发生错误

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

我通过 trigger.get 获得 "triggerid" 和 "dependsOnTriggerid":

triggerid_info = zapi.trigger.get(filter={'host': 'xx','description': 'xx'},output=['triggerid'], selectDependencies=['description'])

triggerid = triggerid_info[0]['triggerid']

dependsOnTriggerid = trigger_info[0]['dependencies'][0]['triggerid']

结果如下:

Traceback (most recent call last): File "E:/10.python/2019-03-07/1.py", line 14, in zapi.trigger.addDependencies(triggerid, dependsOnTriggerid) File "D:\Program Files\Python37\lib\site-packages\pyzabbix__init__.py", line 166, in fn args or kwargs File "D:\Program Files\Python37\lib\site-packages\pyzabbix__init__.py", line 143, in do_request raise ZabbixAPIException(msg, response_json['error']['code']) pyzabbix.ZabbixAPIException: ('Error -32500: Application error., No permissions to referred object or it does not exist!', -32500)

我是不是弄错了triggerid?或者我以错误的方式使用该方法?非常感谢

添加依赖意味着您需要 link 两个不同的触发器(来自同一主机或来自另一主机)具有主依赖逻辑。

您正在尝试添加依赖项 triggerid -> dependsOnTriggerid,它是从假定的现有依赖项 (trigger_info[0]['dependencies'][0]['triggerid']) 中获得的,这毫无意义,我想这是错误的原因。

您需要获取两个触发器的 triggerid 然后添加依赖项:

masterTriggerObj = zapi.trigger.get( /* filter to get your master trigger here */ )
dependentTriggerObj = zapi.trigger.get( /* filter to get your dependent trigger here */)

result = zapi.trigger.adddependencies(triggerid=dependentTriggerObj[0]['triggerid'], dependsOnTriggerid=masterTriggerObj[0]['triggerid'])

方法"trigger.addDependencies"只需要一个参数,应该是dict或者其他的object/array。下面的代码解决了这个问题。

trigger_info = zapi.trigger.get(filter={xx},output=['triggerid'])

trigger_depends_info_193 = zapi.trigger.get(filter={xx},output=['triggerid'])

trigger_dependson_193 = {"triggerid": trigger_info[0]['triggerid'], "dependsOnTriggerid": trigger_depends_info_193[0]['triggerid']}

zapi.trigger.adddependencies(trigger_dependson_193)