cleaner interface and better error control
Costa Tsaousis (ktsaou) committed
Nov 21, 2015 at 02:19 UTC
4db5a28c76a2bd15e128f1b07fc842c2934f4bfd
1 file changed
+138
-105
charts.d/mysql.chart.sh
+138
-105
@@ -11,7 +11,7 @@ mysql_update_every=5
11
12
declare -A mysql_cmds=() mysql_opts=() mysql_ids=()
13
14
-mysql_get() {
14
+mysql_exec() {
15
local ret
16
17
"${@}" -s -e "show global status;"
@@ -21,6 +21,91 @@ mysql_get() {
21
return $ret
22
}
23
24
+mysql_get() {
25
+ unset \
26
+ mysql_Bytes_received \
27
+ mysql_Bytes_sent \
28
+ mysql_Queries \
29
+ mysql_Questions \
30
+ mysql_Slow_queries \
31
+ mysql_Handler_commit \
32
+ mysql_Handler_delete \
33
+ mysql_Handler_prepare \
34
+ mysql_Handler_read_first \
35
+ mysql_Handler_read_key \
36
+ mysql_Handler_read_next \
37
+ mysql_Handler_read_prev \
38
+ mysql_Handler_read_rnd \
39
+ mysql_Handler_read_rnd_next \
40
+ mysql_Handler_rollback \
41
+ mysql_Handler_savepoint \
42
+ mysql_Handler_savepoint_rollback \
43
+ mysql_Handler_update \
44
+ mysql_Handler_write \
45
+ mysql_Table_locks_immediate \
46
+ mysql_Table_locks_waited \
47
+ mysql_Select_full_join \
48
+ mysql_Select_full_range_join \
49
+ mysql_Select_range \
50
+ mysql_Select_range_check \
51
+ mysql_Select_scan \
52
+ mysql_Sort_merge_passes \
53
+ mysql_Sort_range \
54
+ mysql_Sort_scan \
55
+ mysql_Created_tmp_disk_tables \
56
+ mysql_Created_tmp_files \
57
+ mysql_Created_tmp_tables \
58
+ mysql_Connection_errors_accept \
59
+ mysql_Connection_errors_internal \
60
+ mysql_Connection_errors_max_connections \
61
+ mysql_Connection_errors_peer_addr \
62
+ mysql_Connection_errors_select \
63
+ mysql_Connection_errors_tcpwrap \
64
+ mysql_Connections \
65
+ mysql_Aborted_connects \
66
+ mysql_Binlog_cache_disk_use \
67
+ mysql_Binlog_cache_use \
68
+ mysql_Binlog_stmt_cache_disk_use \
69
+ mysql_Binlog_stmt_cache_use \
70
+ mysql_Threads_connected \
71
+ mysql_Threads_created \
72
+ mysql_Threads_cached \
73
+ mysql_Threads_running \
74
+ mysql_Innodb_data_read \
75
+ mysql_Innodb_data_written \
76
+ mysql_Innodb_data_reads \
77
+ mysql_Innodb_data_writes \
78
+ mysql_Innodb_log_waits \
79
+ mysql_Innodb_log_write_requests \
80
+ mysql_Innodb_log_writes \
81
+ mysql_Innodb_os_log_fsyncs \
82
+ mysql_Innodb_os_log_pending_fsyncs \
83
+ mysql_Innodb_os_log_pending_writes \
84
+ mysql_Innodb_os_log_written \
85
+ mysql_Innodb_row_lock_current_waits \
86
+ mysql_Innodb_rows_inserted \
87
+ mysql_Innodb_rows_read \
88
+ mysql_Innodb_rows_updated \
89
+ mysql_Innodb_rows_deleted
90
+
91
+ mysql_plugin_command_failure=0
92
+
93
+ eval "$(mysql_exec "${@}" |\
94
+ sed \
95
+ -e "s/[[:space:]]\+/ /g" \
96
+ -e "s/\./_/g" \
97
+ -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/mysql_\1=\2/g" |\
98
+ egrep "^mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
99
+
100
+ [ $mysql_plugin_command_failure -eq 1 ] && return 1
101
+ [ -z "$mysql_Connections" ] && return 1
102
+
103
+ mysql_Thread_cache_misses=0
104
+ [ $(( mysql_Connections + 1 - 1 )) -gt 0 ] && mysql_Thread_cache_misses=$(( mysql_Threads_created * 10000 / mysql_Connections ))
105
+
106
+ return 0
107
+}
108
+
109
mysql_check() {
110
# this should return:
111
# - 0 to enable the chart
@@ -45,8 +130,8 @@ mysql_check() {
130
echo >&2 "$PROGRAM_NAME: mysql: cannot get mysql command for '$m'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
131
fi
132
48
- x="$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} | grep "^Connections[[:space:]]")"
49
- if [ ! $? -eq 0 -o -z "$x" ]
133
+ mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
134
+ if [ ! $? -eq 0 ]
135
then
136
echo >&2 "$PROGRAM_NAME: mysql: cannot get global status for '$m'. Please set mysql_opts[$m]='options' to whatever needed to get connected to the mysql server, in $confd/mysql.conf"
137
unset mysql_cmds[$m]
@@ -120,14 +205,6 @@ DIMENSION Created_tmp_disk_tables disk_tables incremental 1 $((1 * mysql_update_
205
DIMENSION Created_tmp_files files incremental 1 $((1 * mysql_update_every))
206
DIMENSION Created_tmp_tables tables incremental 1 $((1 * mysql_update_every))
207
123
-CHART mysql_$m.connection_errors '' "mysql Connection Errors" "connections/s" mysql_$m mysql line 20008 $mysql_update_every
124
-DIMENSION Connection_errors_accept accept incremental 1 $((1 * mysql_update_every))
125
-DIMENSION Connection_errors_internal internal incremental 1 $((1 * mysql_update_every))
126
-DIMENSION Connection_errors_max_connections max incremental 1 $((1 * mysql_update_every))
127
-DIMENSION Connection_errors_peer_addr peer_addr incremental 1 $((1 * mysql_update_every))
128
-DIMENSION Connection_errors_select select incremental 1 $((1 * mysql_update_every))
129
-DIMENSION Connection_errors_tcpwrap tcpwrap incremental 1 $((1 * mysql_update_every))
130
-
208
CHART mysql_$m.connections '' "mysql Connections" "connections/s" mysql_$m mysql line 20009 $mysql_update_every
209
DIMENSION Connections all incremental 1 $((1 * mysql_update_every))
210
DIMENSION Aborted_connects aborded incremental 1 $((1 * mysql_update_every))
@@ -136,10 +213,6 @@ CHART mysql_$m.binlog_cache '' "mysql Binlog Cache" "transactions/s" mysql_$m my
213
DIMENSION Binlog_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
214
DIMENSION Binlog_cache_use all incremental 1 $((1 * mysql_update_every))
215
139
-CHART mysql_$m.binlog_stmt_cache '' "mysql Binlog Statement Cache" "statements/s" mysql_$m mysql line 20011 $mysql_update_every
140
-DIMENSION Binlog_stmt_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
141
-DIMENSION Binlog_stmt_cache_use all incremental 1 $((1 * mysql_update_every))
142
-
216
CHART mysql_$m.threads '' "mysql Threads" "threads" mysql_$m mysql line 20012 $mysql_update_every
217
DIMENSION Threads_connected connected absolute 1 $((1 * mysql_update_every))
218
DIMENSION Threads_created created incremental 1 $((1 * mysql_update_every))
@@ -180,6 +253,29 @@ DIMENSION Innodb_rows_inserted inserted incremental 1 $((1 * mysql_update_every)
253
DIMENSION Innodb_rows_updated updated incremental -1 $((1 * mysql_update_every))
254
255
EOF
256
+
257
+ if [ ! -z "$mysql_Binlog_stmt_cache_disk_use" ]
258
+ then
259
+ cat <<EOF
260
+CHART mysql_$m.binlog_stmt_cache '' "mysql Binlog Statement Cache" "statements/s" mysql_$m mysql line 20011 $mysql_update_every
261
+DIMENSION Binlog_stmt_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
262
+DIMENSION Binlog_stmt_cache_use all incremental 1 $((1 * mysql_update_every))
263
+EOF
264
+ fi
265
+
266
+ if [ ! -z "$mysql_Connection_errors_accept" ]
267
+ then
268
+ cat <<EOF
269
+CHART mysql_$m.connection_errors '' "mysql Connection Errors" "connections/s" mysql_$m mysql line 20008 $mysql_update_every
270
+DIMENSION Connection_errors_accept accept incremental 1 $((1 * mysql_update_every))
271
+DIMENSION Connection_errors_internal internal incremental 1 $((1 * mysql_update_every))
272
+DIMENSION Connection_errors_max_connections max incremental 1 $((1 * mysql_update_every))
273
+DIMENSION Connection_errors_peer_addr peer_addr incremental 1 $((1 * mysql_update_every))
274
+DIMENSION Connection_errors_select select incremental 1 $((1 * mysql_update_every))
275
+DIMENSION Connection_errors_tcpwrap tcpwrap incremental 1 $((1 * mysql_update_every))
276
+EOF
277
+ fi
278
+
279
done
280
return 0
281
}
@@ -209,79 +305,8 @@ mysql_update() {
305
do
306
x="${mysql_ids[$m]}"
307
212
- unset \
213
- mysql_Bytes_received \
214
- mysql_Bytes_sent \
215
- mysql_Queries \
216
- mysql_Questions \
217
- mysql_Slow_queries \
218
- mysql_Handler_commit \
219
- mysql_Handler_delete \
220
- mysql_Handler_prepare \
221
- mysql_Handler_read_first \
222
- mysql_Handler_read_key \
223
- mysql_Handler_read_next \
224
- mysql_Handler_read_prev \
225
- mysql_Handler_read_rnd \
226
- mysql_Handler_read_rnd_next \
227
- mysql_Handler_rollback \
228
- mysql_Handler_savepoint \
229
- mysql_Handler_savepoint_rollback \
230
- mysql_Handler_update \
231
- mysql_Handler_write \
232
- mysql_Table_locks_immediate \
233
- mysql_Table_locks_waited \
234
- mysql_Select_full_join \
235
- mysql_Select_full_range_join \
236
- mysql_Select_range \
237
- mysql_Select_range_check \
238
- mysql_Select_scan \
239
- mysql_Sort_merge_passes \
240
- mysql_Sort_range \
241
- mysql_Sort_scan \
242
- mysql_Created_tmp_disk_tables \
243
- mysql_Created_tmp_files \
244
- mysql_Created_tmp_tables \
245
- mysql_Connection_errors_accept \
246
- mysql_Connection_errors_internal \
247
- mysql_Connection_errors_max_connections \
248
- mysql_Connection_errors_peer_addr \
249
- mysql_Connection_errors_select \
250
- mysql_Connection_errors_tcpwrap \
251
- mysql_Connections \
252
- mysql_Aborted_connects \
253
- mysql_Binlog_cache_disk_use \
254
- mysql_Binlog_cache_use \
255
- mysql_Binlog_stmt_cache_disk_use \
256
- mysql_Binlog_stmt_cache_use \
257
- mysql_Threads_connected \
258
- mysql_Threads_created \
259
- mysql_Threads_cached \
260
- mysql_Threads_running \
261
- mysql_Innodb_data_read \
262
- mysql_Innodb_data_written \
263
- mysql_Innodb_data_reads \
264
- mysql_Innodb_data_writes \
265
- mysql_Innodb_log_waits \
266
- mysql_Innodb_log_write_requests \
267
- mysql_Innodb_log_writes \
268
- mysql_Innodb_os_log_fsyncs \
269
- mysql_Innodb_os_log_pending_fsyncs \
270
- mysql_Innodb_os_log_pending_writes \
271
- mysql_Innodb_os_log_written \
272
- mysql_Innodb_row_lock_current_waits \
273
- mysql_Innodb_rows_inserted \
274
- mysql_Innodb_rows_read \
275
- mysql_Innodb_rows_updated \
276
- mysql_Innodb_rows_deleted
277
-
278
- mysql_plugin_command_failure=0
279
-
280
- eval "$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} |\
281
- sed -e "s/[[:space:]]\+/ /g" -e "s/\./_/g" -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/local mysql_\1=\2/g" |\
282
- egrep "^local mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
283
-
284
- if [ $mysql_plugin_command_failure -ne 0 ]
308
+ mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
309
+ if [ $? -ne 0 ]
310
then
311
unset mysql_ids[$m]
312
unset mysql_opts[$m]
@@ -290,10 +315,6 @@ mysql_update() {
315
continue
316
fi
317
293
- local misses=0
294
- [ -z "$mysql_Connections" ] && mysql_Connections=0
295
- [ $(( mysql_Connections + 1 - 1 )) -gt 0 ] && misses=$(( mysql_Threads_created * 10000 / mysql_Connections ))
296
-
318
# write the result of the work.
319
cat <<VALUESEOF
320
BEGIN mysql_$x.bandwidth $1
@@ -342,14 +363,6 @@ SET Created_tmp_disk_tables = $mysql_Created_tmp_disk_tables
363
SET Created_tmp_files = $mysql_Created_tmp_files
364
SET Created_tmp_tables = $mysql_Created_tmp_tables
365
END
345
-BEGIN mysql_$m.connection_errors $1
346
-SET Connection_errors_accept = $mysql_Connection_errors_accept
347
-SET Connection_errors_internal = $mysql_Connection_errors_internal
348
-SET Connection_errors_max_connections = $mysql_Connection_errors_max_connections
349
-SET Connection_errors_peer_addr = $mysql_Connection_errors_peer_addr
350
-SET Connection_errors_select = $mysql_Connection_errors_select
351
-SET Connection_errors_tcpwrap = $mysql_Connection_errors_tcpwrap
352
-END
366
BEGIN mysql_$m.connections $1
367
SET Connections = $mysql_Connections
368
SET Aborted_connects = $mysql_Aborted_connects
@@ -358,10 +371,6 @@ BEGIN mysql_$m.binlog_cache $1
371
SET Binlog_cache_disk_use = $mysql_Binlog_cache_disk_use
372
SET Binlog_cache_use = $mysql_Binlog_cache_use
373
END
361
-BEGIN mysql_$m.binlog_stmt_cache $1
362
-SET Binlog_stmt_cache_disk_use = $mysql_Binlog_stmt_cache_disk_use
363
-SET Binlog_stmt_cache_use = $mysql_Binlog_stmt_cache_use
364
-END
374
BEGIN mysql_$m.threads $1
375
SET Threads_connected = $mysql_Threads_connected
376
SET Threads_created = $mysql_Threads_created
@@ -369,7 +378,7 @@ SET Threads_cached = $mysql_Threads_cached
378
SET Threads_running = $mysql_Threads_running
379
END
380
BEGIN mysql_$m.thread_cache_misses $1
372
-SET misses = $misses
381
+SET misses = $mysql_Thread_cache_misses
382
END
383
BEGIN mysql_$m.innodb_io $1
384
SET Innodb_data_read = $mysql_Innodb_data_read
@@ -402,6 +411,30 @@ SET Innodb_rows_updated = $mysql_Innodb_rows_updated
411
SET Innodb_rows_deleted = $mysql_Innodb_rows_deleted
412
END
413
VALUESEOF
414
+
415
+ if [ ! -z "$mysql_Binlog_stmt_cache_disk_use" ]
416
+ then
417
+ cat <<VALUESEOF
418
+BEGIN mysql_$m.binlog_stmt_cache $1
419
+SET Binlog_stmt_cache_disk_use = $mysql_Binlog_stmt_cache_disk_use
420
+SET Binlog_stmt_cache_use = $mysql_Binlog_stmt_cache_use
421
+END
422
+VALUESEOF
423
+ fi
424
+
425
+ if [ ! -z "$mysql_Connection_errors_accept" ]
426
+ then
427
+ cat <<VALUESEOF
428
+BEGIN mysql_$m.connection_errors $1
429
+SET Connection_errors_accept = $mysql_Connection_errors_accept
430
+SET Connection_errors_internal = $mysql_Connection_errors_internal
431
+SET Connection_errors_max_connections = $mysql_Connection_errors_max_connections
432
+SET Connection_errors_peer_addr = $mysql_Connection_errors_peer_addr
433
+SET Connection_errors_select = $mysql_Connection_errors_select
434
+SET Connection_errors_tcpwrap = $mysql_Connection_errors_tcpwrap
435
+END
436
+VALUESEOF
437
+ fi
438
done
439
440
[ ${#mysql_ids[@]} -eq 0 ] && echo >&2 "$PROGRAM_NAME: mysql: no mysql servers left active." && return 1