一,mtail的用途?
mtail :從應用程序日志中提取指標以導出到時間序列數據庫或時間序列計算器
它是一個google開發的日志提取工具,用途就是:
實時讀取應用程序的日志、
再通過自己編寫的腳本進行分析、
最終生成時間序列指標
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,下載mtail
1,mtail的官方站:
https://github.com/google/mtail
2,下載:
[root@SearchCacheServer ~]# cd /usr/local/source/ [root@SearchCacheServer source]# mkdir mtail [root@SearchCacheServer source]# cd mtail/ [root@SearchCacheServer mtail]# wget https://github.com/google/mtail/releases/download/v3.0.0-rc35/mtail_v3.0.0-rc35_linux_amd64
三,安裝mtail
1,增加可執行權限
[root@SearchCacheServer mtail]# chmod 744 mtail_v3.0.0-rc35_linux_amd64
2,把mtail可執行程序移動到安裝目錄
[root@SearchCacheServer mtail]# mkdir /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64 [root@SearchCacheServer mtail]# mv mtail_v3.0.0-rc35_linux_amd64 /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64/mtail
3,測試程序是否可以運行?查看mtail版本
[root@SearchCacheServer mtail]# /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64/mtail --version mtail version v3.0.0-rc35 git revision a33283598c4b7a70fc2f113680318f29d5826cca go version go1.14 go arch amd64 go os linux
四,運行mtail
1,創建目錄,保存mtail程序
[root@SearchCacheServer phplogs]# mkdir -p /data/mtail/prog [root@SearchCacheServer phplogs]# cd /data/mtail/prog/ [root@SearchCacheServer prog]# vi error_count.mtail
內容:
counter error_count /ERROR/ { error_count++ }
說明:error_count變量值統計了包含ERROR字串的行
說明:
mtail的例子,請參考:
https://github.com/google/mtail/tree/master/examples
2,運行mtail
[root@SearchCacheServer prog]# nohup /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64/mtail -logtostderr -progs /data/mtail/prog/error_count.mtail -logs /data/logs/phplogs/prd_mobile_php_errors.log &
3,檢查啟動是否成功
[root@SearchCacheServer prog]# ss -lntp | grep mtail LISTEN 0 128 *:3903 *:* users:(("mtail",27642,11))
已經守護在了3903
4,查看mtail的相關信息
http://121.122.123.118:3903/
mtail輸出的metrics
http://121.122.123.118:3903/metrics
五,配置prometheus
[root@blog ~]# cd /usr/local/soft/prometheus-2.18.1.linux-amd64/ [root@blog prometheus-2.18.1.linux-amd64]# vi prometheus.yml
在scrape_configs下新增一個job
- job_name: '118mtail' static_configs: - targets: ['121.122.123.118:3903']
重啟prometheus服務
[root@blog prometheus-2.18.1.linux-amd64]# systemctl restart prometheus.service
我們可以通過prometheus查詢error_count的值:
訪問:注意把ip換成自己機器的ip
http://121.122.123.47:9090/graph
如圖:

六,增加對alertmanager報警的配置
1,在rule文件中增加新的報警配置
[root@blog rules]# pwd /data/prometheus/rules [root@blog rules]# vi rule.yml
內容:
- alert: ErrorlogStatus # alert 名字 expr: error_count{job="118mtail"} > 0 # 判斷條件 for: 10s # 條件保持 10s 才會發出 alter labels: # 設置 alert 的標簽 severity: "critical" annotations: # alert 的其他標簽,但不用於標識 alert description: php log error more than 20s summary: php have error
說明:error_count>0是觸發報警的條件
2,檢查配置:
[root@blog prometheus-2.18.1.linux-amd64]# ./promtool check config prometheus.yml Checking prometheus.yml SUCCESS: 1 rule files found Checking /data/prometheus/rules/rule.yml SUCCESS: 2 rules found
說明:也可以只檢查rule配置文件:
[root@blog prometheus-2.18.1.linux-amd64]# ./promtool check rules /data/prometheus/rules/rule.yml Checking /data/prometheus/rules/rule.yml SUCCESS: 2 rules found
3,重啟prometheus服務
[root@blog rules]# systemctl restart prometheus.service
配置alertmanager部分請參考這一篇:
https://www.cnblogs.com/architectforest/p/13065262.html
七,查看prometheus的版本
[root@blog ~]# /usr/local/soft/prometheus-2.18.1.linux-amd64/prometheus --version prometheus, version 2.18.1 (branch: HEAD, revision: ecee9c8abfd118f139014cb1b174b08db3f342cf) build user: root@2117a9e64a7e build date: 20200507-16:51:47 go version: go1.14.2
八,查看linux的版本
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)