zabbix软件API功能集成开发方法指南

1、zabbix api文档

https://www.zabbix.com/documentation/3.4/manual/api

2、zabbix API开发库

zabbix API请求和响应都是json,并且还提供了各种语法的lib库,http://zabbix.org/wiki/Docs/api/libraries,包含php、c#、Python、Perl、go等等

3、pyzabbix python开发库

https://github.com/lukecyca/pyzabbix

4、请求zabbix API

post json数据到api接口地址,例如你得zabbix地址是http://company.com/zabbix,那么你得接口地址是:http://company.com/zabbix/api_jsonrpc.php,必须包含content-type头,值为application/json-rpc, application/json or application/jsonrequest之一。

注:在本章节中是使用Postman来模拟的http请求。

4.1 登录认证

Request:

{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "zentaouser",
        "password": "Test123"
    },
    "id": 1
}

Response:

{
    "jsonrpc": "2.0",
    "result": "0b7ce3a441a7ed0b238b2dfb2a336916",
    "id": 1
}

4.2 登出方法示例

Request:

{
    "jsonrpc": "2.0",
    "method": "user.logout",
    "params": [],
    "id": 1,
    "auth": "0b7ce3a441a7ed0b238b2dfb2a336916"
}

Response:

{
    "jsonrpc": "2.0",
    "result": true,
    "id": 1
}

4.3 按指定的时间段获取监控事件列表

Request:

{
    "jsonrpc": "2.0",
    "method": "event.get",
    "params": {
        "value": 1,
        "acknowledged": 0,
        "severities": [1,2,3,4,5],
        "output": "extend",
        "time_from": "1532656800",
        "time_till": "1532671200",
        "sortfield": ["clock", "eventid"],
        "sortorder": "desc"
    },
    "auth": "644fffb0a89e1251d5ced29d96c7f3a9",
    "id": 1
}
* value参数,指定获取"问题"事件,排除了"恢复"类事件;
* acknowledged,指定获取"未确认"状态下的事件;
* severities,获取指定严重级别的解发器事件;
* time_from和time_till参数,设置查询的起止时间,timestamp格式;
* auth,必须在请求中包含sessionid信息;

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "eventid": "7641666",
            "source": "0",
            "object": "0",
            "objectid": "51675",
            "clock": "1532657100",
            "value": "1",
            "acknowledged": "0",
            "ns": "129226556",
            "r_eventid": "7641669",
            "c_eventid": "0",
            "correlationid": "0",
            "userid": "0"
        },
        {
            "eventid": "7641667",
            "source": "0",
            "object": "0",
            "objectid": "51676",
            "clock": "1532657280",
            "value": "1",
            "acknowledged": "0",
            "ns": "503794274",
            "r_eventid": "7641670",
            "c_eventid": "0",
            "correlationid": "0",
            "userid": "0"
        },
        {
            "eventid": "7641677",
            "source": "0",
            "object": "0",
            "objectid": "41317",
            "clock": "1532659381",
            "value": "1",
            "acknowledged": "0",
            "ns": "439689270",
            "r_eventid": "7641678",
            "c_eventid": "0",
            "correlationid": "0",
            "userid": "0"
        },
        {
            "eventid": "7641695",
            "source": "0",
            "object": "0",
            "objectid": "47935",
            "clock": "1532661989",
            "value": "1",
            "acknowledged": "0",
            "ns": "629362525",
            "r_eventid": "7641696",
            "c_eventid": "0",
            "correlationid": "0",
            "userid": "0"
        },
        {
            "eventid": "7641749",
            "source": "0",
            "object": "0",
            "objectid": "47935",
            "clock": "1532669789",
            "value": "1",
            "acknowledged": "0",
            "ns": "139469184",
            "r_eventid": "7641767",
            "c_eventid": "0",
            "correlationid": "0",
            "userid": "0"
        }
    ],
    "id": 1
}
* eventid,事件ID,可以通过http://172.17.170.17/zabbix/tr_events.php?triggerid=47935&eventid=7641749来查看事件详细信息;
* objectid,触发器ID,在上面的URL中也使用到了这个参数值;
* clock,事件发生时间,timestamp格式;
* r_eventid,关联的恢复事件ID;

4.4 查询指定trigger的状态信息

在上面步骤中我们获取到了一些处于problem状态下的Event事件的信息,但是Event事件中并不提供事件的描述信息(主要是指可以说明事件情况的触发器名称和内容信息)。
所以我们需要根据上面步骤中得到的Event事件中提供的objectid参数,即trigger触发器ID,来继续查询指定trigger的状态,从中获取我们需要的触发器名称和描述等信息。

trigger = zapi.trigger.get(triggerids=51675, expandDescription='Y')

Request:

{
    "jsonrpc": "2.0",
    "method": "trigger.get",
    "params": {
        "triggerids": "51675",
        "expandDescription": "Y"
    },
    "auth": "644fffb0a89e1251d5ced29d96c7f3a9",
    "id": 1
}
* expandDescription参数,将trigger的description字段中的宏变量使用实际的数值进行替代;

Response:

{
    "jsonrpc": "2.0",
    "result": [
        {
            "triggerid": "51675",
            "expression": "{347483}=1 and {347484}=1 and \r\n{347485}=1",
            "description": "Zabbix agent on jumpserver_slave is unreachable for 5 minutes",
            "url": "",
            "status": "0",
            "value": "0",
            "priority": "3",
            "lastchange": "1532657665",
            "comments": "",
            "error": "",
            "templateid": "10047",
            "type": "0",
            "state": "0",
            "flags": "0",
            "recovery_mode": "0",
            "recovery_expression": "",
            "correlation_mode": "0",
            "correlation_tag": "",
            "manual_close": "0"
        }
    ],
    "id": 1
}

5、一些有用的zabbix URL链接

因为我们希望是定时自动查询zabbix api,获取当前处于problem状态下的事件列表,然后调用禅道项目管理软件API,生成一个监控工作任务。
在值班人员打开自己的禅道任务时,肯定需要直观、方便得查看和了解需要响应、分析和处理的监控事件的相关信息。

查看一个事件的详细信息的URL:
http://172.17.170.17/zabbix/tr_events.php?triggerid=51676&eventid=7641667

当然了,更为理想的形式,应该是下面这样:
Zabbix agent on jumpserver_slave is unreachable for 8 minutes

6、pyzabbix python开发库使用说明

安装pyzabbix模块

pip install pyzabbix

验证功能是否可用

[root@localhost ~]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> from pyzabbix import ZabbixAPI
>>> zapi = ZabbixAPI("http://172.17.170.17/zabbix")
>>> zapi.login("zentaouser", "Test123")
>>> print("Connected to Zabbix API Version %s" % zapi.api_version())
Connected to Zabbix API Version 3.4.10

>>> for h in zapi.host.get(output="extend"):
           print(h['hostid'])

查询一下我们在4.4章节中使用到的那个trigger的信息

>>> trigger = zapi.trigger.get(triggerids=51675, expandDescription='Y')
>>> print type(trigger)
<type 'list'>
>>> print trigger
[{u'status': u'0', u'recovery_mode': u'0', u'description': u'Zabbix agent on jumpserver_slave is unreachable for 5 minutes', u'state': u'0', u'url': u'', u'type': u'0', u'templateid': u'10047', u'correlation_tag': u'', u'lastchange': u'1532657665', u'value': u'0', u'priority': u'3', u'triggerid': u'51675', u'flags': u'0', u'comments': u'', u'error': u'', u'correlation_mode': u'0', u'expression': u'{347483}=1 and {347484}=1 and \r\n{347485}=1', u'recovery_expression': u'', u'manual_close': u'0'}]
>>>
>>> for t in trigger:
...   print type(t)
...
<type 'dict'>
>>> for t in trigger:
...   print t['description']
...
Zabbix agent on jumpserver_slave is unreachable for 5 minutes
>>>

我们再尝试获取下前面4.3章节中指定时间段内的监控事件列表信息

event_list = zapi.event.get(value=1,acknowledged=0, severities=[1,2,3,4,5],output="extend",time_from=1532656800, time_till=1532671200, sortfield=["clock", "eventid"],sortorde="desc")
>>> for event in event_list:
...     print "eventid:", event["eventid"]
...     print "objectid:", event["objectid"]
...
eventid: 7641666
objectid: 51675
eventid: 7641667
objectid: 51676
eventid: 7641677
objectid: 41317
eventid: 7641695
objectid: 47935
eventid: 7641749
objectid: 47935

更多的pyzabbix库使用例子:
https://github.com/lukecyca/pyzabbix/tree/master/examples

猜你喜欢

转载自blog.csdn.net/watermelonbig/article/details/81252723
今日推荐