Grafana自定义alert的实现过程

不问归期 提交于 2021-02-08 12:27:00

前言

grafana自带的alert功能是有限的,比如只能对某个query 配置alert,而不能对具体分类,当然我们也可以通过代码来实现定制化的alert

因要定时监控grafana的数据变化情况,所以本篇文章使用easyswoole的定时器来做讲解。

生成key

key为调用grafana http api 所需的验证信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


分析要请求的api和传递的参数

当然你可以去grafana官方文档去翻一番http api 章节
在这里插入图片描述

Install easyswoole的http-client组件

easyswoole\http-client

composer require easyswoole\http-client

easyswoole 定时器

定时器

代码实现

<?php
namespace EasySwoole\EasySwoole;

use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\HttpClient\HttpClient;

class EasySwooleEvent implements Event
{
   
   

    public static function initialize()
    {
   
   
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
   
   
        // TODO: Implement mainServerCreate() method.
        // 定时检测数据是否超过阈值
        Timer::getInstance()->loop(3 * 1000, function () {
   
   
            // 指定要请求的grafana接口
            $httpClient = new HttpClient('http://x.x.x.x./api/tsdb/query');
            // 设置认证信息,grafana 生成的key
            $httpClient->setHeaders([
                'Authorization' => 'Bearer eyJrIjoiS1lTVWhrNjk5cFM1and4Y0kzNmM2SDNXQ2oyMm44Sk'
            ], true, false);
            // 请求参数,这里可以通过代码控制from、to等参数
            $params = '{"from":"1612458544257","to":"1612631344257","queries":[{"refId":"A","intervalMs":300000,"maxDataPoints":634,"datasourceId":1,"rawSql":"SELECT\n  $__timeGroupAlias(ctime,5m),\n  category2 AS metric,\n  sum(price) as price\nFROM grafana_price\nWHERE\n  $__timeFilter(ctime) AND (\n    category2 = \"全部\"\n    OR (\"水果\" = category1 and \"全部\" = \"全部\" and category2 <> \"\")\n  )\nGROUP BY 1,2\nORDER BY $__timeGroup(ctime,5m)\n","format":"time_series"}]}';
            $response = $httpClient->postJson($params);
            $data = $response->getBody();
            $data = json_decode($data, true);
            // series 就是拿到的每个二级分类各时间节点的数据
            $series = $data['results']['A']['series'];

            // TODO: 后面就自定义报警(邮件通知、钉钉通知等)了
            
        });
    }

}

series结果

array(2) {
   
   
  [0]=>
  array(2) {
   
   
    ["name"]=>
    string(6) "香蕉"
    ["points"]=>
    array(2) {
   
   
      [0]=>
      array(2) {
   
   
        [0]=>
        int(88)
        [1]=>
        int(1612537500000)
      }
      [1]=>
      array(2) {
   
   
        [0]=>
        int(121)
        [1]=>
        int(1612542000000)
      }
    }
  }
  [1]=>
  array(2) {
   
   
    ["name"]=>
    string(6) "西瓜"
    ["points"]=>
    array(2) {
   
   
      [0]=>
      array(2) {
   
   
        [0]=>
        int(22)
        [1]=>
        int(1612540200000)
      }
      [1]=>
      array(2) {
   
   
        [0]=>
        int(33)
        [1]=>
        int(1612544100000)
      }
    }
  }
}
``

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!