summaryrefslogtreecommitdiffstats
path: root/charts.d/mysql.chart.sh
diff options
context:
space:
mode:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2015-11-19 01:23:42 +0200
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2015-11-19 01:23:42 +0200
commitd919264e33308b44b18f04a46568c5cb801b5255 (patch)
treeb2a944d19854c517c77a8303d6c3406a1dae8477 /charts.d/mysql.chart.sh
parent6ca901daefa6c7d63fe1a60f88d3db46cf14e117 (diff)
minor fixes; mysql now accepts multiple servers
Diffstat (limited to 'charts.d/mysql.chart.sh')
-rwxr-xr-xcharts.d/mysql.chart.sh118
1 files changed, 86 insertions, 32 deletions
diff --git a/charts.d/mysql.chart.sh b/charts.d/mysql.chart.sh
index 555c9b54ac..1580aa9eee 100755
--- a/charts.d/mysql.chart.sh
+++ b/charts.d/mysql.chart.sh
@@ -7,43 +7,86 @@
# This statement does not require any privilege.
# It requires only the ability to connect to the server.
-mysql_cmd_opts=
mysql_update_every=5
-mysql_get_stats() {
- mysql "${mysql_cmd_opts}" -s -e "show global status;"
+declare -A mysql_cmds=() mysql_opts=() mysql_ids=()
+
+mysql_get() {
+ local ret
+
+ "${@}" -s -e "show global status;"
+ ret=$?
+
+ [ $ret -ne 0 ] && echo "plugin_command_failure $ret"
+ return $ret
}
mysql_check() {
- require_cmd mysql || return 1
+ # this should return:
+ # - 0 to enable the chart
+ # - 1 to disable the chart
+
+ local x m mysql_cmd
+
require_cmd egrep || return 1
require_cmd sed || return 1
+ [ -z "${mysql_cmd}" ] && mysql_cmd="$(which mysql)"
+
+ if [ ${#mysql_opts[@]} -eq 0 ]
+ then
+ mysql_cmds[localhost]="$mysql_cmd"
+ mysql_opts[localhost]=
+ fi
+
# check once if the url works
- local x="$(mysql_get_stats | grep "^Connections[[:space:]]")"
- if [ ! $? -eq 0 -o -z "$x" ]
- then
- 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"
- return 1
+ for m in "${!mysql_opts[@]}"
+ do
+ [ -z "${mysql_cmds[$m]}" ] && mysql_cmds[$m]="$mysql_cmd"
+ if [ -z "${mysql_cmds[$m]}" ]
+ then
+ echo >&2 "mysql: cannot get mysql command for '$m'. Please set mysql_cmds[$m]='/path/to/mysql', in $confd/mysql.conf"
+ fi
+
+ x="$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} | grep "^Connections[[:space:]]")"
+ if [ ! $? -eq 0 -o -z "$x" ]
+ then
+ 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"
+ unset mysql_cmds[$m]
+ unset mysql_opts[$m]
+ unset mysql_ids[$m]
+ continue
+ fi
+
+ mysql_ids[$m]="$( fixid "$m" )"
+ done
+
+ if [ ${#mysql_opts[@]} -eq 0 ]
+ then
+ 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"
fi
return 0
}
mysql_create() {
+ local m
+
# create the charts
- cat <<EOF
-CHART mysql.bandwidth '' "mysql Bandwidth" "kilobits / sec" mysql '' area 20001 $mysql_update_every
+ for m in "${mysql_ids[@]}"
+ do
+ cat <<EOF
+CHART mysql_$m.bandwidth '' "mysql Bandwidth" "kilobits / sec" $m mysql area 20001 $mysql_update_every
DIMENSION Bytes_received in incremental 8 $((1024 * mysql_update_every))
DIMENSION Bytes_sent out incremental -8 $((1024 * mysql_update_every))
-CHART mysql.queries '' "mysql Queries" "queries / sec" mysql '' line 20002 $mysql_update_every
+CHART mysql_$m.queries '' "mysql Queries" "queries / sec" $m mysql line 20002 $mysql_update_every
DIMENSION Queries queries incremental 1 $((1 * mysql_update_every))
DIMENSION Questions questions incremental 1 $((1 * mysql_update_every))
DIMENSION Slow_queries slow_queries incremental -1 $((1 * mysql_update_every))
-CHART mysql.operations '' "mysql Operations" "operations / sec" mysql '' line 20003 $mysql_update_every
-DIMENSION Opened_tables opened_table incremental 1 $((1 * mysql_update_every))
+CHART mysql_$m.operations '' "mysql Operations" "operations / sec" $m mysql line 20003 $mysql_update_every
+DIMENSION Opened_tables opened_tables incremental 1 $((1 * mysql_update_every))
DIMENSION Flush_commands flush incremental 1 $((1 * mysql_update_every))
DIMENSION Handler_commit commit incremental 1 $((1 * mysql_update_every))
DIMENSION Handler_delete delete incremental 1 $((1 * mysql_update_every))
@@ -60,22 +103,23 @@ DIMENSION Handler_savepoint_rollback savepoint_rollback incremental 1 $((1 * mys
DIMENSION Handler_update update incremental 1 $((1 * mysql_update_every))
DIMENSION Handler_write write incremental 1 $((1 * mysql_update_every))
-CHART mysql.table_locks '' "mysql Tables Locks" "locks / sec" mysql '' line 20004 $mysql_update_every
+CHART mysql_$m.table_locks '' "mysql Tables Locks" "locks / sec" $m mysql line 20004 $mysql_update_every
DIMENSION Table_locks_immediate immediate incremental 1 $((1 * mysql_update_every))
DIMENSION Table_locks_waited waited incremental -1 $((1 * mysql_update_every))
-CHART mysql.select_issues '' "mysql Select Issues" "issues / sec" mysql '' line 20005 $mysql_update_every
+CHART mysql_$m.select_issues '' "mysql Select Issues" "issues / sec" $m mysql line 20005 $mysql_update_every
DIMENSION Select_full_join full_join incremental 1 $((1 * mysql_update_every))
DIMENSION Select_full_range_join full_range_join incremental 1 $((1 * mysql_update_every))
DIMENSION Select_range range incremental 1 $((1 * mysql_update_every))
DIMENSION Select_range_check range_check incremental 1 $((1 * mysql_update_every))
DIMENSION Select_scan scan incremental 1 $((1 * mysql_update_every))
-CHART mysql.sort_issues '' "mysql Sort Issues" "issues / sec" mysql '' line 20006 $mysql_update_every
+CHART mysql_$m.sort_issues '' "mysql Sort Issues" "issues / sec" $m mysql line 20006 $mysql_update_every
DIMENSION Sort_merge_passes merge_passes incremental 1 $((1 * mysql_update_every))
DIMENSION Sort_range range incremental 1 $((1 * mysql_update_every))
DIMENSION Sort_scan scan incremental 1 $((1 * mysql_update_every))
EOF
+ done
return 0
}
@@ -99,24 +143,36 @@ mysql_update() {
# prepare the script and always grep at the end the lines that are usefull, so that
# even if something goes wrong, no other code can be executed
- eval "$(mysql_get_stats |\
- sed -e "s/[[:space:]]\+/ /g" -e "s/\./_/g" -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/local mysql_\1=\2/g" |\
- egrep "^local mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
+ local m x
+ for m in "${!mysql_ids[@]}"
+ do
+ x="${mysql_ids[$m]}"
+ mysql_plugin_command_failure=0
+ eval "$(mysql_get "${mysql_cmds[$m]}" ${mysql_opts[$m]} |\
+ sed -e "s/[[:space:]]\+/ /g" -e "s/\./_/g" -e "s/^\([a-zA-Z0-9_]\+\)[[:space:]]\+\([0-9]\+\)$/local mysql_\1=\2/g" |\
+ egrep "^local mysql_[a-zA-Z0-9_]+=[[:digit:]]+$")"
+
+ if [ $mysql_plugin_command_failure -ne 0 ]
+ then
+ unset mysql_ids[$m]
+ unset mysql_opts[$m]
+ unset mysql_cmds[$m]
+ echo >&2 "mysql: failed to get values for '$m', disabling it."
+ continue
+ fi
# write the result of the work.
cat <<VALUESEOF
-BEGIN mysql.bandwidth $1
+BEGIN mysql_$x.bandwidth $1
SET Bytes_received = $mysql_Bytes_received
SET Bytes_sent = $mysql_Bytes_sent
END
-
-BEGIN mysql.queries $1
+BEGIN mysql_$x.queries $1
SET Queries = $mysql_Queries
SET Questions = $mysql_Questions
SET Slow_queries = $mysql_Slow_queries
END
-
-BEGIN mysql.operations $1
+BEGIN mysql_$x.operations $1
SET Opened_tables = $mysql_Opened_tables
SET Flush_commands = $mysql_Flush_commands
SET Handler_commit = $mysql_Handler_commit
@@ -134,28 +190,26 @@ SET Handler_savepoint_rollback = $mysql_Handler_savepoint_rollback
SET Handler_update = $mysql_Handler_update
SET Handler_write = $mysql_Handler_write
END
-
-BEGIN mysql.table_locks $1
+BEGIN mysql_$x.table_locks $1
SET Table_locks_immediate = $mysql_Table_locks_immediate
SET Table_locks_waited = $mysql_Table_locks_waited
END
-
-BEGIN mysql.select_issues $1
+BEGIN mysql_$x.select_issues $1
SET Select_full_join = $mysql_Select_full_join
SET Select_full_range_join = $mysql_Select_full_range_join
SET Select_range = $mysql_Select_range
SET Select_range_check = $mysql_Select_range_check
SET Select_scan = $mysql_Select_scan
END
-
-BEGIN mysql.sort_issues $1
+BEGIN mysql_$x.sort_issues $1
SET Sort_merge_passes = $mysql_Sort_merge_passes
SET Sort_range = $mysql_Sort_range
SET Sort_scan = $mysql_Sort_scan
END
-
VALUESEOF
+ done
+ [ ${#mysql_ids[@]} -eq 0 ] && echo >&2 "mysql: no mysql servers left active." && return 1
return 0
}