一,mtail的用途?
mtail :从应用程序日志中提取指标以导出到时间序列数据库或时间序列计算器
它是一个google开发的日志提取工具,用途就是:
实时读取应用程序的日志、
再通过自己编写的脚本进行分析、
最终生成时间序列指标
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,下载mtail
1,mtail的官方站:
https://github.com/google/mtail
2,下载:
[root@SearchCacheServer ~]# cd /usr/local/source/[root@SearchCacheServer source]#mkdirmtail
[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 | grepmtail
LISTEN0 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 errormorethan 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:1rule 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, version2.18.1(branch: HEAD, revision: ecee9c8abfd118f139014cb1b174b08db3f342cf)
build user: root@2117a9e64a7e
builddate: 20200507-16:51:47go version: go1.14.2
八,查看linux的版本
[root@blog ~]# cat /etc/redhat-release
CentOS Linux release8.0.1905 (Core)