From e48f0a3b799e81c7e1ca7827176f4247116dacff Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Tue, 20 Nov 2018 01:40:09 +0100 Subject: [PATCH] Minor cleanup of logs/ plugins * fix spelling mistake (thanks, codespell) * do not hide exit code of command substitution via "local" (thanks, shellcheck) * avoid access of potentially undefined variables (thanks, shellcheck) * fix tabs/spaces * avoid variable substitution in arithmetic substitution * reduce number of successive blank lines at the top level down to two * simplify evaluation of "printenv" output --- plugins/logs/loggrepx_ | 15 +++++---------- plugins/logs/service_events | 38 +++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/plugins/logs/loggrepx_ b/plugins/logs/loggrepx_ index 69c6ab4d..cf156f00 100755 --- a/plugins/logs/loggrepx_ +++ b/plugins/logs/loggrepx_ @@ -83,14 +83,14 @@ OTHER DEALINGS IN THE SOFTWARE. =cut - . "$MUNIN_LIBDIR/plugins/plugin.sh" + +regex=${regex:-} +logfiles=${logfiles:-} LOGFILES="$(IFS= ; for f in $logfiles; do echo "$f"; done)" title="${title:-Log Matches}" - - function config() { echo "graph_title ${title}" echo "graph_args --base 1000 -l 0" @@ -111,13 +111,12 @@ function config() { } - - function fetch() { # Load state touch "$MUNIN_STATEFILE" - local curstate="$(cat "$MUNIN_STATEFILE")" local nextstate=() + local curstate + curstate="$(cat "$MUNIN_STATEFILE")" local var_prefix logfile prvlines curlines matches while read -u 3 -r logfile; do @@ -161,11 +160,7 @@ function fetch() { } - - - case "$1" in config) config ;; *) fetch ;; esac - diff --git a/plugins/logs/service_events b/plugins/logs/service_events index 3f08b88a..d16554f1 100755 --- a/plugins/logs/service_events +++ b/plugins/logs/service_events @@ -83,7 +83,7 @@ For plugin-specific options, the following rules apply: "nginx", "apt", "syslog", etc. * is a string derived by passing the service name through a filter that removes non-alphabet characters from the beginning and replaces all non- - alpha-numeric characters with underscore (C<_>). + alphanumeric characters with underscore (C<_>). * logfiles are bound to services by matching C<< _logbinding >> on the full logfile path. For example, specifying C would bind both F and F @@ -122,12 +122,12 @@ This example uses services autoconf: env.my_special_service_warning 100 env.my_special_service_critical 300 -This example DOESN'T use services autoconf: +This example DOES NOT use services autoconf: [service_events] user root env.services auth.example.com admin.example.com www.example.com - env.auth_example_com_logbinding my-custom-binding[0-9]+ + env.auth_example_com_logbinding my-custom-binding[0-9]+ env.cfxsvc_logfiles /srv/*/*/logs/app.log env.cfxsvc_regex error|alert|crit|emerg env.phpfpm_logfiles /srv/*/*/logs/php-fpm*.log @@ -188,17 +188,19 @@ OTHER DEALINGS IN THE SOFTWARE. =cut +services_autoconf=${services_autoconf:-} + # Get list of all currently set env variables -vars="$(printenv | sed -r "s/^([^=]+).*/\1/g")" +vars=$(printenv | cut -f 1 -d "=") # Certain variables MUST be set; check that they are (using bitmask) setvars=0 reqvars=(_logfiles _regex) while read -u 3 -r v; do n=0 - while [ $n -lt "${#reqvars[@]}" ]; do + while [ "$n" -lt "${#reqvars[@]}" ]; do if echo "$v" | grep -Eq "${reqvars[$n]}$"; then - setvars=$((setvars | $(( 2 ** $n )) )) + setvars=$((setvars | (2 ** n) )) fi n=$((n+1)) done @@ -208,8 +210,8 @@ done 3< <(echo "$vars") # Sum all required variables n=0 allvars=0 -while [ $n -lt "${#reqvars[@]}" ]; do - allvars=$(( allvars + $(( 2 ** $n )) )) +while [ "$n" -lt "${#reqvars[@]}" ]; do + allvars=$(( allvars + 2 ** n )) n=$((n+1)) done @@ -219,8 +221,8 @@ if ! [ "$setvars" -eq "$allvars" ]; then >&2 echo n=0 i=1 - while [ $n -lt "${#reqvars[@]}" ]; do - if [ $(( $setvars & $i )) -eq 0 ]; then + while [ "$n" -lt "${#reqvars[@]}" ]; do + if [ $(( setvars & i )) -eq 0 ]; then >&2 echo " *${reqvars[$n]}" fi i=$((i<<1)) @@ -301,15 +303,13 @@ function config() { } - - function fetch() { - # Load state - touch "$MUNIN_STATEFILE" - local curstate="$(cat "$MUNIN_STATEFILE")" + local curstate n svcnm varnm service svc svc_counter_var logbinding logfile lognm logmatch prvlines curlines matches extinfo_var local nextstate=() - local n svcnm varnm service svc svc_counter_var logbinding logfile lognm logmatch prvlines curlines matches extinfo_var + # Load state + touch "$MUNIN_STATEFILE" + curstate="$(cat "$MUNIN_STATEFILE")" # Set service counters to 0 and set any logbindings that aren't yet set while read -u 3 -r svc; do @@ -379,7 +379,7 @@ function fetch() { if [ "$matches" -gt 0 ]; then # Aggregate and add to the correct service counter svc_counter_var="${svcnm}_total" - matches=$(($matches + ${!svc_counter_var})) + matches=$((matches + ${!svc_counter_var})) typeset "$svc_counter_var=$matches" # Add this log to extinfo for service @@ -409,11 +409,7 @@ function fetch() { } - - - case "$1" in config) config ;; *) fetch ;; esac -