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