diff options
author | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2015-11-19 01:23:42 +0200 |
---|---|---|
committer | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2015-11-19 01:23:42 +0200 |
commit | d919264e33308b44b18f04a46568c5cb801b5255 (patch) | |
tree | b2a944d19854c517c77a8303d6c3406a1dae8477 /charts.d | |
parent | 6ca901daefa6c7d63fe1a60f88d3db46cf14e117 (diff) |
minor fixes; mysql now accepts multiple servers
Diffstat (limited to 'charts.d')
-rw-r--r-- | charts.d/README.md | 78 | ||||
-rwxr-xr-x | charts.d/mysql.chart.sh | 118 | ||||
-rwxr-xr-x | charts.d/nut.chart.sh | 56 |
3 files changed, 202 insertions, 50 deletions
diff --git a/charts.d/README.md b/charts.d/README.md index 170f587f43..fd66c0d6a1 100644 --- a/charts.d/README.md +++ b/charts.d/README.md @@ -1,5 +1,83 @@ The following charts.d plugins are supported: +# mysql + +The plugin will monitor one or more mysql servers + +It will produce the following charts: + +1. **Bandwidth** in kbps + * in + * out + +2. **Queries** in queries/sec + * queries + * questions + * slow queries + +3. **Operations** in operations/sec + * opened tables + * flush + * commit + * delete + * prepare + * read first + * read key + * read next + * read prev + * read random + * read random next + * rollback + * save point + * update + * write + +4. **Table Locks** in locks/sec + * immediate + * waited + +5. **Select Issues** in issues/sec + * full join + * full range join + * range + * range check + * scan + +6. **Sort Issues** in issues/sec + * merge passes + * range + * scan + +### configuration + +You can configure many database servers, like this: + +You can provide, per server, the following: + +1. a name, anything you like, but keep it short +2. the mysql command to connect to the server +3. the mysql command line options to be used for connecting to the server + +Here is an example for 2 servers: + +```sh +mysql_opts[server1]="-h server1.example.com" +mysql_opts[server2]="-h server2.example.com --connect_timeout 2" +``` + +The above will use the `mysql` command found in the system path. +You can also provide a custom mysql command per server, like this: + +```sh +mysql_cmds[server2]="/opt/mysql/bin/mysql" +``` + +The above sets the mysql command only for server2. server1 will use the system default. + +If no configuration is given, the plugin will attempt to connect to mysql server at localhost. + +--- + # squid The plugin will monitor a squid server. 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 } diff --git a/charts.d/nut.chart.sh b/charts.d/nut.chart.sh index edb132d902..156e9bdec6 100755 --- a/charts.d/nut.chart.sh +++ b/charts.d/nut.chart.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # a space separated list of UPS names # if empty, the list returned by 'upsc -l' will be used @@ -7,24 +7,44 @@ nut_ups= # how frequently to collect UPS data nut_update_every=2 +nut_timeout=2 + +declare -A nut_ids=() + +nut_get_all() { + timeout $nut_timeout upsc -l +} + +nut_get() { + timeout $nut_timeout upsc "$1" +} + nut_check() { - local x all + # this should return: + # - 0 to enable the chart + # - 1 to disable the chart + + local x + + require_cmd timeout || return 1 require_cmd upsc || return 1 require_cmd awk || return 1 - all="$nut_ups" - [ -z "$all" ] && all="$(upsc -l)" + [ -z "$nut_ups" ] && nut_ups="$( nut_get_all )" - nut_ups= - for x in $all + for x in $nut_ups do - upsc "$x" >/dev/null - [ $? -eq 0 ] && nut_ups="$nut_ups $x" && continue + nut_get "$x" >/dev/null + if [ $? -eq 0 ] + then + nut_ids[$x]="$( fixid "$x" )" + continue + fi echo >&2 "nut: ERROR: Cannot get information for NUT UPS '$x'." done - if [ -z "$nut_ups" ] + if [ ${#nut_ids[@]} -eq 0 ] then echo >&2 "nut: Please set nut_ups='ups_name' in $confd/nut.conf" return 1 @@ -37,7 +57,7 @@ nut_create() { # create the charts local x - for x in $nut_ups + for x in "${nut_ids[@]}" do cat <<EOF CHART nut_$x.charge '' "UPS Charge" "percentage" $x nut area 21001 $nut_update_every @@ -84,11 +104,11 @@ nut_update() { # for each dimension # remember: KEEP IT SIMPLE AND SHORT - local x - - for x in $nut_ups + local i x + for i in "${!nut_ids[@]}" do - upsc "$x" | awk " + x="${nut_ids[$i]}" + nut_get "$i" | awk " BEGIN { battery_charge = 0; battery_voltage = 0; @@ -157,10 +177,10 @@ END { print \"BEGIN nut_$x.temp $1\"; print \"SET temp = \" temp; print \"END\" -} -" +}" + [ $? -ne 0 ] && unset nut_ids[$i] && echo >&2 "nut: failed to get values for '$i', disabling it." done - + + [ ${#nut_ids[@]} -eq 0 ] && echo >&2 "nut: no UPSes left active." && return 1 return 0 } - |