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