prometheus本身只是个监控工具,负责指标数据的储存和分析,所以日志监控会涉及到日志的分析,需要额外的插件进行日志分析处理,然后转换成prometheus能够识别的指标数据,然后做出告警。这个工具就是mtail

一,mtail的用途?

mtail :从应用程序日志中提取指标以导出到时间序列数据库或时间序列计算器

它是一个google开发的日志提取工具,用途就是:

实时读取应用程序的日志、

再通过自己编写的脚本进行分析、

最终生成时间序列指标

二,下载mtail

1,mtail的官方站:

  1. https://github.com/google/mtail

2,下载:

  1. [root@SearchCacheServer ~]# cd /usr/local/source/
  2. [root@SearchCacheServer source]# mkdir mtail
  3. [root@SearchCacheServer source]# cd mtail/
  4. [root@SearchCacheServer mtail]# wget https://github.com/google/mtail/releases/download/v3.0.0-rc35/mtail_v3.0.0-rc35_linux_amd64

三,安装mtail

1,增加可执行权限

  1. [root@SearchCacheServer mtail]# chmod 744 mtail_v3.0.0-rc35_linux_amd64

2,把mtail可执行程序移动到安装目录

  1. [root@SearchCacheServer mtail]# mkdir /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64
  2. [root@SearchCacheServer mtail]# mv mtail_v3.0.0-rc35_linux_amd64 /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64/mtail

3,测试程序是否可以运行?查看mtail版本

  1. [root@SearchCacheServer mtail]# /usr/local/soft/mtail_v3.0.0-rc35_linux_amd64/mtail --version
  2. mtail version v3.0.0-rc35 git revision a33283598c4b7a70fc2f113680318f29d5826cca go version go1.14 go arch amd64 go os linux

四,运行mtail

1,创建目录,保存mtail程序

  1. [root@SearchCacheServer phplogs]# mkdir -p /data/mtail/prog
  2. [root@SearchCacheServer phplogs]# cd /data/mtail/prog/
  3. [root@SearchCacheServer prog]# vi error_count.mtail

内容:

  1. counter error_count
  2. /ERROR/ {
  3. error_count++
  4. }

说明:error_count变量值统计了包含ERROR字串的行

https://github.com/google/mtail 项目地址在这里,go语言,更多的配置方式可以在这里看。

说明:

mtail的例子,请参考:

  1. https://github.com/google/mtail/tree/master/examples

2,运行mtail

  1. [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,检查启动是否成功

  1. [root@SearchCacheServer prog]# ss -lntp | grep mtail
  2. LISTEN 0 128 *:3903 *:* users:(("mtail",27642,11))

已经守护在了3903

4,查看mtail的相关信息

  1. http://121.122.123.118:3903/

mtail输出的metrics

  1. http://121.122.123.118:3903/metrics

五,配置prometheus

  1. [root@blog ~]# cd /usr/local/soft/prometheus-2.18.1.linux-amd64/
  2. [root@blog prometheus-2.18.1.linux-amd64]# vi prometheus.yml

在scrape_configs下新增一个job

  1. - job_name: '118mtail'
  2. static_configs:
  3. - targets: ['121.122.123.118:3903']

重启prometheus服务

  1. [root@blog prometheus-2.18.1.linux-amd64]# systemctl restart prometheus.service

我们可以通过prometheus查询error_count的值:

访问:注意把ip换成自己机器的ip

  1. http://121.122.123.47:9090/graph

如图:

8.Prometheus 日志监控 - 图1

六,增加对alertmanager报警的配置

1,在rule文件中增加新的报警配置

  1. [root@blog rules]# pwd
  2. /data/prometheus/rules
  3. [root@blog rules]# vi rule.yml

内容:

  1. - alert: ErrorlogStatus # alert 名字
  2. expr: error_count{job="118mtail"} > 0 # 判断条件
  3. for: 10s # 条件保持 10s 才会发出 alter
  4. labels: # 设置 alert 的标签
  5. severity: "critical"
  6. annotations: # alert 的其他标签,但不用于标识 alert
  7. description: php log error more than 20s
  8. summary: php have error

说明:error_count>0是触发报警的条件

2,检查配置:

  1. [root@blog prometheus-2.18.1.linux-amd64]# ./promtool check config prometheus.yml
  2. Checking prometheus.yml
  3. SUCCESS: 1 rule files found
  4. Checking /data/prometheus/rules/rule.yml
  5. SUCCESS: 2 rules found

说明:也可以只检查rule配置文件:

  1. [root@blog prometheus-2.18.1.linux-amd64]# ./promtool check rules /data/prometheus/rules/rule.yml
  2. Checking /data/prometheus/rules/rule.yml
  3. SUCCESS: 2 rules found

3,重启prometheus服务

  1. [root@blog rules]# systemctl restart prometheus.service

配置alertmanager部分请参考这一篇:

  1. https://www.cnblogs.com/architectforest/p/13065262.html

七,查看prometheus的版本

  1. [root@blog ~]# /usr/local/soft/prometheus-2.18.1.linux-amd64/prometheus --version
  2. prometheus, version 2.18.1 (branch: HEAD, revision: ecee9c8abfd118f139014cb1b174b08db3f342cf)
  3. build user: root@2117a9e64a7e
  4. build date: 20200507-16:51:47
  5. go version: go1.14.2

八,查看linux的版本

  1. [root@blog ~]# cat /etc/redhat-release
  2. CentOS Linux release 8.0.1905 (Core)