minor fixes; mysql now accepts multiple servers
Costa Tsaousis (ktsaou) committed
Nov 19, 2015 at 01:23 UTC
d919264e33308b44b18f04a46568c5cb801b5255
4 files changed
+219
-62
charts.d/README.md
+78
@@ -1,5 +1,83 @@
1
The following charts.d plugins are supported:
2
3
+# mysql
4
+
5
+The plugin will monitor one or more mysql servers
6
+
7
+It will produce the following charts:
8
+
9
+1. **Bandwidth** in kbps
10
+ * in
11
+ * out
12
+
13
+2. **Queries** in queries/sec
14
+ * queries
15
+ * questions
16
+ * slow queries
17
+
18
+3. **Operations** in operations/sec
19
+ * opened tables
20
+ * flush
21
+ * commit
22
+ * delete
23
+ * prepare
24
+ * read first
25
+ * read key
26
+ * read next
27
+ * read prev
28
+ * read random
29
+ * read random next
30
+ * rollback
31
+ * save point
32
+ * update
33
+ * write
34
+
35
+4. **Table Locks** in locks/sec
36
+ * immediate
37
+ * waited
38
+
39
+5. **Select Issues** in issues/sec
40
+ * full join
41
+ * full range join
42
+ * range
43
+ * range check
44
+ * scan
45
+
46
+6. **Sort Issues** in issues/sec
47
+ * merge passes
48
+ * range
49
+ * scan
50
+
51
+### configuration
52
+
53
+You can configure many database servers, like this:
54
+
55
+You can provide, per server, the following:
56
+
57
+1. a name, anything you like, but keep it short
58
+2. the mysql command to connect to the server
59
+3. the mysql command line options to be used for connecting to the server
60
+
61
+Here is an example for 2 servers:
62
+
63
+```sh
64
+mysql_opts[server1]="-h server1.example.com"
65
+mysql_opts[server2]="-h server2.example.com --connect_timeout 2"
66
+```
67
+
68
+The above will use the `mysql` command found in the system path.
69
+You can also provide a custom mysql command per server, like this:
70
+
71
+```sh
72
+mysql_cmds[server2]="/opt/mysql/bin/mysql"
73
+```
74
+
75
+The above sets the mysql command only for server2. server1 will use the system default.
76
+
77
+If no configuration is given, the plugin will attempt to connect to mysql server at localhost.
78
+
79
+---
80
+
81
# squid
82
83
The plugin will monitor a squid server.
charts.d/mysql.chart.sh
+86
-32
@@ -7,43 +7,86 @@
7
# This statement does not require any privilege.
8
# It requires only the ability to connect to the server.
9
10
-mysql_cmd_opts=
10
mysql_update_every=5
11
13
-mysql_get_stats() {
14
- mysql "${mysql_cmd_opts}" -s -e "show global status;"
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() {
18
- require_cmd mysql || return 1
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
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
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
35
- cat <<EOF
36
-CHART mysql.bandwidth '' "mysql Bandwidth" "kilobits / sec" mysql '' area 20001 $mysql_update_every
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
40
-CHART mysql.queries '' "mysql Queries" "queries / sec" mysql '' line 20002 $mysql_update_every
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
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))
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))
@@ -60,22 +103,23 @@ DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mys
103
DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
104
DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
105
63
-CHART mysql.table_locks '' "mysql Tables Locks" "locks / sec" mysql '' line 20004 $mysql_update_every
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
67
-CHART mysql.select_issues '' "mysql Select Issues" "issues / sec" mysql '' line 20005 $mysql_update_every
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
74
-CHART mysql.sort_issues '' "mysql Sort Issues" "issues / sec" mysql '' line 20006 $mysql_update_every
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
@@ -99,24 +143,36 @@ mysql_update() {
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
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:]]+$")"
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
108
-BEGIN mysql.bandwidth $1
166
+BEGIN mysql_$x.bandwidth $1
167
SET Bytes_received = $mysql_Bytes_received
168
SET Bytes_sent = $mysql_Bytes_sent
169
END
112
-
113
-BEGIN mysql.queries $1
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
118
-
119
-BEGIN mysql.operations $1
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
@@ -134,28 +190,26 @@ SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
190
SET Handler_update = $mysql_Handler_update
191
SET Handler_write = $mysql_Handler_write
192
END
137
-
138
-BEGIN mysql.table_locks $1
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
142
-
143
-BEGIN mysql.select_issues $1
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
150
-
151
-BEGIN mysql.sort_issues $1
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
156
-
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
charts.d/nut.chart.sh
+38
-18
@@ -1,4 +1,4 @@
1
-#!/bin/sh
1
+#!/bin/bash
2
3
# a space separated list of UPS names
4
# if empty, the list returned by 'upsc -l' will be used
@@ -7,24 +7,44 @@ nut_ups=
7
# how frequently to collect UPS data
8
nut_update_every=2
9
10
+nut_timeout=2
11
+
12
+declare -A nut_ids=()
13
+
14
+nut_get_all() {
15
+ timeout $nut_timeout upsc -l
16
+}
17
+
18
+nut_get() {
19
+ timeout $nut_timeout upsc "$1"
20
+}
21
+
22
nut_check() {
11
- local x all
23
24
+ # this should return:
25
+ # - 0 to enable the chart
26
+ # - 1 to disable the chart
27
+
28
+ local x
29
+
30
+ require_cmd timeout || return 1
31
require_cmd upsc || return 1
32
require_cmd awk || return 1
33
16
- all="$nut_ups"
17
- [ -z "$all" ] && all="$(upsc -l)"
34
+ [ -z "$nut_ups" ] && nut_ups="$( nut_get_all )"
35
19
- nut_ups=
20
- for x in $all
36
+ for x in $nut_ups
37
do
22
- upsc "$x" >/dev/null
23
- [ $? -eq 0 ] && nut_ups="$nut_ups $x" && continue
38
+ nut_get "$x" >/dev/null
39
+ if [ $? -eq 0 ]
40
+ then
41
+ nut_ids[$x]="$( fixid "$x" )"
42
+ continue
43
+ fi
44
echo >&2 "nut: ERROR: Cannot get information for NUT UPS '$x'."
45
done
46
27
- if [ -z "$nut_ups" ]
47
+ if [ ${#nut_ids[@]} -eq 0 ]
48
then
49
echo >&2 "nut: Please set nut_ups='ups_name' in $confd/nut.conf"
50
return 1
@@ -37,7 +57,7 @@ nut_create() {
57
# create the charts
58
local x
59
40
- for x in $nut_ups
60
+ for x in "${nut_ids[@]}"
61
do
62
cat <<EOF
63
CHART nut_$x.charge '' "UPS Charge" "percentage" $x nut area 21001 $nut_update_every
@@ -84,11 +104,11 @@ nut_update() {
104
# for each dimension
105
# remember: KEEP IT SIMPLE AND SHORT
106
87
- local x
88
-
89
- for x in $nut_ups
107
+ local i x
108
+ for i in "${!nut_ids[@]}"
109
do
91
- upsc "$x" | awk "
110
+ x="${nut_ids[$i]}"
111
+ nut_get "$i" | awk "
112
BEGIN {
113
battery_charge = 0;
114
battery_voltage = 0;
@@ -157,10 +177,10 @@ END {
177
print \"BEGIN nut_$x.temp $1\";
178
print \"SET temp = \" temp;
179
print \"END\"
160
-}
161
-"
180
+}"
181
+ [ $? -ne 0 ] && unset nut_ids[$i] && echo >&2 "nut: failed to get values for '$i', disabling it."
182
done
163
-
183
+
184
+ [ ${#nut_ids[@]} -eq 0 ] && echo >&2 "nut: no UPSes left active." && return 1
185
return 0
186
}
166
-
plugins.d/charts.d.plugin
+17
-12
@@ -79,6 +79,11 @@ charts_undescore="_"
79
# above their update_every.
80
time_divisor=50
81
82
+# number of seconds to run without restart
83
+# after this time, charts.d.plugin will exit
84
+# netdata will restart it
85
+restart_timeout=1800
86
+
87
# -----------------------------------------------------------------------------
88
# parse parameters
89
@@ -193,6 +198,17 @@ current_time_ms() {
198
. "$pluginsd/loopsleepms.sh.inc"
199
200
201
+# -----------------------------------------------------------------------------
202
+# library functions
203
+
204
+fixid() {
205
+ echo "$*" |\
206
+ tr -c "[A-Z][a-z][0-9]" "_" |\
207
+ sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
208
+ tr "[A-Z]" "[a-z]"
209
+}
210
+
211
+
212
# -----------------------------------------------------------------------------
213
# charts check functions
214
@@ -363,17 +379,6 @@ fi
379
380
cd "$TMP_DIR" || exit 1
381
366
-# -----------------------------------------------------------------------------
367
-# library functions
368
-
369
-fixid() {
370
- echo "$*" |\
371
- tr -c "[A-Z][a-z][0-9]" "_" |\
372
- sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
373
- tr "[A-Z]" "[a-z]"
374
-}
375
-
376
-
382
# -----------------------------------------------------------------------------
383
# create charts
384
@@ -405,7 +410,7 @@ fi
410
411
declare -A charts_last_update=() charts_min_dt=()
412
global_update() {
408
- local exit_after=$((3600 / update_every)) \
413
+ local exit_after=$((restart_timeout / update_every)) \
414
c=0 dt \
415
chart now_charts=() next_charts=($run_charts)
416