Hawkular Alert快速入门

快速入门

以下是基于官方关于hawkular alert的示例在本地的实践,更改了官方示例中不正确的部分。

添加警报

警报触发器的组成:

  • 一组触发警报/事件的条件;
  • 满足条件时要执行一个或多个操作(发送电子邮件,调用webhook等);
  • 一些额外的元数据(如严重性);

下面的示例触发条件为:当气温低于0摄氏度时,示例会发送一封电子邮件至“admin@example.org”。
此处为了演示方便将所有的内容放在了一个文件中,如下:
trigger_definition.json

{
  "triggers": [
    {
      "trigger": {
        "id": "temperature-trigger",    //指定trigger id
        "name": "Trigger for the temperature sensor",    //指定trigger name
        "severity": "HIGH",         //级别
        "enabled": true,             //启用
        "actions": [
          {
            "actionPlugin": "email",           //触发后将会以邮件的形式提醒
            "actionId": "notify-admin"      
          }
        ]
      },
      "conditions": [
        {
          "triggerMode": "FIRING",
          "type": "threshold",
         "dataId": "hm_g_temperature",   //指标数据(此处指标名称为:temperature,由于是gauge类型,因此加“hm_g_前缀”)
          "operator": "LT",
          "threshold": 0
        }
      ]
    }
  ],
  "actions": [
    {
      "actionPlugin": "email",       
      "actionId": "notify-admin",
      "properties": {
        "to": "admin@example.org"       //邮箱地址,可改成你可用的邮箱
      }
    }
  ]
}

导入触发器定义:

curl -u myUsername:myPassword -X POST http://localhost:8080/hawkular/alerts/import/all -d @trigger_definition.json \
   -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

测试

基于之前的配置,导入满足触发条件的指标数据,将会发送电子邮件。
补充:hawkular默认使用localhost:25发送邮件。

metrics_day_2.json

[
  {"timestamp": 1468620000000, "value": 8},
  {"timestamp": 1468621800000, "value": 6},
  {"timestamp": 1468623600000, "value": 3},
  {"timestamp": 1468625400000, "value": 0},
  {"timestamp": 1468627200000, "value": -2},
  {"timestamp": 1468629000000, "value": -3},
  {"timestamp": 1468630800000, "value": -1},
  {"timestamp": 1468632600000, "value": 2},
  {"timestamp": 1468634400000, "value": 4},
  {"timestamp": 1468636200000, "value": 5},
  {"timestamp": 1468638000000, "value": 8},
  {"timestamp": 1468639800000, "value": 12},
  {"timestamp": 1468641600000, "value": 13},
  {"timestamp": 1468643400000, "value": 12},
  {"timestamp": 1468645200000, "value": 13.4},
  {"timestamp": 1468647000000, "value": 14},
  {"timestamp": 1468648800000, "value": 14.3},
  {"timestamp": 1468650600000, "value": 14.6},
  {"timestamp": 1468652400000, "value": 17},
  {"timestamp": 1468654200000, "value": 17.3},
  {"timestamp": 1468656000000, "value": 17.5},
  {"timestamp": 1468657800000, "value": 17.9},
  {"timestamp": 1468659600000, "value": 18},
  {"timestamp": 1468661400000, "value": 18.7},
  {"timestamp": 1468663200000, "value": 19.2}
]

存入指标数据:

curl -u myUsername:myPassword -X POST http://localhost:8080/hawkular/metrics/gauges/temperature/raw -d @metrics_day_2.json \
   -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

完成之后可以在日志文件中看到如下的输出:

15:10:02,110 INFO  [org.hawkular.alerts.actions.api] (standalone-action-3) HAWKALERT240001: Plugin [email] has received an action message: [StandaloneActionMessage[action=Action[eventId='temperature-trigger-1501053001894-ee945e8a-7c26-4a5a-8236-de476d21f70c', ctime=1501053001903, event=Alert [tenantId=myTenant, triggerId=temperature-trigger, severity=HIGH, status=OPEN, ctime=1501053001894, lifecycle=[LifeCycle{user='system', status=OPEN, stime=1501053001894}], resolvedEvalSets=null], result='WAITING']]]
15:10:02,113 INFO  [org.hawkular.alerts.actions.api] (standalone-action-4) HAWKALERT240001: Plugin [email] has received an action message: [StandaloneActionMessage[action=Action[eventId='temperature-trigger-1501053001895-d8046519-6e4e-430d-85e9-059f6333f549', ctime=1501053001903, event=Alert [tenantId=myTenant, triggerId=temperature-trigger, severity=HIGH, status=OPEN, ctime=1501053001895, lifecycle=[LifeCycle{user='system', status=OPEN, stime=1501053001895}], resolvedEvalSets=null], result='WAITING']]]
15:10:02,121 INFO  [org.hawkular.alerts.actions.api] (standalone-action-5) HAWKALERT240001: Plugin [email] has received an action message: [StandaloneActionMessage[action=Action[eventId='temperature-trigger-1501053001892-55aa4be1-e955-4eaf-8d14-b79e8d4ec24f', ctime=1501053001897, event=Alert [tenantId=myTenant, triggerId=temperature-trigger, severity=HIGH, status=OPEN, ctime=1501053001892, lifecycle=[LifeCycle{user='system', status=OPEN, stime=1501053001892}], resolvedEvalSets=null], result='WAITING']]]

此时,我们可以在Cassandra数据库中查看一下我们的警报配置。

  1. 进入当前集群hawkular中的node1节点,启动cqlsh:

    [root@localhost node1]# ./bin/cqlsh
    Connected to hawkular at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 3.0.12 | CQL spec 3.4.0 | Native protocol v4]
    Use HELP for help.
    cqlsh> 
    
  2. 查看所有的keyspace:

    cqlsh> describe keyspaces;
    
    system_schema  hawkular_metrics  hawkular_alerts           system_traces
     system_auth    system            system_distributed
    
  3. 进入hawkular_alerts键空间:

    cqlsh> use hawkular_alerts;
    cqlsh:hawkular_alerts> 
    
  4. 查看该空间中的所有表:

    cqlsh:hawkular_alerts> describe tables;
    
    alerts_statuses         events                   cassalog               
    actions_history_ctimes  dampenings               actions_definitions    
    events_triggers         tags                     alerts_triggers        
    alerts_severities       actions_history_alerts   action_plugins         
    alerts_lifecycle        actions_history          triggers_actions       
    alerts_stimes           actions_history_results  sys_config             
    alerts_ctimes           events_categories          actions_history_actions
    alerts                  triggers               
    conditions              events_ctimes 
    
  5. 在表actions_definitions数据:

    cqlsh:hawkular_alerts> select * from actions_definitions;
    
    tenantid | actionplugin | actionid     | payload
    ----------+--------------+--------------+-----------------------------------------------  ----------------------------------------------------------------------------------
     myTenant |        email | notify-admin |     {"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin","global":false,"properties":{"to":"admin@example.org"}}
    
    (1 rows)
    

上述结果可以看到我们之前的触发定义信息。

登录邮箱可看到如下图的邮件:

补充
Hawkular Alerting Details中有介绍关于alert中condition部分的dataId名称的问题,不同类型metric的dataId要添加不同的前缀,具体如下:

  • hm_a: availability
  • hm_c: counter
  • hm_cr: counter rate
  • hm_g: gauge
  • hm_gr: gauge rate
  • hm_s: string

因此,当我们有一个gauge类型的指标temperature时,dataId就要写成:hm_g_temperature,否则就不会发生告警(所以,按照官方文档实践的同学就要注意啦,记得要改过来哟!)。

常用API命令示例

如果使用的是openshift中的hawkular metrics,则需要将-u "username: userpassword"替换为-H "Authorization: Bearer XXXXXX",并使用https
查看alert状态

 curl -u "username:userpassword" -X GET http://localhost:8080/hawkular/alerts/status -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

{"distributed":"false","Implementation-Version":"1.6.0.Final","Built-From-Git-SHA1":"82b3bb25a3b5a6d3e0e793110c6c544d6d0ce1cf","status":"STARTED"}

导入triggers和actions定义

curl -u "username:userpassword" -X POST http://localhost:8080/hawkular/alerts/import/all -d @trigger_definition.json -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

{"triggers":[{"trigger":{"tenantId":"myTenant","id":"memory_usage-trigger","name":"Trigger for the memory sensor","type":"STANDARD","eventType":"ALERT","eventCategory":null,"eventText":null,"severity":"HIGH","actions":[{"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin"}],"autoDisable":false,"autoEnable":false,"autoResolve":false,"autoResolveAlerts":true,"autoResolveMatch":"ALL","enabled":true,"firingMatch":"ALL","source":"_none_"},"conditions":[{"tenantId":"myTenant","triggerId":"memory_usage-trigger","triggerMode":"FIRING","type":"THRESHOLD","conditionSetSize":1,"conditionSetIndex":1,"conditionId":"myTenant-memory_usage-trigger-FIRING-1-1","dataId":"hm_g_elasticsearch/9f5dae41-71d0-11e7-97e5-fa163eaf6ed5/memory/usage","operator":"GT","threshold":1.8E9}]}],"actions":[{"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin","global":false,"properties":{"to":"email@example.com"}}]}

导出triggers和actions定义

 curl -u "username:userpassword" -X GET http://localhost:8080/hawkular/alerts/export -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

{"triggers":[{"trigger":{"tenantId":"myTenant","id":"temperature-trigger","name":"Trigger for the temperature sensor","type":"STANDARD","eventType":"ALERT","eventCategory":null,"eventText":null,"severity":"HIGH","actions":[{"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin"}],"autoDisable":false,"autoEnable":false,"autoResolve":false,"autoResolveAlerts":true,"autoResolveMatch":"ALL","enabled":true,"firingMatch":"ALL","source":"_none_"},"conditions":[{"tenantId":"myTenant","triggerId":"temperature-trigger","triggerMode":"FIRING","type":"THRESHOLD","conditionSetSize":1,"conditionSetIndex":1,"conditionId":"myTenant-temperature-trigger-FIRING-1-1","dataId":"hm_g_temperature","operator":"LT","threshold":0.0}]}],"actions":[{"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin","global":false,"properties":{"to":"email@example.com"}}]}

查看可使用的action插件

curl -u "username:userpassword" -X GET http://localhost:8080/hawkular/alerts/plugins -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

["elasticsearch","email","webhook"]

获取已经定义的trigger

curl -u "username:userpassword" -X GET http://localhost:8080/hawkular/alerts/triggers/your_trigger_id -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

{"tenantId":"myTenant","id":"temperature-trigger","name":"Trigger for the temperature sensor","type":"STANDARD","eventType":"ALERT","eventCategory":null,"eventText":null,"severity":"HIGH","actions":[{"tenantId":"myTenant","actionPlugin":"email","actionId":"notify-admin"}],"autoDisable":false,"autoEnable":false,"autoResolve":false,"autoResolveAlerts":true,"autoResolveMatch":"ALL","enabled":true,"firingMatch":"ALL","source":"_none_"}

获取特定trigger的condition

curl -u "username:userpassword" -X GET http://localhost:8080/hawkular/alerts/triggers/temperature-trigger/conditions -H "Content-Type: application/json" -H "Hawkular-Tenant: myTenant"

响应结果:

[{"tenantId":"myTenant","triggerId":"temperature-trigger","triggerMode":"FIRING","type":"THRESHOLD","conditionSetSize":1,"conditionSetIndex":1,"conditionId":"myTenant-temperature-trigger-FIRING-1-1","dataId":"hm_g_temperature","operator":"LT","threshold":0.0}]

参考文献

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,087评论 18 139
  • 本篇翻译的是触觉反馈的API文档以及相关的人机交互指南内容什么是触觉反馈呢?拥有 iPhone 7 以上的同学在滚...
    从来吃不胖阅读 3,307评论 0 51
  • Zabbix简介 Zabbix官方网站Zabbix中文文档 本文系统环境是CentOS7x86_64, Zabbi...
    Zhang21阅读 7,842评论 0 37
  • E2 Violent clashes between hard-line protesters and polic...
    一日一译阅读 225评论 0 1
  • 还可以吧→_→ 我可爱的小鲁班 大宝石项链,可惜我画的不怎么样 仿照一粒红尘画的,应该可以吧 有生以来,画的第一条...
    一起去碉堡阅读 249评论 0 1