使用zabbix api获取最近告警数据

使用zabbix api获取最近告警数据

基于zabbix3.0,python2.7(其他未测)

以下涉及账号密码段请自行修改为适用于自己的内容,直接上脚本,核心代码会做简短标注(翻译于zabbix官网手册)

使用zabbix api获取最新告警数据,获取到之后至于你要通过邮件,sms,wechat,钉钉去通知到人取决于你的环境!本文是在某位大神的基础上做的改造。具体在那里看到的文章忘记了。

#!/usr/bin/env python
#coding:utf-8
import json
import urllib2
from urllib2 import URLError
import sys
zabbix_addresses=['http://x.x.x.x/zabbix,nihouge,pwmasaike']
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'])
#                    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

核心代码:

                           "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
                                    },
  • 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
    只返回最近处于问题状态的触发器(个人理解为处于告警状态的主机)

其他相关功能暂时未测。此文章仅限借阅,若作为线上使用请自行测试bug以及风险

你可能感兴趣的:(zabbix)