es watcher插件安裝及使用

es watcher插件安裝及使用

簡介

  • watcher是一個用於elasticsearch的插件,它可以根據數據的變化提供警報和通知

安裝

  • 從5.0版本之後,watcher就成爲了x-pack的一部分,也就是說安裝了x-pack,watcher就已經安裝了。

使用

  • 驗證watcher是否啓動:
 curl --user elastic:changeme -XGET 'http://172.30.1.45:9200/_watcher/stats?pretty'

返回:
{
  "watcher_state" : "started",
  "watch_count" : 0,
  "execution_thread_pool" : {
    "queue_size" : 0,
    "max_size" : 10
  },
  "manually_stopped" : false
}

說明watcher已經啓動
  • watcher通常用於兩個場景:

    • 監控日誌數據的錯誤(Watch Log Data for Errors)
    • 監控es集羣的狀態(Watch Your Cluster Health)
  • 構建一個watcher,watcher的主體內容包含四部分:

    • 觸發設置(trigger):設置一個檢查錯誤日誌數據的週期(periodically checks your log data for error conditions),以下是兩種表達式:
      • ”schedule” : { “interval” : “10s” } 每十秒檢查一次
      • “schedule” : { “cron” : “0 0/1 * * * ?” } 每分鐘一次
      • 通常Schedules 配置不會很頻繁的,配置10s是爲了方便觸發,便於測試,測試完成要記得刪除測試watcher
    "trigger" : { 
        "schedule" : { "interval" : "10s" } 
    }
    或者
     "trigger" : { 
        "schedule" : { "cron" : "0 0/1 * * * ?" } 
    }
    
    • 數據來源設置(input):
    "input" : {
        "search" : {
            "request" : {
                "indices": ["ecps-*"],  //索引名稱
                "body" : {
                    "query" : {
                        "match" : { "message": "WEIFAN-NIHAO" }
                    }
                }
            }
        }
    }
    
    match短語:
    
    "match" : {
        "message" : {
            "query" : "this is a test",
            "type" : "phrase"
        }
    }
    • 條件設置(condition):判斷要監控的內容是否命中(checks to see if any errors were found or hit)
    條件:總命中數大於2,gt是大於,gte是大於等於
    
    "condition" : {
        "compare" : { "ctx.payload.hits.total" : { "gt" : 2 }}
    }
    
    ctx.payload.hits   查詢結果命中內容
    ctx.payload.hits.total  查詢結果命中數量
    ctx.payload.hits.hits.2  指定第幾個命中的,0是起始索引
    ctx.payload.hits.hits.<index>.fields.<fieldname> 指定第幾個命中的某個屬性,eg:ctx.payload.hits.hits.0.fields.message
    
    以上均可可以作爲郵件內容發送
    • 動作設置(actions): 一旦監控的內容命中,需要採取的措施,可以打印日誌或者郵件預警等。(Take action if there are any errors)
    可以把命中數動態寫入動作內容:
    
    動作一:通過打印日誌
    
    logging:會將日誌打印到es的標準輸出日誌中!(A watch action that simply logs text to the standard Elasticsearch logs)
    
    "actions" : {
        "log_error" : {//動作id
            "logging" : {
                "text" : "Found {{ctx.payload.hits.total}} errors in the logs, message:服務掛了!!!",
                "level": info //默認就是info
            }
        }
    }
    
    動作二:通過發送郵件
    
    "actions" : {
        "reminder_email" : {//動作 id
            "email" : {
                "to" : "[email protected]",//收件人地址
                "subject" : "system error",//主題
                "body" : "Found {{ctx.payload.hits.total}} errors in the logs, message:服務掛了!!!"//內容
        },
       "priority" : "high"//優先級:lowest, low, normal, high and highest
    }
    
  • 測試(測試過程可以直接在kibana dev tools 控制檯進行):

# 驗證watcher是否啓動
 curl --user elastic:changeme -XGET 'http://172.30.1.45:9200/_watcher/stats?pretty'

# 創建索引index1
curl --user elastic:changeme -XPUT '172.30.1.45:9200/index1?pretty'

# 設置watch,id:weifan_watch
curl --user elastic:changeme -XPUT 'http://172.30.1.45:9200/_watcher/watch/weifan_watch' -d '{這裏是watch的配置,json字符串}

# 測試案例一:
curl --user elastic:changeme -XPUT 'http://172.30.1.45:9200/_watcher/watch/weifan_watch' -d '{
  "trigger" : { "schedule" : { "interval" : "10s" } },
  "input" : {
    "search" : {
      "request" : {
        "indices":["index1"],
        "body" : {
          "query" : {
            "match" : { "message": "YIKANG" }//這裏的message是精確匹配
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gte" : 1 }}
  },
  "actions" : {
    "log_error" : {
      "logging" : {
        "text" : "Found {{ctx.payload.hits.total}} errors in the logs,message: 服務掛了!!!"
      }
    }
  }
}'

# 測試案例二:
curl --user elastic:changeme -XPUT 'http://172.30.1.45:9200/_watcher/watch/weifan_watch' -d '{
  "trigger" : { "schedule" : { "interval" : "10s" } },
  "input" : {
    "search" : {
      "request" : {
        "indices":["index1"],
        "body" : {
          "query" : {
           "match" : {
                "message" : {
                    "query" : "java.lang.OutOfMemoryError",
                    "type" : "phrase"//這裏的message是根據短語匹配,message包含關鍵字即可預警
                }
            }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gte" : 1 }}
  },
  "actions" : {
    "log_error" : {
      "logging" : {
        "text" : "Found {{ctx.payload.hits.total}} errors in the logs,message: 服務掛了!!!"
      }
    }
  }
}'

# 查看watcher內容:
curl --user elastic:changeme -XGET 'http://172.30.1.45:9200/_watcher/watch/weifan_watch'

# 往es裏面放數據,索引index1,type:type1,ID:1
# 測試案例一:
curl -XPOST '172.30.1.45:9200/index1/type1/1?pretty' -d '{"message": "YIKANG" }'

# 測試案例二:
curl -XPOST '172.30.1.45:9200/index1/type1/1?pretty' -d '{"message": "tomcat: java.lang.OutOfMemoryError: PermGen space" }'

查看es標準輸出日誌,是否打印:Found 1 errors in the logs,message: 服務掛了!!!,如果打印,即測試通過

# 刪除測試watcher:
curl --user elastic:changeme -XDELETE 'http://172.30.1.45:9200/_watcher/watch/weifan_watch'

其他可能用到的命令:

# 查看索引信息(根據id查詢):
curl --user elastic:changeme -XGET '172.30.1.45:9200/index1/type1/1?pretty'

# 查看索引內容(索引下所有):
curl --user elastic:changeme '172.30.1.45:9200/index1/_search?q=*&pretty'

# 刪除索引:
curl --user elastic:changeme -XDELETE '172.30.1.45:9200/index1?pretty'

# 列舉所有索引
curl --user elastic:changeme 'http://172.30.1.45:9200/_cat/indices?v'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章