我正在尝试引发异常并提供错误主机的主机名
我已经尝试过此代码
class ZabbixNonZero(Exception):
""" Raise when output value is failed """
def __init__(self, value):
super(ZabbixNonZero, self).__init__(value)
self.value = value
def __str__(self):
return repr(self.value)
Metric = [
ZabbixMetric(host='xxx.xx.x.xx', key='test[cpu_usage]',value=2),
ZabbixMetric(host='xx.xx.xx', key='zabbixsender', value="Everything ok")
]
print Metric
result = ZabbixSender(use_config=True).send(Metric)
host='xx.xx.xx'
try:
print(result)
except Exception as err:
raise ZabbixNonZero(err)
if result.failed != 0:
raise ZabbixNonZero('Failed to send data to host'.format(Metric.host))
我的输出是
This is Metric output [{"host": "xx.xx.xx", "value": "2", "key": "test[cpu_usage]"}, {"host": "xxx.xx.xx", "value": "Everything ok", "key": "zabbixsender"}]
This is result output {"failed": 1, "chunk": 1, "total": 2, "processed": 1, "time": "0.000237"}
我正在尝试在发生故障的主机上引发异常。
除了输出应该失败,我无法将数据发送到主机xxx.xx.xx
能帮您如何在异常情况下检索主机值
答案 0 :(得分:0)
您需要遍历列表以获取主机。
例如:
raise ZabbixNonZero('Failed to send data to host {}'.format(", ".join(i["host"] for i in Metric)))