1.需求:有一个语音合成播报项目,要实时获取zabbix的Hight级别以上告警信息,将该信息合成语音播报出去。(合成语音及播报已经完成)
2.现实:整理zabbix告警级别,将不太重要的告警放到Hight级别以下,将重点信息设置为Hight级别,然后通过zabbbix api获取。
3.简单实现:
#基础版本:包含全部信息,如果group hosts 等信息。博客有些格式缩进问题,复制下来请调整对齐就好。
#!/usr/bin/env python
#coding:utf-8
import json
import urllib2
from urllib2 import URLError
import sys
zabbix_addresses=["http://www.baidu.com/zabbix,Admin,Admin"]
class ZabbixTools:
def __init__(self,address,username,password):
self.address = address
self.username = username
self.password = password
self.url = '%s/api_jsonrpc.php' % self.address
self.header = {"Content-Type":"application/json"}
def user_login(self):
data = json.dumps({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.username,
"password": self.password
},
"id": 0
})
request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
print "Auth Failed, please Check your name and password:", e.code
else:
response = json.loads(result.read())
result.close()
#print response['result']
self.authID = response['result']
return self.authID
def trigger_get(self):
data = json.dumps({
"jsonrpc":"2.0",
"method":"trigger.get",
"params": {
"output": [
"triggerid",
"description",
"priority"
],
"filter": {
"value": 1
},
"sortfield": "priority",
"sortorder": "DESC",
"min_severity": 4,
"skipDependent": 1,
"monitored": 1,
"active": 1,
"expandDescription": 1,
"selectHosts": ['host'],
"selectGroups": ['name'],
"only_true": 1
},
"auth": self.user_login(),
"id":1
})
request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close()
issues = response['result']
content = ''
if issues:
for line in issues:
content = content + "%s:%s:%s\r\n" % (line['groups'],line['hosts'],line['description'])
content = content + "%s\r\n" % (line['description'])
#print line
return content
if __name__ == "__main__":
for zabbix_addres in zabbix_addresses:
address,username,password = zabbix_addres.split(',')
z = ZabbixTools(address=address, username=username, password=password)
content = z.trigger_get()
print content
####################核心代码解释:
def trigger_get(self):
data = json.dumps({
"jsonrpc":"2.0",
"method":"trigger.get",
"params": {
"output": [
"triggerid",
"description",
"priority"
],
"filter": {
"value": 1
},
"sortfield": "priority",
"sortorder": "DESC",
"min_severity": 4,
"skipDependent": 1,
"monitored": 1,
"active": 1,
"expandDescription": 1,
"selectHosts": ['host'],
"selectGroups": ['name'],
"only_true": 1
},
"auth": self.user_login(),
"id":1
})
method 此选项意为调用zabbix api的具体操作,如获取监控的主机,主机组,触发器等,此处trigger.get为获取zabbix 触发器状态(根据下面设定条件获取当前告警的触发器)
params 需要传递给API method的参数,这里是trigger.get下的一些属性(诸多属性请移步官网https://www.zabbix.com/documentation/3.0/manual/api/reference/trigger/get)
triggerid
触发器id
description
触发器内容秒数
priority
触发器等级1-5,5最大 1最小
filter
过滤信息 value 1 表示启动的
sortfield
排序
sortorder
正排还是倒排
min_severity
大于等于给定的触发器级别,这里是大于等于严重
skipDependent
跳过依赖于其他触发器的问题状态中的触发器。请注意,如果禁用了其他触发器,则会禁用其他触发器,禁用项目或禁用项目主机。
monitored
属于受监控主机的已启用触发器,并仅包含已启用的项目
active
只返回属于受监控主机的启用的触发器(与上条意思差不多,至于什么区别,未测)
expandDescription
在触发器的名称中展开宏
selectHosts
在结果中返回关联的主机信息(意思就是显示出那台主机告警的)
selectGroups
在结果中返回关联的主机组信息(意思就是显示出那个主机组告警的)
only_true
只返回最近处于问题状态的触发器(个人理解为处于告警状态的主机)
#简洁版:仅提示trigger ,不包含group hosts 等其他信息。博客有些格式缩进问题,复制下来请调整对齐就好。
#!/usr/bin/env python
#coding:utf-8
import json
import urllib2
from urllib2 import URLError
import sys
zabbix_addresses=['http://www.aliyun.com/zabbix,Admin,Admin']
class ZabbixTools:
def __init__(self,address,username,password):
self.address = address
self.username = username
self.password = password
self.url = '%s/api_jsonrpc.php' % self.address
self.header = {"Content-Type":"application/json"}
def user_login(self):
data = json.dumps({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.username,
"password": self.password
},
"id": 0
})
request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
print "Auth Failed, please Check your name and password:", e.code
else:
response = json.loads(result.read())
result.close()
#print response['result']
self.authID = response['result']
return self.authID
def trigger_get(self):
data = json.dumps({
"jsonrpc":"2.0",
"method":"trigger.get",
"params": {
"output": [
"triggerid",
"description",
"priority"
],
"filter": {
"value": 1
},
"sortfield": "priority",
"sortorder": "DESC",
"min_severity": 4,
"skipDependent": 1,
"monitored": 1,
"active": 1,
"expandDescription": 1,
"selectHosts": ['host'],
"selectGroups": ['name'],
"only_true": 1
},
"auth": self.user_login(),
"id":1
})
request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close()
issues = response['result']
content = ''
if issues:
for line in issues:
#content = content + "%s:%s:%s\r\n" % (line['groups'],line['hosts'],line['description'])
content = content + "%s\r\n" % (line['description'])
# print line
return content
if __name__ == "__main__":
for zabbix_addres in zabbix_addresses:
address,username,password = zabbix_addres.split(',')
z = ZabbixTools(address=address, username=username, password=password)
content = z.trigger_get()
print conten
注意:以上代码都是本人测试通过,如果有因zabbix /python版本不同可能有所偏差,请各位同行大佬相互指教。