prometheus和grafana告警汇总统计

为了建立完善的监控体系,需要对告警信息入库进行分类汇总。目前PaaS告警方式主要有prometheus和grafana,grafna的告警配置里无告警级别字段,为了对每个告警信息级别进行区分,1、可以在grafana每个告警配置中“Message”里写{"severity":"waring"},接口通过获取grafana告警信息中'message': '{"severity":"waring"}'字段获取告警级别信息,并入库
2、可以配置告警时添加tags进行告警标记,接口通过获取grafana告警信息中'tags': {'level': 'waring'}字段获取告警级别信息,并入库
tag可以配合alertmanager,实现基于标签的告警处理。实现根据告警等级,告警组匹配规则,进行不同的处理
grafana告警信息示例
告警信息
{'dashboardId': 19, 'evalMatches': [{'value': 50.01444270196602, 'metric': '{group="test", instance="127.0.0.1:9100", job="agent_linux", module="test"}', 'tags': {'group': 'test', 'instance': '127.0.0.1:9100', 'job': 'agent_linux', 'module': 'test'}}], 'message': '{"severity":"waring"}', 'orgId': 1, 'panelId': 2, 'ruleId': 1, 'ruleName': '内存使用率 alert', 'ruleUrl': 'http://localhost:3000/d/JkDvYNp7z/test?tab=alert&viewPanel=2&orgId=1', 'state': 'alerting', 'tags': {}, 'title': '[Alerting] 内存使用率 alert'}


{'dashboardId': 19, 'evalMatches': [{'value': 49.97435888466107, 'metric': '{group="test", instance="127.0.0.1:9100", job="agent_linux", module="test"}', 'tags': {'group': 'test', 'instance': '127.0.0.1:9100', 'job': 'agent_linux', 'module': 'test'}}], 'message': '{"level":"waring"}', 'orgId': 1, 'panelId': 2, 'ruleId': 1, 'ruleName': '内存使用率 alert', 'ruleUrl': 'http://localhost:3000/d/JkDvYNp7z/test?tab=alert&viewPanel=2&orgId=1', 'state': 'alerting', 'tags': {'level': 'waring'}, 'title': '[Alerting] 内存使用率 alert'}
{'dashboardId': 19, 'evalMatches': [], 'message': '{"severity":"waring"}', 'orgId': 1, 'panelId': 2, 'ruleId': 1, 'ruleName': '内存使用率 alert', 'ruleUrl': 'http://localhost:3000/d/JkDvYNp7z/test?tab=alert&viewPanel=2&orgId=1', 'state': 'ok', 'tags': {}, 'title': '[OK] 内存使用率 alert'}


{'dashboardId': 19, 'evalMatches': [], 'message': '{"level":"waring"}', 'orgId': 1, 'panelId': 2, 'ruleId': 1, 'ruleName': '内存使用率 alert', 'ruleUrl': 'http://localhost:3000/d/JkDvYNp7z/test?tab=alert&viewPanel=2&orgId=1', 'state': 'ok', 'tags': {'level': 'waring'}, 'title': '[OK] 内存使用率 alert'}

 

alertname 
job 
instance 
severity 
status 
alert_source 
value 
summary 
startsAt 
endsAt 
create_time 
update_time 
数据库字段

 

prometheus告警信息中入库的字段都有,相关字段写入数据库就行
告警信息中有2个地方需要注意一下
1、告警信息中 alerts里面的startsAt和endsAt时间是 UTC 时区的时间,需要转换
2、最外层的status如果是firing状态,不代表alerts中的status就一定都是firing,还有可能是resolved
告警示例
{
    'receiver': 'default',
    'status': 'firing',
    'alerts': [{
        'status': 'firing',
        'labels': {
            'alertname': 'NodeDown',
            'group': 'test',
            'instance': '127.0.0.1:9100',
            'job': 'agent_linux',
            'module': 'test',
            'severity': 'critical'
        },
        'annotations': {
            'description': 'Instance: 127.0.0.1:9100 已经宕机',
            'summary': 'instance: 127.0.0.1:9100 down',
            'value': '0'
        },
        'startsAt': '2021-11-30T02:29:50.37086163Z',
        'endsAt': '0001-01-01T00:00:00Z',
        'generatorURL': 'http://localhost:9091/graph?g0.expr=up%7Bjob%3D%22agent_linux%22%7D+%3D%3D+0&g0.tab=1',
        'fingerprint': 'c1894f96a619955e'
    }],
    'groupLabels': {
        'alertname': 'NodeDown'
    },
    'commonLabels': {
        'alertname': 'NodeDown',
        'group': 'test',
        'instance': '127.0.0.1:9100',
        'job': 'agent_linux',
        'module': 'test',
        'severity': 'critical'
    },
    'commonAnnotations': {
        'description': 'Instance: 127.0.0.1:9100 已经宕机',
        'summary': 'instance: 127.0.0.1:9100 down',
        'value': '0'
    },
    'externalURL': 'http://b7e112381cd7:9093',
    'version': '4',
    'groupKey': '{}:{alertname="NodeDown"}',
    'truncatedAlerts': 0
View Code

 

入库的规则
告警信息入库时根据alertname,job,instance,startsAt字段来判断是插入还是更新数据
一、如果为告警状态
1、判断之前未存在未恢复同类告警,则插入告警信息
2、判断之前存在未恢复同类告警,则插入告警信息,则更新告警当前值
二、如果为告警恢复状态,判断之前存在未恢复同类告警,则更新告警状态

grafna告警信息中部分字段没有,可以使用tags添加相应tag进行标记,然后获取字段入库。grafana告警信息中也没有startsAt和endsAt字段需可以考虑直接使用告警当前时间

根据ruleUrl,status进行判断入库
 一、如果不存在ruleUrl记录,则插入告警信息
二、存在ruleUrl记录
1、当前status为firing,数据库不存在status为firing,则插入告警信息
2、当前status为firing,数据库存在status为firing,则更新告警信息
3、当前status为resolved,数据库存在status为firing,则更新告警信息

 grafana展示告警数据

 

 

 

 

select alertname,status,level,value,source,startsAt,endsAt
from paas_monitor_alarm_info
where $__timeFilter(startsAt) and alertname in (${alertname}) limit 100;

 

 

posted @ 2023-02-06 14:13  泉love水  阅读(614)  评论(0编辑  收藏  举报