Grafana ALert功能使用webhook,并预警数据信息

使用webhook预警的时候如果能够同时获取预警的数据信息是最好不过的,这里我们就来看下怎么获取这些东西。

数据源依然选用InfluxDB,具体配置过程可以参考前边两篇文章,现在从webhook配置开始。

    无参对接

在Grafana的Alerting下配置webhook信息:

具体参数信息前面有讲过,实际操作的时候也是很容易理解的,这里不在赘述。 但是有一点必须说明,我的Grafana是装在云服务器上的,本机适用的是公司内网ip,所以需要做一个本地ip映射,否则可能send test的时候直接失败。

配置好之后点击send Test可以检测接口是否能調通。

先看接收的接口代码:

@PostMapping("/getTest")
    public void alertTest(@RequestBody String body){
        System.out.println("this is getTest");

    }

因为现在Grafana的webhook只支持POST和PUT方法,这里我选用的是POST。

点击Send Test按钮,查看控制台信息:

可以看到接口是通了,如果调用失败就确认下是否内外网端口映射问题。

    静态参数

即使使用POST方式也是可以在url中加参数的,因为这里的配置只有URL供我们操作,所以我们先把参数放到url中。

将webhook的配置修改如下:

代码及测试结果如下:

这里可以看到数据被正常接收到。

    报警信息数据

一般来说触发预警的信息才是我们最想要获取的数据,现在我们来看这块数据的获取。 webhook中url里有参无参都可。

建立dashboard以及graph数据设定,如果不熟悉的同学可以参考前面两篇博文。

首先设置Metrics,显示数据信息:

Alert设置

Alert Config:这里设置好条件,使得能够满足预警触发。

Notifications:

代码修改如下:

@PostMapping("/getTest")
    public void alertTest(@RequestBody String body){

        System.out.println("this is getTest");

        System.out.println("body:"+body);
    }

这里我是使用body来接收数据的。

在Alert Config中测试:

上边query中的时间范围我设置成1h以便能获取到数据。

控制台输出信息(body信息我转成了json格式):

this is getTest
body:{
    "ruleName": "webhooktest",
    "state": "alerting",
    "message": "This is my webhook send test ,my target is to get the alert data.",
    "ruleId": 4,
    "title": "[Alerting] webhooktest",
    "ruleUrl": "http://localhost:3000/dashboard/db/webhookdatatest?fullscreen&edit&tab=alert&panelId=1&orgId=1",
    "evalMatches": [
        {
            "metric": "mytest.age",
            "value": 25,
            "tags": null
        }
    ]
}

因为这里的触发都是field,所以接收的数据是mytest.age的值。

    多曲线数据收集

更改metrics信息如下:

现在Graph由单线改为了多线,这样触发的时候极有可能是多条线都触发报警,也可能是单独一个触发的,这里我们看下多线同时触发的。

将触发点修改,使得其中三条线触发,最下边一条不触发(如果没有成功,试着先保存下修改):

控制台信息:

this is getTest
body:{
    "ruleName": "webhooktest",
    "state": "alerting",
    "message": "This is my webhook send test ,my target is to get the alert data.",
    "ruleId": 4,
    "title": "[Alerting] webhooktest",
    "ruleUrl": "http://localhost:3000/dashboard/db/webhookdatatest?fullscreen&edit&tab=alert&panelId=1&orgId=1",
    "evalMatches": [
        {
            "metric": "mytest.age { myName: pangkun2 }",
            "value": 27,
            "tags": null
        },
        {
            "metric": "mytest.age { myName: pangkun3 }",
            "value": 26,
            "tags": null
        }
    ]
}

这上面只是显示了其中两条线的触发,pangkun1这条线的数据并没有发送出来。

因为这里比较的是最小值,pangkun1这条线最后一个点的值低于了预警值,所以被视为OK的。

经验分享 程序员 微信小程序 职场和发展