]> arthur.barton.de Git - netdata.git/blob - charts.d/mysql.chart.sh
Merge pull request #31 from alonbl/build
[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_cmd_opts=
11 mysql_update_every=5
12
13 mysql_get_stats() {
14         mysql "${mysql_cmd_opts}" -s -e "show global status;"
15 }
16
17 mysql_check() {
18         require_cmd mysql || return 1
19         require_cmd egrep || return 1
20         require_cmd sed   || return 1
21
22         # check once if the url works
23         local x="$(mysql_get_stats | grep "^Connections[[:space:]]")"
24         if [ ! $? -eq 0 -o -z "$x" ]
25         then
26                 echo >&2 "mysql: cannot get global status. Please set mysql_cmd_opts='options' whatever needed to get connected to mysql server, in $confd/mysql.conf"
27                 return 1
28         fi
29
30         return 0
31 }
32
33 mysql_create() {
34         # create the charts
35         cat <<EOF
36 CHART mysql.bandwidth '' "mysql Bandwidth" "kilobits / sec" mysql '' area 20001 $mysql_update_every
37 DIMENSION Bytes_received in incremental 8 $((1024 * mysql_update_every))
38 DIMENSION Bytes_sent out incremental -8 $((1024 * mysql_update_every))
39
40 CHART mysql.queries '' "mysql Queries" "queries / sec" mysql '' line 20002 $mysql_update_every
41 DIMENSION Queries queries incremental 1 $((1 * mysql_update_every))
42 DIMENSION Questions questions incremental 1 $((1 * mysql_update_every))
43 DIMENSION Slow_queries slow_queries incremental -1 $((1 * mysql_update_every))
44
45 CHART mysql.operations '' "mysql Operations" "operations / sec" mysql '' line 20003 $mysql_update_every
46 DIMENSION Opened_tables opened_table incremental 1 $((1 * mysql_update_every))
47 DIMENSION Flush_commands flush incremental 1 $((1 * mysql_update_every))
48 DIMENSION Handler_commit commit incremental 1 $((1 * mysql_update_every))
49 DIMENSION Handler_delete delete incremental 1 $((1 * mysql_update_every))
50 DIMENSION Handler_prepare prepare incremental 1 $((1 * mysql_update_every))
51 DIMENSION Handler_read_first read_first incremental 1 $((1 * mysql_update_every))
52 DIMENSION Handler_read_key read_key incremental 1 $((1 * mysql_update_every))
53 DIMENSION Handler_read_next read_next incremental 1 $((1 * mysql_update_every))
54 DIMENSION Handler_read_prev read_prev incremental 1 $((1 * mysql_update_every))
55 DIMENSION Handler_read_rnd read_rnd incremental 1 $((1 * mysql_update_every))
56 DIMENSION Handler_read_rnd_next read_rnd_next incremental 1 $((1 * mysql_update_every))
57 DIMENSION Handler_rollback rollback incremental 1 $((1 * mysql_update_every))
58 DIMENSION Handler_savepoint savepoint incremental 1 $((1 * mysql_update_every))
59 DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mysql_update_every))
60 DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
61 DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
62
63 CHART mysql.table_locks '' "mysql Tables Locks" "locks / sec" mysql '' line 20004 $mysql_update_every
64 DIMENSION Table_locks_immediate immediate incremental 1 $((1 * mysql_update_every))
65 DIMENSION Table_locks_waited waited incremental -1 $((1 * mysql_update_every))
66
67 CHART mysql.select_issues '' "mysql Select Issues" "issues / sec" mysql '' line 20005 $mysql_update_every
68 DIMENSION Select_full_join full_join incremental 1 $((1 * mysql_update_every))
69 DIMENSION Select_full_range_join full_range_join incremental 1 $((1 * mysql_update_every))
70 DIMENSION Select_range range incremental 1 $((1 * mysql_update_every))
71 DIMENSION Select_range_check range_check incremental 1 $((1 * mysql_update_every))
72 DIMENSION Select_scan scan incremental 1 $((1 * mysql_update_every))
73
74 CHART mysql.sort_issues '' "mysql Sort Issues" "issues / sec" mysql '' line 20006 $mysql_update_every
75 DIMENSION Sort_merge_passes merge_passes incremental 1 $((1 * mysql_update_every))
76 DIMENSION Sort_range range incremental 1 $((1 * mysql_update_every))
77 DIMENSION Sort_scan scan incremental 1 $((1 * mysql_update_every))
78 EOF
79         return 0
80 }
81
82
83 mysql_update() {
84         # the first argument to this function is the microseconds since last update
85         # pass this parameter to the BEGIN statement (see bellow).
86
87         # do all the work to collect / calculate the values
88         # for each dimension
89         # remember: KEEP IT SIMPLE AND SHORT
90
91         # 1. get the counters page from mysql
92         # 2. sed to remove spaces; replace . with _; remove spaces around =; prepend each line with: local mysql_
93         # 3. egrep lines starting with:
94         #    local mysql_client_http_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
95         #    local mysql_server_all_ then one or more of these a-z 0-9 _ then = and one of more of 0-9
96         # 4. then execute this as a script with the eval
97         #
98         # be very carefull with eval:
99         # prepare the script and always grep at the end the lines that are usefull, so that
100         # even if something goes wrong, no other code can be executed
101
102         eval "$(mysql_get_stats |\
103                  sed -e "s/[[:space:]]\+/ /g" -e "s/\./_/g" -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/local mysql_\1=\2/g" |\
104                  egrep "^local mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
105
106         # write the result of the work.
107         cat <<VALUESEOF
108 BEGIN mysql.bandwidth $1
109 SET Bytes_received = $mysql_Bytes_received
110 SET Bytes_sent = $mysql_Bytes_sent
111 END
112
113 BEGIN mysql.queries $1
114 SET Queries = $mysql_Queries
115 SET Questions = $mysql_Questions
116 SET Slow_queries = $mysql_Slow_queries
117 END
118
119 BEGIN mysql.operations $1
120 SET Opened_tables = $mysql_Opened_tables
121 SET Flush_commands = $mysql_Flush_commands
122 SET Handler_commit = $mysql_Handler_commit
123 SET Handler_delete = $mysql_Handler_delete
124 SET Handler_prepare = $mysql_Handler_prepare
125 SET Handler_read_first = $mysql_Handler_read_first
126 SET Handler_read_key = $mysql_Handler_read_key
127 SET Handler_read_next = $mysql_Handler_read_next
128 SET Handler_read_prev = $mysql_Handler_read_prev
129 SET Handler_read_rnd = $mysql_Handler_read_rnd
130 SET Handler_read_rnd_next = $mysql_Handler_read_rnd_next
131 SET Handler_rollback = $mysql_Handler_rollback
132 SET Handler_savepoint = $mysql_Handler_savepoint
133 SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
134 SET Handler_update = $mysql_Handler_update
135 SET Handler_write = $mysql_Handler_write
136 END
137
138 BEGIN mysql.table_locks $1
139 SET Table_locks_immediate = $mysql_Table_locks_immediate
140 SET Table_locks_waited = $mysql_Table_locks_waited
141 END
142
143 BEGIN mysql.select_issues $1
144 SET Select_full_join = $mysql_Select_full_join
145 SET Select_full_range_join = $mysql_Select_full_range_join
146 SET Select_range = $mysql_Select_range
147 SET Select_range_check = $mysql_Select_range_check
148 SET Select_scan = $mysql_Select_scan
149 END
150
151 BEGIN mysql.sort_issues $1
152 SET Sort_merge_passes = $mysql_Sort_merge_passes
153 SET Sort_range = $mysql_Sort_range
154 SET Sort_scan = $mysql_Sort_scan
155 END
156
157 VALUESEOF
158
159         return 0
160 }
161