It works by combining Elasticsearch with two types of components, rule types and alerts. Elasticsearch is periodically queried and the data is passed to the rule type, which determines when a match is found. When a match occurs, it is given to one or more alerts, which take action based on the match.
周期性的查询Elastsearch而且将数据传递给规则类型,规则类型定义了须要查询哪些数据。html
当一个规则匹配触发,就会给到一个或者多个的告警,这些告警具体会根据规则的配置来选择告警途径,就是告警行为,好比邮件、企业微信等python
使用官网的pip install elastalert
安装时,我这里报错,因此改用了git clone到本地的方式
ElastAlert官方安装流程github
若是没有pip安装工具看下面流程
pip 安装流程api
yum install python-devel
sudo yum install openssl-devel
Next, open up config.yaml.example. In it, you will find several configuration options. ElastAlert may be run without changing any of these settings.
rules_folder is where ElastAlert will load rule configuration files from. It will attempt to load every .yaml file in the folder. Without any valid rules, ElastAlert will not start. ElastAlert will also load new rules, stop running missing rules, and restart modified rules as the files in this folder change. For this tutorial, we will use the example_rules folder.
这里咱们复制config.yaml.example
为config.yaml
,新建目录rules
服务器
cp config.yaml.example config.yaml mkdir rules
修改config.yaml
文件以下,其余的配置不须要修改微信
# 这里指定了咱们配置的规则的目录 rules_folder: rules # How often ElastAlert will query Elasticsearch # The unit can be anything from weeks to seconds # 每次间隔1分钟触发一次 run_every: minutes: 1 # ElastAlert will buffer results from the most recent # period of time, in case some log sources are not in real time buffer_time: minutes: 15 # The Elasticsearch hostname for metadata writeback # Note that every rule can have its own Elasticsearch host # 配置elasticsearch 的地址和端口 es_host: xxx.xx.xxx.xx # The Elasticsearch port es_port: 9200
里面已经给出了配置的范例,这里咱们使用frequency的配置。要作根据频率变化的告警。app
[example_rules]# tree . ├── example_cardinality.yaml ├── example_change.yaml ├── example_frequency.yaml ├── example_new_term.yaml ├── example_opsgenie_frequency.yaml ├── example_percentage_match.yaml ├── example_single_metric_agg.yaml ├── example_spike.yaml └── jira_acct.txt
复制frequency的配置文件到新的rules目录elasticsearch
cp example_rules/example_frequency.yaml rules/ cd rules mv example_frequency.yaml app_frequency_mail.yaml
这里会详细介绍下配置,可是只会用到个别字段ide
# Alert when the rate of events exceeds a threshold # (Optional) # Elasticsearch host # 无需修改使用全局 # es_host: elasticsearch.example.com # (Optional) # Elasticsearch port # es_port: 14900 # (OptionaL) Connect with SSL to Elasticsearch #use_ssl: True # (Optional) basic-auth username and password for Elasticsearch #es_username: someusername #es_password: somepassword # (Required) # Rule name, must be unique # 这里要定义一个规则名称,并且要unique惟一 name: app frequency rule mail # (Required) # Type of alert. # the frequency rule type alerts when num_events events occur with timeframe time # 定义规则类型 type: frequency # (Required) # Index to search, wildcard supported # 须要检索的日志索引 index: logstash-app-prod* # (Required, frequency specific) # Alert when this many documents matching the query occur within a timeframe # 命中五次 num_events: 5 # (Required, frequency specific) # num_events must occur within this amount of time to trigger an alert # 十分钟以内命中五次,就算是触发一次规则 timeframe: # hours: 4 minutes: 10 # 按照某个字段进行聚合,意思是aggreation_key会和rule的名称拼接在一块儿做为一个组,单独发送告警,相同的mesage是一个组 #aggregation_key: # - message # 聚合2分钟 aggregation: minutes: 2 # 不进行重复提醒的字段,和realert联合使用,30分钟内这个query_key只告警一次 query_key: - message realert: minutes: 30 # (Required) # A list of Elasticsearch filters used for find events # These filters are joined with AND and nested in a filtered query # For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html # 这里按照正则匹配来查询,能够看query-dsl里面的官方文档 filter: - query: regexp: category: "error-*" #- term: # category: "error-*" # 邮箱设置 smtp_host: smtp.qq.com smtp_port: 465 smtp_ssl: true # 发件箱的地址 from_addr: "xx@qq.com" # 这个是指向的邮箱验证的配置文件,有用户名、和密码,对于qq而言,这里面的密码是受权码,能够经过qq邮箱设置里面,开启smtp的时候查看 smtp_auth_file: /home/elastalert/smtp_auth_file.yaml # (Required) # The alert is use when a match is found # 定义告警类型是邮件提醒 alert: - "email" # 下面这些不配置,会发送一个默认的告警模板,纯文字太丑了,因此咱们进行了格式化,发送一个html格式的 email_format: html alert_subject: "app 正式环境 告警 {}" # 这里使用python 的format 进行格式化 alert_subject_args: - category # 若是这个去掉,那么发送alert_text的同时,也会发送默认模板内容 alert_text_type: alert_text_only # 下面这个是本身配置的 alert_text: "<div style='display:block;background-color: red;padding: 10px;border-radius: 5px;color: white;font-weight: bold;' > <p>{}</p></div><br><a href='这里填写本身的kibana地址href' target='_blank' style='padding: 8px 16px;background-color: #46bc99;text-decoration:none;color: white;border-radius: 5px;'>Click to Kibana</a><br><h3>告警详情</h3><table><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>@timestamp:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>@version:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>_id:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>_index:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>_type:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>appType:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>appVersion:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>business:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>category:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>geoip:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>guid:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>host:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>message:</td><td style='padding:10px 5px;border-radius: 5px;background-color: red;color: white;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>num_hits:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>num_matches:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>path:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>server:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>uid:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>uri:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr><tr><td style='padding:5px;text-align: right;font-weight: bold;border-radius: 5px;background-color: #eef;'>userAgent:</td><td style='padding:5px;border-radius: 5px;background-color: #eef;'>{}</td></tr></table>" # 这里须要配置area_text中出现的各个字段,其实跟sprintf同样按照顺序格式化的 alert_text_args: - message - "@timestamp" - "@version" - _id - _index - _type - appType - appVersion - business - category - geoip - guid - host - message - num_hits - num_matches - path - server - uid - uri - userAgent # (required, email specific) # a list of email addresses to send alerts to # 这里配置收件人的邮箱 email: - "xxx@xxx.com"
而后来看下邮箱验证的配置,也就是smtp_auth_file.yaml
# 发件箱的qq邮箱地址,也就是用户名 user: xxx@qq.com # 不是qq邮箱的登陆密码,是受权码 password: uxmmmmtefwqeibcjd
执行的时候,很简单,稍后咱们看下配置supervisor高可用
nohup python -m elastalert.elastalert --rule app_frequency_mail.yaml --verbose &
须要信息
这里咱们使用一个开源企业微信发送插件
git地址:https://github.com/anjia0532/elastalert-wechat-plugin
插件使用说明
https://anjia0532.github.io/2017/02/16/elastalert-wechat-plugin/
按照建立邮件告警规则同样,建立新的规则告警文件。其中从alert开始配置成新的告警方式
alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text: " ======start====== \n 索引:{}\n 服务器:{}\n 接口:{}\n 告警:\n {}" alert_text_type: alert_text_only # 企业微信告警的数据不须要太多,太长 alert_text_args: - _index - server - path - message #后台登录后【设置】->【权限管理】->【普通管理组】->【建立并设置通信录和应用权限】->【CorpID,Secret】 #设置微信企业号的appid corp_id: wxea4f5f73xxxx #设置微信企业号的Secret secret: "xxxxxBGnxxxxxxxxxrBNHxxxxxxxE" #后台登录后【应用中心】->【选择应用】->【应用id】 #设置微信企业号应用id agent_id: 100xxxx #部门id party_id: 14 #用户微信号 user_id: "@all" # 标签id #tag_id:
查看做者的另外一个项目https://github.com/anjia0532/weixin-qiye-alert 发现对于user_id,tag_id
的配置是有规则的:
若是指定标签名称不存在,会自动经过api建立一个标签(处于锁定状态),须要管理员,手动解锁,而且添加成员 若是指定标签下没有成员(标签添加部门无效),则会根据cp.properties指定的部门idPartyId和人员idUserId进行发送 若是部门下没有成员,并且指定的用户也没有关注该企业号,则会将信息推送给该企业号所有已关注成员,测试时需谨记
这正合咱们的心意,由于咱们不会只给一我的发送消息!
咱们须要的是,发给全部日志告警部门的小伙伴,因此咱们要怎么作呢?!!
通过测试,我将user_id注释掉,并不能发送消息, 理想状态不该该是删掉user_id,就只发送给所有门么?然而并非哒~,咱们查看下源码(发现做者简直是每一行代码都有注释~太好啦~)
咱们会看到做者的注释,所有用@all~~ ,因此能看到上面user_id 我配置的是@all
啦
self.party_id = self.rule.get('party_id') #部门id self.user_id = self.rule.get('user_id', '') #用户id,多人用 | 分割,所有用 @all self.tag_id = self.rule.get('tag_id', '') #标签id