[普罗米修斯] 通过自己编写的mtail进行日志监控
目录
- [目次] Prometheusで監視システムを作る with Docker
简要介绍
使用Prometheus进行日志监控的环境搭建步骤。使用Google提供的日志解析工具mtail。还将说明如何自定义mtail规则的方法。
以下是用于mtail的简易验证环境的链接:
https://github.com/Esfahan/prometheus-and-mtail
前提条件 (one option)
假设您已经按照以下步骤搭建了Prometheus环境。
https://qiita.com/Esfahan/items/0feaedfd771f49ac7ee4
山尾
请使用此链接:
https://github.com/google/mtail
环境
- mtail v3.0.0-rc46
暂时试着运行一下
Docker:+ 选项
在被监控的服务器上启动。收集/var/log/messages和/var/log/cron的指标,并仅计算日志行数的示例。
使用mtail公式提供的linecount.mtail规则。
后面会详细介绍使用自定义规则收集度量数据的方法。
version: '3'
services:
mtail:
image: dylanmei/mtail
container_name: mtail
hostname: mtail
volumes:
- /var/log/:/var/log/
ports:
- 3903:3903
entrypoint:
- mtail
- --progs
- /go/src/github.com/google/mtail/examples/linecount.mtail
- --logs
- /var/log/messages
- --logs
- /var/log/cron
$ sudo docker-compose up -d --build
普罗米修斯的配置
编辑在这篇文章中构建的Prometheus环境的配置文件。
添加监视对象
在prometheus.yaml文件中添加以下内容。
# 前略
scrape_configs:
+ - job_name: mtail
+ metrics_path: /mtail/metrics
+ static_configs:
+ - targets:
+ - your_mtail_server.com:3903
+ labels:
+ env: development
Mtail的用户界面

点击”prometheus”链接后可以查看指标。
# TYPE line_count counter
# line_count defined at linecount.mtail:4:9-18
line_count{prog="linecount.mtail"} 244
普罗米修斯的用户界面


PromQL 的汉语近义词是:Prometheus 查询语言。

使用自制的mtail来收集度量。
为了根据自己的规则进行度量收集,我要自己制作一个mtail。
在这个例子中,我要制作一个mtail来通过正则表达式计算example.log中输出日志的数量,并判断是否包含特定的字符串。
关于 mtail 的语法已在以下链接中进行了解释。
https://github.com/google/mtail/blob/main/docs/Language.md
此外,mtail的示例已经在以下链接中准备好了。
https://github.com/google/mtail/tree/main/examples
目录结构
创建以下目录结构。
├── docker-compose.yaml
├── logs
│ └── example.log # メトリクス収集対象のログ
└── mtail
└── example.mtail # 自作mtail
事前准备
$ mkdir {logs,mtail}
$ touch logs/example.log
例子。mtail
创建mtail/example.mtail文件。有关如何收集度量的规则,请参考注释。
在定义变量时,需要定义counter或gauge中的一种类型。
counter用于在匹配正则表达式的行数时进行计数。
gauge是一种可以赋予数值、字符串等的类型,在以下示例中,如果匹配到正则表达式的行,则返回1,否则返回0。还可以返回字符串,如果返回字符串,请使用双引号而不是单引号,因为使用单引号会报错。line_count_info和warn_flg_no_declared是变量名,但可以使用任意名称。
counter line_count_error
counter line_count_fatal
counter line_count_debug
gauge error_flg_syntax
gauge fatal_flg_oom
gauge debug_flg
# [ERROR]の行が何行あるか
/^\[ERROR/ {
line_count_error++
}
# [FATAL]の行が何行あるか
/^\[FATAL/ {
line_count_fatal++
}
# [DEBUG]の行が何行あるか
/^\[DEBUG/ {
line_count_debug++
}
# "Syntax error"を含む行があるかどうか
/Syntax error/ {
error_flg_syntax = 1
}
# "Out of memory"を含む行があるかどうか
/Out of memory/ {
fatal_flg_oom = 1
}
# "debug message"を含む行があるかどうか
/debug message/ {
debug_flg = 1
}
顺便一提,在Golang中,无法使用正则表达式实现“字符串不匹配”的否定操作,因此需要进行一些技巧处理。请参考以下内容。
使用Docker搭建mtail的环境
version: '3'
services:
mtail:
image: dylanmei/mtail
container_name: mtail
volumes:
- ./logs:/logs/
- ./mtail/:/mtail
ports:
- 3903:3903
entrypoint:
- mtail
- --progs
- /mtail/example.mtail
- --logs
- /logs/example.log
#######################
# 複数ファイル指定する場合
#######################
#- mtail
#- --progs
# progsはディレクトリを指定することで、その配下のファイルが全て読み込まれる
#- /mtail/
# logsは、以下の様にカンマ区切りで指定するか、もしくは--logsを複数指定することでも可能。
#- --logs
#- /logs/example.log,/logs/example02.log
#- --logs
#- /logs/example03.log
networks:
- sample-network
networks:
sample-network:
external: true
$ sudo docker-compose up -d --build mtail
确认如何检查mtail的语法错误等


日志目标
在`logs/example.log`中写入以下内容。
由于Docker容器启动后生成的日志将被收集,因此请在执行`docker-compose up`之后进行日志写入。
[INFO] This is an info message.
[WARN] This is a warning message.
[WARN] No declared value.
[ERROR] This is an error message.
[ERROR] Syntax error.
[ERROR] Syntax error.
[FATAL] This is a fatal message.
[FATAL] Out of memory.
[FATAL] Disk error.
[FATAL] Kernel panic.
在Prometheus的页面上查看指标

检测障碍、验证恢复
在example.mtail中添加规则。
在之前创建的example.mtail文件中,我们制定了规则,如果包含了”Out of memory”的行,那么将设置fatal_flg_oom为1。现在除此之外,我们还要补充一条规则,如果包含了”Memory recorverd”的行,那么将设置fatal_flg_oom为0。
通过这样的方式,如果日志中写入了”Out of memory”,则发生故障;如果写入了”Memory recorverd”,则进行恢复操作。
gauge fatal_flg_oom
# "Out of memory"を含む行があるかどうか
/Out of memory/ {
fatal_flg_oom = 1
}
+# 以下を追記
+# "Memory recorverd"を含む行があるかどうか
+/Memory recorverd/ {
+ fatal_flg_oom = 0
+}
反映设定
$ sudo docker-compose restart mtail
验证
将”Out of memory”添加到日志中。
$ echo "[FATAL] Out of memory." >> logs/example.log

将“Memory recovered”添加到日志中。
$ echo "[INFO] Memory recorverd" >> logs/example.log

建议多次尝试以确认再次写入 [FATAL] Out of memory 时,将会使 fatal_flg_oom = 1;写入 [INFO] Memory recover 时,将使 fatal_flg_oom = 0。
当日志中写入了”ERROR”字符串时,进行错误检测的示例。
麻袋
创建一个规则,当日志中写入”ERROR”字符串时,error_count将递增。
mtail/mtail/example.mtail的中文翻译如下:mtail/mtail/example.mtail
counter error_count
/ERROR/ {
error_count++
}
记录
将”ERROR”字样写入日志。
[ERROR] Syntax error.
检测方法(PromQL)
使用rate函数检查最近1分钟是否写入了”ERROR”字符串。
如果没有写入,返回0,可以使用以下的PromQL来检测错误。
rate(error_count[1m]) > 0
rate(error_count[5m]) > 0
请参考以下关于rate函数的内容。
参考资料:rate函数
使用mtail命令检查指标
通过在mtail命令中加上–one_shot选项来执行,也可以查看度量指标。
version: '3'
services:
mtail-checker:
image: dylanmei/mtail
container_name: mtail-checker
hostname: mtail-checker
volumes:
- ./logs:/logs/
- ./mtail/:/mtail
entrypoint:
- mtail
- --one_shot
- --progs
- /mtail/example.mtail
- --logs
- /logs/example.log
$ sudo docker-compose run --rm mtail-checker
Creating mtail_mtail-checker_run ... done
Metrics store:{
"debug_flg": [
{
"Name": "debug_flg",
"Program": "example.mtail",
"Kind": 2,
"Type": 0
}
],
"error_flg_syntax": [
{
"Name": "error_flg_syntax",
"Program": "example.mtail",
"Kind": 2,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182670126
}
}
]
}
],
"fatal_flg_de": [
{
"Name": "fatal_flg_de",
"Program": "example.mtail",
"Kind": 2,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182861408
}
}
]
}
],
"fatal_flg_kp": [
{
"Name": "fatal_flg_kp",
"Program": "example.mtail",
"Kind": 2,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182882874
}
}
]
}
],
"fatal_flg_oom": [
{
"Name": "fatal_flg_oom",
"Program": "example.mtail",
"Kind": 2,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182722713
}
}
]
}
],
"line_count_debug": [
{
"Name": "line_count_debug",
"Program": "example.mtail",
"Kind": 1,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 0,
"Time": 0
}
}
]
}
],
"line_count_error": [
{
"Name": "line_count_error",
"Program": "example.mtail",
"Kind": 1,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 3,
"Time": 1623583432182657661
}
}
]
}
],
"line_count_fatal": [
{
"Name": "line_count_fatal",
"Program": "example.mtail",
"Kind": 1,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 4,
"Time": 1623583432182874744
}
}
]
}
],
"line_count_info": [
{
"Name": "line_count_info",
"Program": "example.mtail",
"Kind": 1,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182384349
}
}
]
}
],
"line_count_warn": [
{
"Name": "line_count_warn",
"Program": "example.mtail",
"Kind": 1,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 2,
"Time": 1623583432182542428
}
}
]
}
],
"warn_flg_no_declared": [
{
"Name": "warn_flg_no_declared",
"Program": "example.mtail",
"Kind": 2,
"Type": 0,
"LabelValues": [
{
"Value": {
"Value": 1,
"Time": 1623583432182558035
}
}
]
}
]
}
其他的出口商使用方式
-
- MySQL Exporter with Docker
- Postgres Exporter with Docker