]> arthur.barton.de Git - netdata.git/blob - charts.d/mysql.chart.sh
more mysql charts
[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_get() {
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_check() {
25         # this should return:
26         #  - 0 to enable the chart
27         #  - 1 to disable the chart
28
29         local x m mysql_cmd
30
31         [ -z "${mysql_cmd}" ] && mysql_cmd="$(which mysql)"
32
33         if [ ${#mysql_opts[@]} -eq 0 ]
34                 then
35                 mysql_cmds[local]="$mysql_cmd"
36                 mysql_opts[local]=
37         fi
38
39         # check once if the url works
40         for m in "${!mysql_opts[@]}"
41         do
42                 [ -z "${mysql_cmds[$m]}" ] && mysql_cmds[$m]="$mysql_cmd"
43                 if [ -z "${mysql_cmds[$m]}" ]
44                         then
45                         echo >&2 "$PROGRAM_NAME: mysql: cannot get mysql command for '$m'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
46                 fi
47
48                 x="$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} | grep "^Connections[[:space:]]")"
49                 if [ ! $? -eq 0 -o -z "$x" ]
50                 then
51                         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"
52                         unset mysql_cmds[$m]
53                         unset mysql_opts[$m]
54                         unset mysql_ids[$m]
55                         continue
56                 fi
57
58                 mysql_ids[$m]="$( fixid "$m" )"
59         done
60
61         if [ ${#mysql_opts[@]} -eq 0 ]
62                 then
63                 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"
64                 return 1
65         fi
66
67         return 0
68 }
69
70 mysql_create() {
71         local m
72
73         # create the charts
74         for m in "${mysql_ids[@]}"
75         do
76                 cat <<EOF
77 CHART mysql_$m.bandwidth '' "mysql Bandwidth" "kilobits / sec" mysql_$m mysql area 20001 $mysql_update_every
78 DIMENSION Bytes_received in incremental 8 $((1024 * mysql_update_every))
79 DIMENSION Bytes_sent out incremental -8 $((1024 * mysql_update_every))
80
81 CHART mysql_$m.queries '' "mysql Queries" "queries / sec" mysql_$m mysql line 20002 $mysql_update_every
82 DIMENSION Queries queries incremental 1 $((1 * mysql_update_every))
83 DIMENSION Questions questions incremental 1 $((1 * mysql_update_every))
84 DIMENSION Slow_queries slow_queries incremental -1 $((1 * mysql_update_every))
85
86 CHART mysql_$m.handlers '' "mysql Handlers" "handlers / sec" mysql_$m mysql line 20003 $mysql_update_every
87 DIMENSION Handler_commit commit incremental 1 $((1 * mysql_update_every))
88 DIMENSION Handler_delete delete incremental 1 $((1 * mysql_update_every))
89 DIMENSION Handler_prepare prepare incremental 1 $((1 * mysql_update_every))
90 DIMENSION Handler_read_first read_first incremental 1 $((1 * mysql_update_every))
91 DIMENSION Handler_read_key read_key incremental 1 $((1 * mysql_update_every))
92 DIMENSION Handler_read_next read_next incremental 1 $((1 * mysql_update_every))
93 DIMENSION Handler_read_prev read_prev incremental 1 $((1 * mysql_update_every))
94 DIMENSION Handler_read_rnd read_rnd incremental 1 $((1 * mysql_update_every))
95 DIMENSION Handler_read_rnd_next read_rnd_next incremental 1 $((1 * mysql_update_every))
96 DIMENSION Handler_rollback rollback incremental 1 $((1 * mysql_update_every))
97 DIMENSION Handler_savepoint savepoint incremental 1 $((1 * mysql_update_every))
98 DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mysql_update_every))
99 DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
100 DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
101
102 CHART mysql_$m.table_locks '' "mysql Tables Locks" "locks / sec" mysql_$m mysql line 20004 $mysql_update_every
103 DIMENSION Table_locks_immediate immediate incremental 1 $((1 * mysql_update_every))
104 DIMENSION Table_locks_waited waited incremental -1 $((1 * mysql_update_every))
105
106 CHART mysql_$m.join_issues '' "mysql Select Join Issues" "joins / sec" mysql_$m mysql line 20005 $mysql_update_every
107 DIMENSION Select_full_join full_join incremental 1 $((1 * mysql_update_every))
108 DIMENSION Select_full_range_join full_range_join incremental 1 $((1 * mysql_update_every))
109 DIMENSION Select_range range incremental 1 $((1 * mysql_update_every))
110 DIMENSION Select_range_check range_check incremental 1 $((1 * mysql_update_every))
111 DIMENSION Select_scan scan incremental 1 $((1 * mysql_update_every))
112
113 CHART mysql_$m.sort_issues '' "mysql Sort Issues" "issues / sec" mysql_$m mysql line 20006 $mysql_update_every
114 DIMENSION Sort_merge_passes merge_passes incremental 1 $((1 * mysql_update_every))
115 DIMENSION Sort_range range incremental 1 $((1 * mysql_update_every))
116 DIMENSION Sort_scan scan incremental 1 $((1 * mysql_update_every))
117
118 CHART mysql_$m.tmp '' "mysql Tmp Operations" "counter" mysql_$m mysql line 20007 $mysql_update_every
119 DIMENSION Created_tmp_disk_tables disk_tables incremental 1 $((1 * mysql_update_every))
120 DIMENSION Created_tmp_files files incremental 1 $((1 * mysql_update_every))
121 DIMENSION Created_tmp_tables tables incremental 1 $((1 * mysql_update_every))
122
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
131 CHART mysql_$m.connections '' "mysql Connections" "connections/s" mysql_$m mysql line 20009 $mysql_update_every
132 DIMENSION Connections all incremental 1 $((1 * mysql_update_every))
133 DIMENSION Aborted_connects aborded incremental 1 $((1 * mysql_update_every))
134
135 CHART mysql_$m.binlog_cache '' "mysql Binlog Cache" "transactions/s" mysql_$m mysql line 20010 $mysql_update_every
136 DIMENSION Binlog_cache_disk_use disk incremental 1 $((1 * mysql_update_every))
137 DIMENSION Binlog_cache_use all incremental 1 $((1 * mysql_update_every))
138
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
143 CHART mysql_$m.threads '' "mysql Threads" "threads" mysql_$m mysql line 20012 $mysql_update_every
144 DIMENSION Threads_connected connected absolute 1 $((1 * mysql_update_every))
145 DIMENSION Threads_created created incremental 1 $((1 * mysql_update_every))
146 DIMENSION Threads_cached cached absolute -1 $((1 * mysql_update_every))
147 DIMENSION Threads_running running absolute 1 $((1 * mysql_update_every))
148
149 CHART mysql_$m.thread_cache_misses '' "mysql Threads Cache Misses" "misses" mysql_$m mysql area 20013 $mysql_update_every
150 DIMENSION misses misses absolute 1 $((100 * mysql_update_every))
151
152 CHART mysql_$m.innodb_io '' "mysql InnoDB I/O Bandwidth" "kbps" mysql_$m mysql area 20014 $mysql_update_every
153 DIMENSION Innodb_data_read read incremental 8 $((1000 * mysql_update_every))
154 DIMENSION Innodb_data_written write incremental -8 $((1000 * mysql_update_every))
155
156 CHART mysql_$m.innodb_io_ops '' "mysql InnoDB I/O Operations" "operations/s" mysql_$m mysql line 20015 $mysql_update_every
157 DIMENSION Innodb_data_reads reads incremental 1 $((1 * mysql_update_every))
158 DIMENSION Innodb_data_writes writes incremental -1 $((1 * mysql_update_every))
159
160 CHART mysql_$m.innodb_log '' "mysql InnoDB Log Operations" "operations/s" mysql_$m mysql line 20016 $mysql_update_every
161 DIMENSION Innodb_log_waits waits incremental 1 $((1 * mysql_update_every))
162 DIMENSION Innodb_log_write_requests write_requests incremental -1 $((1 * mysql_update_every))
163 DIMENSION Innodb_log_writes writes incremental -1 $((1 * mysql_update_every))
164
165 CHART mysql_$m.innodb_os_log '' "mysql InnoDB OS Log Operations" "operations/s" mysql_$m mysql line 20017 $mysql_update_every
166 DIMENSION Innodb_os_log_fsyncs fsyncs incremental 1 $((1 * mysql_update_every))
167 DIMENSION Innodb_os_log_pending_fsyncs pending_fsyncs incremental 1 $((1 * mysql_update_every))
168 DIMENSION Innodb_os_log_pending_writes pending_writes incremental -1 $((1 * mysql_update_every))
169
170 CHART mysql_$m.innodb_os_log_io '' "mysql InnoDB OS Log Bandwidth" "kbps" mysql_$m mysql area 20018 $mysql_update_every
171 DIMENSION Innodb_os_log_written write incremental -8 $((1000 * mysql_update_every))
172
173 CHART mysql_$m.innodb_cur_row_lock '' "mysql InnoDB Current Row Locks" "operations" mysql_$m mysql area 20019 $mysql_update_every
174 DIMENSION Innodb_row_lock_current_waits current_waits absolute 1 $((1 * mysql_update_every))
175
176 CHART mysql_$m.innodb_rows '' "mysql InnoDB Row Operations" "operations/s" mysql_$m mysql area 20020 $mysql_update_every
177 DIMENSION Innodb_rows_read read incremental 1 $((1 * mysql_update_every))
178 DIMENSION Innodb_rows_deleted deleted incremental -1 $((1 * mysql_update_every))
179 DIMENSION Innodb_rows_inserted inserted incremental 1 $((1 * mysql_update_every))
180 DIMENSION Innodb_rows_updated updated incremental -1 $((1 * mysql_update_every))
181
182 EOF
183         done
184         return 0
185 }
186
187
188 mysql_update() {
189         # the first argument to this function is the microseconds since last update
190         # pass this parameter to the BEGIN statement (see bellow).
191
192         # do all the work to collect / calculate the values
193         # for each dimension
194         # remember: KEEP IT SIMPLE AND SHORT
195
196         # 1. get the counters page from mysql
197         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local mysql_
198         # 3. egrep lines starting with:
199         #    local mysql_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
200         #    local mysql_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
201         # 4. then execute this as a script with the eval
202         #
203         # be very carefull with eval:
204         # prepare the script and always grep at the end the lines that are usefull, so that
205         # even if something goes wrong, no other code can be executed
206
207         local m x
208         for m in "${!mysql_ids[@]}"
209         do
210                 x="${mysql_ids[$m]}"
211
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 ]
285                         then
286                         unset mysql_ids[$m]
287                         unset mysql_opts[$m]
288                         unset mysql_cmds[$m]
289                         echo >&2 "$PROGRAM_NAME: mysql: failed to get values for '$m', disabling it."
290                         continue
291                 fi
292
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
297                 # write the result of the work.
298                 cat <<VALUESEOF
299 BEGIN mysql_$x.bandwidth $1
300 SET Bytes_received = $mysql_Bytes_received
301 SET Bytes_sent = $mysql_Bytes_sent
302 END
303 BEGIN mysql_$x.queries $1
304 SET Queries = $mysql_Queries
305 SET Questions = $mysql_Questions
306 SET Slow_queries = $mysql_Slow_queries
307 END
308 BEGIN mysql_$x.handlers $1
309 SET Handler_commit = $mysql_Handler_commit
310 SET Handler_delete = $mysql_Handler_delete
311 SET Handler_prepare = $mysql_Handler_prepare
312 SET Handler_read_first = $mysql_Handler_read_first
313 SET Handler_read_key = $mysql_Handler_read_key
314 SET Handler_read_next = $mysql_Handler_read_next
315 SET Handler_read_prev = $mysql_Handler_read_prev
316 SET Handler_read_rnd = $mysql_Handler_read_rnd
317 SET Handler_read_rnd_next = $mysql_Handler_read_rnd_next
318 SET Handler_rollback = $mysql_Handler_rollback
319 SET Handler_savepoint = $mysql_Handler_savepoint
320 SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
321 SET Handler_update = $mysql_Handler_update
322 SET Handler_write = $mysql_Handler_write
323 END
324 BEGIN mysql_$x.table_locks $1
325 SET Table_locks_immediate = $mysql_Table_locks_immediate
326 SET Table_locks_waited = $mysql_Table_locks_waited
327 END
328 BEGIN mysql_$x.join_issues $1
329 SET Select_full_join = $mysql_Select_full_join
330 SET Select_full_range_join = $mysql_Select_full_range_join
331 SET Select_range = $mysql_Select_range
332 SET Select_range_check = $mysql_Select_range_check
333 SET Select_scan = $mysql_Select_scan
334 END
335 BEGIN mysql_$x.sort_issues $1
336 SET Sort_merge_passes = $mysql_Sort_merge_passes
337 SET Sort_range = $mysql_Sort_range
338 SET Sort_scan = $mysql_Sort_scan
339 END
340 BEGIN mysql_$m.tmp $1
341 SET Created_tmp_disk_tables = $mysql_Created_tmp_disk_tables
342 SET Created_tmp_files = $mysql_Created_tmp_files
343 SET Created_tmp_tables = $mysql_Created_tmp_tables
344 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
353 BEGIN mysql_$m.connections $1
354 SET Connections = $mysql_Connections
355 SET Aborted_connects = $mysql_Aborted_connects
356 END
357 BEGIN mysql_$m.binlog_cache $1
358 SET Binlog_cache_disk_use = $mysql_Binlog_cache_disk_use
359 SET Binlog_cache_use = $mysql_Binlog_cache_use
360 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
365 BEGIN mysql_$m.threads $1
366 SET Threads_connected = $mysql_Threads_connected
367 SET Threads_created = $mysql_Threads_created
368 SET Threads_cached = $mysql_Threads_cached
369 SET Threads_running = $mysql_Threads_running
370 END
371 BEGIN mysql_$m.thread_cache_misses $1
372 SET misses = $misses
373 END
374 BEGIN mysql_$m.innodb_io $1
375 SET Innodb_data_read = $mysql_Innodb_data_read
376 SET Innodb_data_written = $mysql_Innodb_data_written
377 END
378 BEGIN mysql_$m.innodb_io_ops $1
379 SET Innodb_data_reads = $mysql_Innodb_data_reads
380 SET Innodb_data_writes = $mysql_Innodb_data_writes
381 END
382 BEGIN mysql_$m.innodb_log $1
383 SET Innodb_log_waits = $mysql_Innodb_log_waits
384 SET Innodb_log_write_requests = $mysql_Innodb_log_write_requests
385 SET Innodb_log_writes = $mysql_Innodb_log_writes
386 END
387 BEGIN mysql_$m.innodb_os_log $1
388 SET Innodb_os_log_fsyncs = $mysql_Innodb_os_log_fsyncs
389 SET Innodb_os_log_pending_fsyncs = $mysql_Innodb_os_log_pending_fsyncs
390 SET Innodb_os_log_pending_writes = $mysql_Innodb_os_log_pending_writes
391 END
392 BEGIN mysql_$m.innodb_os_log_io $1
393 SET Innodb_os_log_written = $mysql_Innodb_os_log_written
394 END
395 BEGIN mysql_$m.innodb_cur_row_lock $1
396 SET Innodb_row_lock_current_waits = $mysql_Innodb_row_lock_current_waits
397 END
398 BEGIN mysql_$m.innodb_rows $1
399 SET Innodb_rows_inserted = $mysql_Innodb_rows_inserted
400 SET Innodb_rows_read = $mysql_Innodb_rows_read
401 SET Innodb_rows_updated = $mysql_Innodb_rows_updated
402 SET Innodb_rows_deleted = $mysql_Innodb_rows_deleted
403 END
404 VALUESEOF
405         done
406
407         [ ${#mysql_ids[@]} -eq 0 ] && echo >&2 "$PROGRAM_NAME: mysql: no mysql servers left active." && return 1
408         return 0
409 }
410