ElasticSearch预警服务-Watcher详解-Schedule配置

介绍

Watcher服务详解-定时器的设定
关于Schedule配置选择,Watcher提供了丰富的时间语法支持,采用UTC时间,来我们一起看下如何设置:
支持的设置方式:

hourly:按小时周期设置
daily:按天周期设置
weekly:按周周期设置
monthly:按月周期设置
yearly:按年周期设置
cron:Cron表达式设置
interval :固定周期设置   

详细案例:

** 1.按小时 **

    每小时过30分钟执行
    "trigger" : {"schedule" : {"hourly" : { "minute" : 30 }}}
    指定时间执行,每小时第0,15,30,45分钟执行
    "trigger" : {"schedule" : {"hourly" : { "minute" : [ 0, 15, 30, 45 ] }}}

**2.按天 **

    每天下午5点执行
    "trigger" : {"schedule" : {"daily" : { "at" : "17:00" }}}
    "trigger" : {"schedule" : {"daily" : { "at" { "hour" : 17, "minute" : 0 } }
    每天00:00, 12:00, 17:00 执行
    "trigger" : {"schedule" : {"daily" : { "at" : [ "midnight", "noon", "17:00" ] }}}
    每天00:00, 00:30, 12:00, 12:30, 17:00,17:30执行
    "trigger" : {"schedule" : {"daily" : {"at" {"hour" : [ 0, 12, 17 ]"minute" : [0, 30]}}
**3.按星期**
    每周五下午5点执行
    "trigger" : {"schedule" : {"weekly" : { "on" : "friday", "at" : "17:00" } }}
    每周二12:00,周五17:00执行
    "trigger" : {"schedule" : {"weekly" : [{ "on" : "tuesday", "at" : "noon" },{ "on" : "friday", "at" : "17:00" }]}}
    每周二,周五的12:00,17:00执行
    "trigger" : {"schedule" : {"weekly" : {"on" : [ "tuesday", "friday" ]"at" : [ "noon", "17:00" ]}}

** 4.按月 **

    每月1012:00执行
    "trigger" : {"schedule" : {"monthly" : { "on" : 10, "at" : "noon" }}}
    每月1012:00,2017:00执行
    "trigger" : {"schedule" : {"monthly" : [{ "on" : 10, "at" : "noon" },{ "on" : 20, "at" : "17:00" }]}
    每月10号,20号的0:00,12:00执行
    "trigger" : {"schedule" : {"monthly" : {"on" : [ 10, 20 ]"at" : [ "midnight", "noon" ]}}

**5.按年 **

 "trigger" : {"schedule" : {"yearly" : { "in" : "january", "on" : 10, "at" : "noon" } }}
 "trigger" : {"schedule" : {"yearly" : [{ "in" : "january", "on" : 10, "at" : "noon" },{ "in" : "july", "on" : 20, "at" : "17:00" }]}
 "trigger" : {"schedule" : {"yearly" : {"in: : [ "jan", "dec" ],"on" : [ 10, 20 ],"at" : [ "midnight", "noon" ]}}

** 6.Cron表达式 **

"trigger" : {"schedule" : { "cron" : "0 0 12 * * ?"  } }

** 7.固定周期设置 **

    5"trigger" : { "schedule" : {"interval" : "5s" } }
    5分钟
    "trigger" : { "schedule" : {"interval" : "5m" } }
    5小时
    "trigger" : { "schedule" : {"interval" : "5h" } }
    5"trigger" : { "schedule" : {"interval" : "5d" } }
    5"trigger" : { "schedule" : {"interval" : "5w" } }

ElasticSearch(ES)预警服务 Watcher安装以及探究

介绍

最近公司需求要搭建一个监控系统,用来监控程序运行状态,请求曲线,错误日志发生次数,等等分析,保障系统稳定性。因此最近研究了基于Lucene 号称企业级搜索服务的ElasticSearch ,用了一段时间发现速度还真快,对百万级甚至千万级的数据检索毫无压力,比之前用的MongoDB速度快了不少。

ElasticSearch服务安装过程这里就不在说了,有了监控之后应该还需给开发人员发送邮件,告知某项错误已经达到某个阀值,应该关注去分析解决发生错误的原因,进一步发现程序上的bug,然后fix掉,下面隆重介绍下es(这里将ElasticSearch简称为es)的插件,Watcher,功能嘛很显然就是预警服务。

一、安装

1.依赖于License 插件,先安装License插件,在ES_HOME目录下面安装License

bin/plugin install license

2.安裝watcher插件

bin/plugin install watcher

注:中间过程询问是否继续时候输入y按回车继
3.验证是否安装成功

curl -XGET 'http://localhost:9200/_watcher/stats?pretty'

返回结果如下则表示安装成功

{
  "watcher_state": "started",
  "watch_count": 0,
  "execution_thread_pool": {
    "queue_size": 0,
    "max_size": 0
  }
}

二、Watcher插件配置(创建预警任务)

watcher目前是沒有界面配置的,需要通过Resfulapi调用创建、管理、更新预警任务

创建一个Watcher任务的流程是怎样的?

我们先来看下创建一个预警demo的api

curl -XPUT 'http://localhost:9200/_watcher/watch/log_error_watch' -d '
{
    "trigger": {
        "schedule": {
            "interval": "1m"
        }
    },
    "input": {
        "search": {
            "request": {
                "indices": [
                    "monitor_ticket_bargainfindermaxrq_201610"
                ],
                "body": {
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "match": {
                                        "collectionName": "BargainFinderMaxRQ"
                                    }
                                },
                                {
                                    "range": {
                                        "createTime": {
                                            "gt" : "now-1m"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    },
    "condition": {
        "compare": {
            "ctx.payload.hits.total": {
                "gt": 20
            }
        }
    },
    "actions": {
        "send_email": {
            "email": {
                "to": "[email protected]",
                "subject": "TICKET_BFM 過去一分鐘內錯誤發生次數超過20,請關注",
                "body": "TICKET_BFM 過去一分鐘內錯誤發生次數超過20,請關注"
            }
        }
    }
}'

当然actions里面还支持模板,比如 :

"actions": {
        "send_email": {
            "email": {
                "to": "[email protected]",
                "subject": "TICKET_BFM 過去一分鐘內錯誤發生{{ctx.payload.hits.total}}次數超過20,請關注",
                "body": "TICKET_BFM:{{ctx.watch_id}}  過去一分鐘內錯誤發生{{ctx.payload.hits.total}}次數超過20,請關注 Happened at {{ctx.execution_time}}"
            }
        }
    }

从api的content我們可以看出,內容主要主体分成trigger(触发设置),input(来源设置),condition(条件),actions(触发动作设置)

这里我们设置触发间隔时间为1m,来源是monitor_ticket_bargainfindermaxrq_201610索引块,查询条件是collectionName字段值是BargainFinderMaxRQ,以及时间范围是过去1分钟到现在,然后触发条件是当数量大于20,动作设置的是发邮件

Action类型其实有4种,EMail(邮件),Webhook(第三方对接),Index(索引),Logging(日志记录),相关配置可以参考下官方文档

如果配置了发送邮件,那么需要在es的yml文件配置下邮件发送服务器

我这里设置的是公司的服务器

watcher.actions.email.service.account:
  work:
    profile: standard
    email_defaults:
      from: '<yourname>@xx.com'
    smtp:
      auth: true
      starttls.enable: true
      host: smtp.xx.com
      port: 25
      user: yourname@xx.com
      password: password

注:设置之后重启下es服务器

三、删除预警任务

有时候我们不在使用某项预警服务的时候,我们可以删除他

#curl -XDELETE 'http://localhost:9200/_watcher/watch/log_error_watch'

参考链接 :
Elasticsearch索引mapping的写入、查看与修改 : https://blog.csdn.net/napoay/article/details/52012249?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

ElasticSearch(ES)预警服务 Watcher安装以及探究 : https://www.cnblogs.com/dafanshu/p/5980553.html

elasticsearch使用river同步mysql数据 : https://blog.csdn.net/alen1985/article/details/41356361?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

ElasticSearch预警服务-Watcher详解-监控时间线数据 :https://blog.csdn.net/corejava999/article/details/84720830?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
ElasticSearch预警服务-Watcher详解-Schedule配置 : https://blog.csdn.net/corejava999/article/details/84720010

发布了349 篇原创文章 · 获赞 60 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/104737263