Prometheus监控MySQL慢查询

mtail是一种工具,用于从要导出的应用程序日志中提取指标,放入时间序列数据库或时间序列计算器中,用于警报和仪表板。简单来说,就是实时读取应用程序的日志,并且通过自己编写的脚本实时分析,最终生成时间序列指标的工具。

开启 MySQL 慢日志

临时开启,使用SQL语句开启

1
2
3
4
5
6
## 开启慢日志
SET GLOBAL slow_query_log = ON;
## 超过 2 秒记为慢 SQL(根据业务情况调整)
SET GLOBAL long_query_time = 2;
## 记录未使用索引的 SQL(慎用,排错时再开)
SET GLOBAL log_queries_not_using_indexes = ON;

永久开启,写入配置文件需要重启mysql服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[mysqld]
# 慢查询日志
slow_query_log = ON
long_query_time = 2
slow_query_log_file = /var/log/mysql/slow.log

# 可选(排错用,不建议常开)
# log_queries_not_using_indexes = ON

# 记录慢管理的语句(ALTER/OPTIMIZE 等)
log_slow_admin_statements = ON

# 记录从库复制的慢 SQL
log_slow_slave_statements = ON

mtail 解析慢日志sql

下载mtail

1
2
3
# wget https://github.com/google/mtail/releases/download/v3.0.8/mtail_3.0.8_linux_amd64.tar.gz
# tar xf mtail_3.0.8_linux_amd64.tar.gz
# cd mtail_3.0.8_linux_amd64

编辑mtail用于解析mysql的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# vim slow_sql.mtail

# Copyright 2008 Google Inc. All Rights Reserved.
# This file is available under the Apache license.

# mysql-slowqueries -- mtail module tracking slow mysql queries

hidden text user
hidden text host
hidden text query_type
hidden text query_sql
hidden text query_process_id
hidden text query_timestamp
hidden text query_slow_time
hidden gauge tmp_query_time
hidden gauge tmp_lock_time
hidden gauge partial
hidden gauge time
hidden gauge query_timestamp_gauge
counter mysql_query_time by host, query_timestamp, query_slow_time, query_sql, user, type
counter mysql_lock_time by host, query_timestamp, query_slow_time, query_sql, user, type
counter mysql_slow_query_detail by host, query_process_id, query_timestamp, query_slow_time, query_sql, user, type
counter query_time_overall_sum
counter query_time_total_count
counter lock_time_overall_sum
counter lock_time_total_count

# Example lines from the file and regex to match them:

# # User@Host: dbuser[dbuser] @ host [192.0.2.87]
const USER_HOST /^# User@Host: ([a-zA-Z]+)\[[a-zA-Z]+\] @\s+\[(\d+(?:\.\d+){3})\]\s+Id:\s*(\d+)/

# # Query_time: 30 Lock_time: 0 Rows_sent: 0 Rows_examined: 0
const QUERY_TIME /^# Query_time: (\d+\.?\d*)\s+Lock_time: (\d+\.?\d*)/

# UPDATE ... # outbox;
const FULL_QUERY_LINE /^(INSERT|UPDATE|DELETE|SELECT) (.*);$/

# Not all queries have helpful comments at the end
const UNINSTRUMENTED_QUERY_LINE /^(INSERT|UPDATE|DELETE|SELECT) .*;$/

# If the query gets split up, the query_sql may end up on another line
const PARTIAL_QUERY_LINE /^(INSERT|UPDATE|DELETE|SELECT) .*[^;]$/

# This one has the potential to catch too many things, so it can only be a last
# resort match.
const END_QUERY_LINE /.*;$/

/^# Time: (\d{6} .\d:\d\d:\d\d)/ {
strptime($1, "060102 3:04:05")
time = timestamp()
}

/^SET timestamp=(\d+);/ {
time = $1
query_timestamp = $1
query_timestamp_gauge = $1
}

settime(time)

// + USER_HOST {
user = $1
query_process_id = $3
host = $2
}
# break if no user set yet
user == "" {
stop
}

// + QUERY_TIME {
tmp_query_time = $1
query_slow_time = $1
tmp_lock_time = $2
query_time_overall_sum += tmp_query_time
query_time_total_count++
lock_time_overall_sum += tmp_lock_time
lock_time_total_count++
}

// + FULL_QUERY_LINE {
# We should have everything we need now.
query_type = tolower($1)
query_sql = $1 + " "+ $2
mysql_query_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_slow_query_detail[host, query_process_id, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_lock_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
}

// + UNINSTRUMENTED_QUERY_LINE {
# We should have everything we need now.
query_type = tolower($1)
query_sql = "n/a"
mysql_query_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_slow_query_detail[host, query_process_id, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_lock_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
}

// + PARTIAL_QUERY_LINE {
query_type = tolower($1)
partial = 1
}

// + END_QUERY_LINE && partial == 1 {
partial = 0
/.*# (.*)$/ {
query_sql = $1
}
otherwise {
query_sql = "n/a"
}
mysql_query_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_slow_query_detail[host, query_process_id, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
mysql_lock_time[host, query_timestamp, query_slow_time, query_sql, user ,query_type] += query_timestamp_gauge
}
#counter mysql_slow_query_duration_seconds by user
#
## 采集查询的用户
#/^# User@Host: ([^[]+)\[.*\] @ \[(.*)\] Id: .*/ {
# mysql_slow_query_duration_seconds[$user]++
#}
##
#/^# Query_time: ([0-9.]+) Lock_time: ([0-9.]+) Rows_sent: [0-9]+ Rows_examined: [0-9]+/ {
# query_time = $1
# lock_time = $2
# # 转换启动时间(如需要)可以在这里做
# # 时间解析规则可依据实际日志
# # 这里假设用后续的 SET timestamp 行的时间
#}
#
#/^SET timestamp=([0-9]+);/ {
# timestamp = $1
#}
#
#/^# Time: ([^+]+)\+/ {
# start_time_str = $1
# # 这里可以用后续处理将字符串时间转换为Unix时间戳
# # 或直接采集这个字符串
# start_time_str_clipped = start_time_str
#}
#
##/^(UPDATE|SELECT|INSERT|DELETE) (.*?) ;$/ {
## sql_statement = $0
##}
#
##/* 你可以定义一些挂载点,输出 Prometheus metric */
#
## 示例:将查询耗时、用户、锁时间、SQL语句作为指标暴露
##/^[^#]/ {
## console::gauge("mysql_slow_query_duration_seconds", query_time) {
## #labels = {
## # "user": user,
## # "sql": sql_statement,
## # "lock_time": lock_time,
## # "start_time": start_time_str_clipped
## }
## set(labels, query_time)
## }
##}
#
#/^(UPDATE|SELECT|INSERT|DELETE) (.*?) ;$/ {
# sql_statement = $0
# # 生成指标
# console::gauge("mysql_slow_query_duration_seconds", query_time) {
# labels = {
# "user": user,
# "sql": sql_statement,
# "lock_time": lock_time,
# "start_time": start_time_str_clipped
# }
# set(labels, query_time)
# }
#}

/^# User@Host: ([^[]+)\[.*\] @ \[(.*)\] Id: .*/ {
user = $1
}

# 采集查询时间
/^# Query_time: ([0-9.]+) Lock_time: ([0-9.]+) Rows_sent: [0-9]+ Rows_examined: [0-9]+/ {
query_time = $1
lock_time = $2
}

# 采集SQL语句
/^(UPDATE|SELECT|INSERT|DELETE) .*/ {
sql_statement = $0
# 生成指标
console::gauge("mysql_slow_query_duration_seconds", query_time) {
labels = {
"user": user,
#"sql": sql_statement,
"lock_time": lock_time,
"start_time": start_time_str_clipped
}
set(labels, query_time)
}
}

启动 mtaile 服务

1
# nohup ./mtail -logs /data/mysqldata/logs/slow.log -progs ./slow_sql.mtail --logtostderr &

prometheus 采集监控数据

prometheus配置文件添加采集 mtail监控数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# vim prometheus.yml

- job_name: mysql-slow-sql
honor_timestamps: true
scrape_interval: 10s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- 10.183.33.99:3903
labels:
alerttype: mysql-slow-sql
host_ip: 10.183.33.99
job: mysql-slow-sql
product: 北京会员权益
province: 北京
- targets:
- 10.183.33.108:3903
labels:
alerttype: mysql-slow-sql
host_ip: 10.183.33.108
job: mysql-slow-sql
product: 北京会员权益
province: 北京

prometheus 配置慢SQL告警规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# vim rules/alert_rules_slow_sql.yml
name: MySQL慢查询
expr: time() - mysql_slow_query_detail{query_sql!~"n/a"} <= 300 and time() - mysql_slow_query_detail{query_sql!~"n/a"} >= 0
for: 1m
labels:
alert_host: {{ reReplaceAll ":(.*)" "" $labels.host_ip }}
alert_notice: once
alert_type: MySQL慢查询告警
severity: low
team: dwz-gt-monitor
annotations:
description: {{ reReplaceAll ":(.*)" "" $labels.host_ip }} {{ $labels.alerttype}}
用户: {{ $labels.user }}
执行时间戳: {{ $labels.query_timestamp }}
耗时: {{ $labels.query_slow_time }}
SQL语句: {{ $labels.query_sql }}
summary: {{ reReplaceAll ":(.*)" "" $labels.host_ip }} : {{ $labels.type}} 慢SQL

动态加载配置文件

1
# curl -X POST http://127.0.0.1:9090/-/reload
-------------本文结束感谢您的阅读-------------