]> arthur.barton.de Git - netdata.git/blob - charts.d/mysql.chart.sh
normalized all charts.d plugins
[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.operations '' "mysql Operations" "operations / sec" mysql_$m mysql line 20003 $mysql_update_every
87 DIMENSION Opened_tables opened_tables incremental 1 $((1 * mysql_update_every))
88 DIMENSION Flush_commands flush incremental 1 $((1 * mysql_update_every))
89 DIMENSION Handler_commit commit incremental 1 $((1 * mysql_update_every))
90 DIMENSION Handler_delete delete incremental 1 $((1 * mysql_update_every))
91 DIMENSION Handler_prepare prepare incremental 1 $((1 * mysql_update_every))
92 DIMENSION Handler_read_first read_first incremental 1 $((1 * mysql_update_every))
93 DIMENSION Handler_read_key read_key incremental 1 $((1 * mysql_update_every))
94 DIMENSION Handler_read_next read_next incremental 1 $((1 * mysql_update_every))
95 DIMENSION Handler_read_prev read_prev incremental 1 $((1 * mysql_update_every))
96 DIMENSION Handler_read_rnd read_rnd incremental 1 $((1 * mysql_update_every))
97 DIMENSION Handler_read_rnd_next read_rnd_next incremental 1 $((1 * mysql_update_every))
98 DIMENSION Handler_rollback rollback incremental 1 $((1 * mysql_update_every))
99 DIMENSION Handler_savepoint savepoint incremental 1 $((1 * mysql_update_every))
100 DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mysql_update_every))
101 DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
102 DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
103
104 CHART mysql_$m.table_locks '' "mysql Tables Locks" "locks / sec" mysql_$m mysql line 20004 $mysql_update_every
105 DIMENSION Table_locks_immediate immediate incremental 1 $((1 * mysql_update_every))
106 DIMENSION Table_locks_waited waited incremental -1 $((1 * mysql_update_every))
107
108 CHART mysql_$m.select_issues '' "mysql Select Issues" "issues / sec" mysql_$m mysql line 20005 $mysql_update_every
109 DIMENSION Select_full_join full_join incremental 1 $((1 * mysql_update_every))
110 DIMENSION Select_full_range_join full_range_join incremental 1 $((1 * mysql_update_every))
111 DIMENSION Select_range range incremental 1 $((1 * mysql_update_every))
112 DIMENSION Select_range_check range_check incremental 1 $((1 * mysql_update_every))
113 DIMENSION Select_scan scan incremental 1 $((1 * mysql_update_every))
114
115 CHART mysql_$m.sort_issues '' "mysql Sort Issues" "issues / sec" mysql_$m mysql line 20006 $mysql_update_every
116 DIMENSION Sort_merge_passes merge_passes incremental 1 $((1 * mysql_update_every))
117 DIMENSION Sort_range range incremental 1 $((1 * mysql_update_every))
118 DIMENSION Sort_scan scan incremental 1 $((1 * mysql_update_every))
119 EOF
120         done
121         return 0
122 }
123
124
125 mysql_update() {
126         # the first argument to this function is the microseconds since last update
127         # pass this parameter to the BEGIN statement (see bellow).
128
129         # do all the work to collect / calculate the values
130         # for each dimension
131         # remember: KEEP IT SIMPLE AND SHORT
132
133         # 1. get the counters page from mysql
134         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local mysql_
135         # 3. egrep lines starting with:
136         #    local mysql_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
137         #    local mysql_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
138         # 4. then execute this as a script with the eval
139         #
140         # be very carefull with eval:
141         # prepare the script and always grep at the end the lines that are usefull, so that
142         # even if something goes wrong, no other code can be executed
143
144         local m x
145         for m in "${!mysql_ids[@]}"
146         do
147                 x="${mysql_ids[$m]}"
148                 mysql_plugin_command_failure=0
149                 eval "$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} |\
150                         sed -e "s/[[:space:]]\+/ /g" -e "s/\./_/g" -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/local mysql_\1=\2/g" |\
151                         egrep "^local mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
152
153                 if [ $mysql_plugin_command_failure -ne 0 ]
154                         then
155                         unset mysql_ids[$m]
156                         unset mysql_opts[$m]
157                         unset mysql_cmds[$m]
158                         echo >&2 "$PROGRAM_NAME: mysql: failed to get values for '$m', disabling it."
159                         continue
160                 fi
161
162         # write the result of the work.
163         cat <<VALUESEOF
164 BEGIN mysql_$x.bandwidth $1
165 SET Bytes_received = $mysql_Bytes_received
166 SET Bytes_sent = $mysql_Bytes_sent
167 END
168 BEGIN mysql_$x.queries $1
169 SET Queries = $mysql_Queries
170 SET Questions = $mysql_Questions
171 SET Slow_queries = $mysql_Slow_queries
172 END
173 BEGIN mysql_$x.operations $1
174 SET Opened_tables = $mysql_Opened_tables
175 SET Flush_commands = $mysql_Flush_commands
176 SET Handler_commit = $mysql_Handler_commit
177 SET Handler_delete = $mysql_Handler_delete
178 SET Handler_prepare = $mysql_Handler_prepare
179 SET Handler_read_first = $mysql_Handler_read_first
180 SET Handler_read_key = $mysql_Handler_read_key
181 SET Handler_read_next = $mysql_Handler_read_next
182 SET Handler_read_prev = $mysql_Handler_read_prev
183 SET Handler_read_rnd = $mysql_Handler_read_rnd
184 SET Handler_read_rnd_next = $mysql_Handler_read_rnd_next
185 SET Handler_rollback = $mysql_Handler_rollback
186 SET Handler_savepoint = $mysql_Handler_savepoint
187 SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
188 SET Handler_update = $mysql_Handler_update
189 SET Handler_write = $mysql_Handler_write
190 END
191 BEGIN mysql_$x.table_locks $1
192 SET Table_locks_immediate = $mysql_Table_locks_immediate
193 SET Table_locks_waited = $mysql_Table_locks_waited
194 END
195 BEGIN mysql_$x.select_issues $1
196 SET Select_full_join = $mysql_Select_full_join
197 SET Select_full_range_join = $mysql_Select_full_range_join
198 SET Select_range = $mysql_Select_range
199 SET Select_range_check = $mysql_Select_range_check
200 SET Select_scan = $mysql_Select_scan
201 END
202 BEGIN mysql_$x.sort_issues $1
203 SET Sort_merge_passes = $mysql_Sort_merge_passes
204 SET Sort_range = $mysql_Sort_range
205 SET Sort_scan = $mysql_Sort_scan
206 END
207 VALUESEOF
208         done
209
210         [ ${#mysql_ids[@]} -eq 0 ] && echo >&2 "$PROGRAM_NAME: mysql: no mysql servers left active." && return 1
211         return 0
212 }
213