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