]> arthur.barton.de Git - netdata.git/blob - charts.d/mysql.chart.sh
added more values
[netdata.git] / charts.d / mysql.chart.sh
1 #!/bin/sh
2
3 # http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html
4 #
5 # https://dev.mysql.com/doc/refman/5.1/en/show-status.html
6 # SHOW STATUS provides server status information (see Section 5.1.6, “Server Status Variables”).
7 # This statement does not require any privilege.
8 # It requires only the ability to connect to the server.
9
10 mysql_update_every=5
11
12 declare -A mysql_cmds=() mysql_opts=() mysql_ids=()
13
14 mysql_exec() {
15         local ret
16         
17         "${@}" -s -e "show global status;"
18         ret=$?
19
20         [ $ret -ne 0 ] && echo "plugin_command_failure $ret"
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_data_fsyncs \
79                 mysql_Innodb_data_pending_reads \
80                 mysql_Innodb_data_pending_writes \
81                 mysql_Innodb_data_pending_fsyncs \
82                 mysql_Innodb_log_waits \
83                 mysql_Innodb_log_write_requests \
84                 mysql_Innodb_log_writes \
85                 mysql_Innodb_os_log_fsyncs \
86                 mysql_Innodb_os_log_pending_fsyncs \
87                 mysql_Innodb_os_log_pending_writes \
88                 mysql_Innodb_os_log_written \
89                 mysql_Innodb_row_lock_current_waits \
90                 mysql_Innodb_rows_inserted \
91                 mysql_Innodb_rows_read \
92                 mysql_Innodb_rows_updated \
93                 mysql_Innodb_rows_deleted
94
95         mysql_plugin_command_failure=0
96
97         eval "$(mysql_exec "${@}" |\
98                 sed \
99                         -e "s/[[:space:]]\+/ /g" \
100                         -e "s/\./_/g" \
101                         -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/mysql_\1=\2/g" |\
102                 egrep "^mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
103
104         [ $mysql_plugin_command_failure -eq 1 ] && return 1
105         [ -z "$mysql_Connections" ] && return 1
106
107         mysql_Thread_cache_misses=0
108         [ $(( mysql_Connections + 1 - 1 )) -gt 0 ] && mysql_Thread_cache_misses=$(( mysql_Threads_created * 10000 / mysql_Connections ))
109
110         return 0
111 }
112
113 mysql_check() {
114         # this should return:
115         #  - 0 to enable the chart
116         #  - 1 to disable the chart
117
118         local x m mysql_cmd
119
120         [ -z "${mysql_cmd}" ] && mysql_cmd="$(which mysql)"
121
122         if [ ${#mysql_opts[@]} -eq 0 ]
123                 then
124                 mysql_cmds[local]="$mysql_cmd"
125                 mysql_opts[local]=
126         fi
127
128         # check once if the url works
129         for m in "${!mysql_opts[@]}"
130         do
131                 [ -z "${mysql_cmds[$m]}" ] && mysql_cmds[$m]="$mysql_cmd"
132                 if [ -z "${mysql_cmds[$m]}" ]
133                         then
134                         echo >&2 "$PROGRAM_NAME: mysql: cannot get mysql command for '$m'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
135                 fi
136
137                 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
138                 if [ ! $? -eq 0 ]
139                 then
140                         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"
141                         unset mysql_cmds[$m]
142                         unset mysql_opts[$m]
143                         unset mysql_ids[$m]
144                         continue
145                 fi
146
147                 mysql_ids[$m]="$( fixid "$m" )"
148         done
149
150         if [ ${#mysql_opts[@]} -eq 0 ]
151                 then
152                 echo >&2 "$PROGRAM_NAME: mysql: no mysql servers found. Please set mysql_opts[name]='options' to whatever needed to get connected to the mysql server, in $confd/mysql.conf"
153                 return 1
154         fi
155
156         return 0
157 }
158
159 mysql_create() {
160         local m
161
162         # create the charts
163         for m in "${mysql_ids[@]}"
164         do
165                 cat <<EOF
166 CHART mysql_$m.bandwidth '' "mysql Bandwidth" "kilobits/s" mysql_$m mysql area 20001 $mysql_update_every
167 DIMENSION Bytes_received in incremental 8 $((1024 * mysql_update_every))
168 DIMENSION Bytes_sent out incremental -8 $((1024 * mysql_update_every))
169
170 CHART mysql_$m.queries '' "mysql Queries" "queries/s" mysql_$m mysql line 20002 $mysql_update_every
171 DIMENSION Queries queries incremental 1 $((1 * mysql_update_every))
172 DIMENSION Questions questions incremental 1 $((1 * mysql_update_every))
173 DIMENSION Slow_queries slow_queries incremental -1 $((1 * mysql_update_every))
174
175 CHART mysql_$m.handlers '' "mysql Handlers" "handlers/s" mysql_$m mysql line 20003 $mysql_update_every
176 DIMENSION Handler_commit commit incremental 1 $((1 * mysql_update_every))
177 DIMENSION Handler_delete delete incremental 1 $((1 * mysql_update_every))
178 DIMENSION Handler_prepare prepare incremental 1 $((1 * mysql_update_every))
179 DIMENSION Handler_read_first read_first incremental 1 $((1 * mysql_update_every))
180 DIMENSION Handler_read_key read_key incremental 1 $((1 * mysql_update_every))
181 DIMENSION Handler_read_next read_next incremental 1 $((1 * mysql_update_every))
182 DIMENSION Handler_read_prev read_prev incremental 1 $((1 * mysql_update_every))
183 DIMENSION Handler_read_rnd read_rnd incremental 1 $((1 * mysql_update_every))
184 DIMENSION Handler_read_rnd_next read_rnd_next incremental 1 $((1 * mysql_update_every))
185 DIMENSION Handler_rollback rollback incremental 1 $((1 * mysql_update_every))
186 DIMENSION Handler_savepoint savepoint incremental 1 $((1 * mysql_update_every))
187 DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mysql_update_every))
188 DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
189 DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
190
191 CHART mysql_$m.table_locks '' "mysql Tables Locks" "locks/s" mysql_$m mysql line 20004 $mysql_update_every
192 DIMENSION Table_locks_immediate immediate incremental 1 $((1 * mysql_update_every))
193 DIMENSION Table_locks_waited waited incremental -1 $((1 * mysql_update_every))
194
195 CHART mysql_$m.join_issues '' "mysql Select Join Issues" "joins/s" mysql_$m mysql line 20005 $mysql_update_every
196 DIMENSION Select_full_join full_join incremental 1 $((1 * mysql_update_every))
197 DIMENSION Select_full_range_join full_range_join incremental 1 $((1 * mysql_update_every))
198 DIMENSION Select_range range incremental 1 $((1 * mysql_update_every))
199 DIMENSION Select_range_check range_check incremental 1 $((1 * mysql_update_every))
200 DIMENSION Select_scan scan incremental 1 $((1 * mysql_update_every))
201
202 CHART mysql_$m.sort_issues '' "mysql Sort Issues" "issues/s" mysql_$m mysql line 20006 $mysql_update_every
203 DIMENSION Sort_merge_passes merge_passes incremental 1 $((1 * mysql_update_every))
204 DIMENSION Sort_range range incremental 1 $((1 * mysql_update_every))
205 DIMENSION Sort_scan scan incremental 1 $((1 * mysql_update_every))
206
207 CHART mysql_$m.tmp '' "mysql Tmp Operations" "counter" mysql_$m mysql line 20007 $mysql_update_every
208 DIMENSION Created_tmp_disk_tables disk_tables incremental 1 $((1 * mysql_update_every))
209 DIMENSION Created_tmp_files files incremental 1 $((1 * mysql_update_every))
210 DIMENSION Created_tmp_tables tables incremental 1 $((1 * mysql_update_every))
211
212 CHART mysql_$m.connections '' "mysql Connections" "connections/s" mysql_$m mysql line 20009 $mysql_update_every
213 DIMENSION Connections all incremental 1 $((1 * mysql_update_every))
214 DIMENSION Aborted_connects aborded incremental 1 $((1 * mysql_update_every))
215
216 CHART mysql_$m.binlog_cache '' "mysql Binlog Cache" "transactions/s" mysql_$m mysql line 20010 $mysql_update_every
217 DIMENSION Binlog_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
218 DIMENSION Binlog_cache_use all incremental 1 $((1 * mysql_update_every))
219
220 CHART mysql_$m.threads '' "mysql Threads" "threads" mysql_$m mysql line 20012 $mysql_update_every
221 DIMENSION Threads_connected connected absolute 1 1
222 DIMENSION Threads_created created incremental 1 $((1 * mysql_update_every))
223 DIMENSION Threads_cached cached absolute -1 1
224 DIMENSION Threads_running running absolute 1 $((1 * mysql_update_every))
225
226 CHART mysql_$m.thread_cache_misses '' "mysql Threads Cache Misses" "misses" mysql_$m mysql area 20013 $mysql_update_every
227 DIMENSION misses misses absolute 1 100
228
229 CHART mysql_$m.innodb_io '' "mysql InnoDB I/O Bandwidth" "kilobytes/s" mysql_$m mysql area 20014 $mysql_update_every
230 DIMENSION Innodb_data_read read incremental 1 $((1000 * mysql_update_every))
231 DIMENSION Innodb_data_written write incremental -1 $((1000 * mysql_update_every))
232
233 CHART mysql_$m.innodb_io_ops '' "mysql InnoDB I/O Operations" "operations/s" mysql_$m mysql line 20015 $mysql_update_every
234 DIMENSION Innodb_data_reads reads incremental 1 $((1 * mysql_update_every))
235 DIMENSION Innodb_data_writes writes incremental -1 $((1 * mysql_update_every))
236 DIMENSION Innodb_data_fsyncs fsyncs incremental 1 $((1 * mysql_update_every))
237
238 CHART mysql_$m.innodb_io_pending_ops '' "mysql InnoDB Pending I/O Operations" "operations/s" mysql_$m mysql line 20015 $mysql_update_every
239 DIMENSION Innodb_data_pending_reads reads absolute 1 1
240 DIMENSION Innodb_data_pending_writes writes absolute -1 1
241 DIMENSION Innodb_data_pending_fsyncs fsyncs absolute 1 1
242
243 CHART mysql_$m.innodb_log '' "mysql InnoDB Log Operations" "operations/s" mysql_$m mysql line 20016 $mysql_update_every
244 DIMENSION Innodb_log_waits waits incremental 1 $((1 * mysql_update_every))
245 DIMENSION Innodb_log_write_requests write_requests incremental -1 $((1 * mysql_update_every))
246 DIMENSION Innodb_log_writes writes incremental -1 $((1 * mysql_update_every))
247
248 CHART mysql_$m.innodb_os_log '' "mysql InnoDB OS Log Operations" "operations/s" mysql_$m mysql line 20017 $mysql_update_every
249 DIMENSION Innodb_os_log_fsyncs fsyncs incremental 1 $((1 * mysql_update_every))
250 DIMENSION Innodb_os_log_pending_fsyncs pending_fsyncs incremental 1 $((1 * mysql_update_every))
251 DIMENSION Innodb_os_log_pending_writes pending_writes incremental -1 $((1 * mysql_update_every))
252
253 CHART mysql_$m.innodb_os_log_io '' "mysql InnoDB OS Log Bandwidth" "kilobytes/s" mysql_$m mysql area 20018 $mysql_update_every
254 DIMENSION Innodb_os_log_written write incremental -1 $((1000 * mysql_update_every))
255
256 CHART mysql_$m.innodb_cur_row_lock '' "mysql InnoDB Current Row Locks" "operations" mysql_$m mysql area 20019 $mysql_update_every
257 DIMENSION Innodb_row_lock_current_waits current_waits absolute 1 1
258
259 CHART mysql_$m.innodb_rows '' "mysql InnoDB Row Operations" "operations/s" mysql_$m mysql area 20020 $mysql_update_every
260 DIMENSION Innodb_rows_read read incremental 1 $((1 * mysql_update_every))
261 DIMENSION Innodb_rows_deleted deleted incremental -1 $((1 * mysql_update_every))
262 DIMENSION Innodb_rows_inserted inserted incremental 1 $((1 * mysql_update_every))
263 DIMENSION Innodb_rows_updated updated incremental -1 $((1 * mysql_update_every))
264
265 EOF
266
267         if [ ! -z "$mysql_Binlog_stmt_cache_disk_use" ]
268                 then
269                 cat <<EOF
270 CHART mysql_$m.binlog_stmt_cache '' "mysql Binlog Statement Cache" "statements/s" mysql_$m mysql line 20011 $mysql_update_every
271 DIMENSION Binlog_stmt_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
272 DIMENSION Binlog_stmt_cache_use all incremental 1 $((1 * mysql_update_every))
273 EOF
274         fi
275
276         if [ ! -z "$mysql_Connection_errors_accept" ]
277                 then
278                 cat <<EOF
279 CHART mysql_$m.connection_errors '' "mysql Connection Errors" "connections/s" mysql_$m mysql line 20008 $mysql_update_every
280 DIMENSION Connection_errors_accept accept incremental 1 $((1 * mysql_update_every))
281 DIMENSION Connection_errors_internal internal incremental 1 $((1 * mysql_update_every))
282 DIMENSION Connection_errors_max_connections max incremental 1 $((1 * mysql_update_every))
283 DIMENSION Connection_errors_peer_addr peer_addr incremental 1 $((1 * mysql_update_every))
284 DIMENSION Connection_errors_select select incremental 1 $((1 * mysql_update_every))
285 DIMENSION Connection_errors_tcpwrap tcpwrap incremental 1 $((1 * mysql_update_every))
286 EOF
287         fi
288
289         done
290         return 0
291 }
292
293
294 mysql_update() {
295         # the first argument to this function is the microseconds since last update
296         # pass this parameter to the BEGIN statement (see bellow).
297
298         # do all the work to collect / calculate the values
299         # for each dimension
300         # remember: KEEP IT SIMPLE AND SHORT
301
302         # 1. get the counters page from mysql
303         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local mysql_
304         # 3. egrep lines starting with:
305         #    local mysql_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
306         #    local mysql_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
307         # 4. then execute this as a script with the eval
308         #
309         # be very carefull with eval:
310         # prepare the script and always grep at the end the lines that are usefull, so that
311         # even if something goes wrong, no other code can be executed
312
313         local m x
314         for m in "${!mysql_ids[@]}"
315         do
316                 x="${mysql_ids[$m]}"
317
318                 mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]}
319                 if [ $? -ne 0 ]
320                         then
321                         unset mysql_ids[$m]
322                         unset mysql_opts[$m]
323                         unset mysql_cmds[$m]
324                         echo >&2 "$PROGRAM_NAME: mysql: failed to get values for '$m', disabling it."
325                         continue
326                 fi
327
328                 # write the result of the work.
329                 cat <<VALUESEOF
330 BEGIN mysql_$x.bandwidth $1
331 SET Bytes_received = $mysql_Bytes_received
332 SET Bytes_sent = $mysql_Bytes_sent
333 END
334 BEGIN mysql_$x.queries $1
335 SET Queries = $mysql_Queries
336 SET Questions = $mysql_Questions
337 SET Slow_queries = $mysql_Slow_queries
338 END
339 BEGIN mysql_$x.handlers $1
340 SET Handler_commit = $mysql_Handler_commit
341 SET Handler_delete = $mysql_Handler_delete
342 SET Handler_prepare = $mysql_Handler_prepare
343 SET Handler_read_first = $mysql_Handler_read_first
344 SET Handler_read_key = $mysql_Handler_read_key
345 SET Handler_read_next = $mysql_Handler_read_next
346 SET Handler_read_prev = $mysql_Handler_read_prev
347 SET Handler_read_rnd = $mysql_Handler_read_rnd
348 SET Handler_read_rnd_next = $mysql_Handler_read_rnd_next
349 SET Handler_rollback = $mysql_Handler_rollback
350 SET Handler_savepoint = $mysql_Handler_savepoint
351 SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
352 SET Handler_update = $mysql_Handler_update
353 SET Handler_write = $mysql_Handler_write
354 END
355 BEGIN mysql_$x.table_locks $1
356 SET Table_locks_immediate = $mysql_Table_locks_immediate
357 SET Table_locks_waited = $mysql_Table_locks_waited
358 END
359 BEGIN mysql_$x.join_issues $1
360 SET Select_full_join = $mysql_Select_full_join
361 SET Select_full_range_join = $mysql_Select_full_range_join
362 SET Select_range = $mysql_Select_range
363 SET Select_range_check = $mysql_Select_range_check
364 SET Select_scan = $mysql_Select_scan
365 END
366 BEGIN mysql_$x.sort_issues $1
367 SET Sort_merge_passes = $mysql_Sort_merge_passes
368 SET Sort_range = $mysql_Sort_range
369 SET Sort_scan = $mysql_Sort_scan
370 END
371 BEGIN mysql_$m.tmp $1
372 SET Created_tmp_disk_tables = $mysql_Created_tmp_disk_tables
373 SET Created_tmp_files = $mysql_Created_tmp_files
374 SET Created_tmp_tables = $mysql_Created_tmp_tables
375 END
376 BEGIN mysql_$m.connections $1
377 SET Connections = $mysql_Connections
378 SET Aborted_connects = $mysql_Aborted_connects
379 END
380 BEGIN mysql_$m.binlog_cache $1
381 SET Binlog_cache_disk_use = $mysql_Binlog_cache_disk_use
382 SET Binlog_cache_use = $mysql_Binlog_cache_use
383 END
384 BEGIN mysql_$m.threads $1
385 SET Threads_connected = $mysql_Threads_connected
386 SET Threads_created = $mysql_Threads_created
387 SET Threads_cached = $mysql_Threads_cached
388 SET Threads_running = $mysql_Threads_running
389 END
390 BEGIN mysql_$m.thread_cache_misses $1
391 SET misses = $mysql_Thread_cache_misses
392 END
393 BEGIN mysql_$m.innodb_io $1
394 SET Innodb_data_read = $mysql_Innodb_data_read
395 SET Innodb_data_written = $mysql_Innodb_data_written
396 END
397 BEGIN mysql_$m.innodb_io_ops $1
398 SET Innodb_data_reads = $mysql_Innodb_data_reads
399 SET Innodb_data_writes = $mysql_Innodb_data_writes
400 SET Innodb_data_fsyncs = $mysql_Innodb_data_fsyncs
401 END
402 BEGIN mysql_$m.innodb_io_pending_ops $1
403 SET Innodb_data_pending_reads = $mysql_Innodb_data_pending_reads
404 SET Innodb_data_pending_writes = $mysql_Innodb_data_pending_writes
405 SET Innodb_data_pending_fsyncs = $mysql_Innodb_data_pending_fsyncs
406 END
407 BEGIN mysql_$m.innodb_log $1
408 SET Innodb_log_waits = $mysql_Innodb_log_waits
409 SET Innodb_log_write_requests = $mysql_Innodb_log_write_requests
410 SET Innodb_log_writes = $mysql_Innodb_log_writes
411 END
412 BEGIN mysql_$m.innodb_os_log $1
413 SET Innodb_os_log_fsyncs = $mysql_Innodb_os_log_fsyncs
414 SET Innodb_os_log_pending_fsyncs = $mysql_Innodb_os_log_pending_fsyncs
415 SET Innodb_os_log_pending_writes = $mysql_Innodb_os_log_pending_writes
416 END
417 BEGIN mysql_$m.innodb_os_log_io $1
418 SET Innodb_os_log_written = $mysql_Innodb_os_log_written
419 END
420 BEGIN mysql_$m.innodb_cur_row_lock $1
421 SET Innodb_row_lock_current_waits = $mysql_Innodb_row_lock_current_waits
422 END
423 BEGIN mysql_$m.innodb_rows $1
424 SET Innodb_rows_inserted = $mysql_Innodb_rows_inserted
425 SET Innodb_rows_read = $mysql_Innodb_rows_read
426 SET Innodb_rows_updated = $mysql_Innodb_rows_updated
427 SET Innodb_rows_deleted = $mysql_Innodb_rows_deleted
428 END
429 VALUESEOF
430
431                 if [ ! -z "$mysql_Binlog_stmt_cache_disk_use" ]
432                         then
433                         cat <<VALUESEOF
434 BEGIN mysql_$m.binlog_stmt_cache $1
435 SET Binlog_stmt_cache_disk_use = $mysql_Binlog_stmt_cache_disk_use
436 SET Binlog_stmt_cache_use = $mysql_Binlog_stmt_cache_use
437 END
438 VALUESEOF
439                 fi
440
441                 if [ ! -z "$mysql_Connection_errors_accept" ]
442                         then
443                         cat <<VALUESEOF
444 BEGIN mysql_$m.connection_errors $1
445 SET Connection_errors_accept = $mysql_Connection_errors_accept
446 SET Connection_errors_internal = $mysql_Connection_errors_internal
447 SET Connection_errors_max_connections = $mysql_Connection_errors_max_connections
448 SET Connection_errors_peer_addr = $mysql_Connection_errors_peer_addr
449 SET Connection_errors_select = $mysql_Connection_errors_select
450 SET Connection_errors_tcpwrap = $mysql_Connection_errors_tcpwrap
451 END
452 VALUESEOF
453                 fi
454         done
455
456         [ ${#mysql_ids[@]} -eq 0 ] && echo >&2 "$PROGRAM_NAME: mysql: no mysql servers left active." && return 1
457         return 0
458 }
459