1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

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
This commit is contained in:
Lars Kruse 2018-11-20 01:40:09 +01:00
parent 48953d84d2
commit e48f0a3b79
2 changed files with 22 additions and 31 deletions

View file

@ -83,14 +83,14 @@ OTHER DEALINGS IN THE SOFTWARE.
=cut =cut
. "$MUNIN_LIBDIR/plugins/plugin.sh" . "$MUNIN_LIBDIR/plugins/plugin.sh"
regex=${regex:-}
logfiles=${logfiles:-}
LOGFILES="$(IFS= ; for f in $logfiles; do echo "$f"; done)" LOGFILES="$(IFS= ; for f in $logfiles; do echo "$f"; done)"
title="${title:-Log Matches}" title="${title:-Log Matches}"
function config() { function config() {
echo "graph_title ${title}" echo "graph_title ${title}"
echo "graph_args --base 1000 -l 0" echo "graph_args --base 1000 -l 0"
@ -111,13 +111,12 @@ function config() {
} }
function fetch() { function fetch() {
# Load state # Load state
touch "$MUNIN_STATEFILE" touch "$MUNIN_STATEFILE"
local curstate="$(cat "$MUNIN_STATEFILE")"
local nextstate=() local nextstate=()
local curstate
curstate="$(cat "$MUNIN_STATEFILE")"
local var_prefix logfile prvlines curlines matches local var_prefix logfile prvlines curlines matches
while read -u 3 -r logfile; do while read -u 3 -r logfile; do
@ -161,11 +160,7 @@ function fetch() {
} }
case "$1" in case "$1" in
config) config ;; config) config ;;
*) fetch ;; *) fetch ;;
esac esac

View file

@ -83,7 +83,7 @@ For plugin-specific options, the following rules apply:
"nginx", "apt", "syslog", etc. "nginx", "apt", "syslog", etc.
* <service> is a string derived by passing the service name through a filter * <service> is a string derived by passing the service name through a filter
that removes non-alphabet characters from the beginning and replaces all non- 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<< <service>_logbinding >> on the * logfiles are bound to services by matching C<< <service>_logbinding >> on the
full logfile path. For example, specifying C<my_site_logbinding=my-site> would full logfile path. For example, specifying C<my_site_logbinding=my-site> would
bind both F</var/log/my-site/errors.log> and F</srv/www/my-site/logs/app.log> bind both F</var/log/my-site/errors.log> and F</srv/www/my-site/logs/app.log>
@ -122,7 +122,7 @@ This example uses services autoconf:
env.my_special_service_warning 100 env.my_special_service_warning 100
env.my_special_service_critical 300 env.my_special_service_critical 300
This example DOESN'T use services autoconf: This example DOES NOT use services autoconf:
[service_events] [service_events]
user root user root
@ -188,17 +188,19 @@ OTHER DEALINGS IN THE SOFTWARE.
=cut =cut
services_autoconf=${services_autoconf:-}
# Get list of all currently set env variables # 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) # Certain variables MUST be set; check that they are (using bitmask)
setvars=0 setvars=0
reqvars=(_logfiles _regex) reqvars=(_logfiles _regex)
while read -u 3 -r v; do while read -u 3 -r v; do
n=0 n=0
while [ $n -lt "${#reqvars[@]}" ]; do while [ "$n" -lt "${#reqvars[@]}" ]; do
if echo "$v" | grep -Eq "${reqvars[$n]}$"; then if echo "$v" | grep -Eq "${reqvars[$n]}$"; then
setvars=$((setvars | $(( 2 ** $n )) )) setvars=$((setvars | (2 ** n) ))
fi fi
n=$((n+1)) n=$((n+1))
done done
@ -208,8 +210,8 @@ done 3< <(echo "$vars")
# Sum all required variables # Sum all required variables
n=0 n=0
allvars=0 allvars=0
while [ $n -lt "${#reqvars[@]}" ]; do while [ "$n" -lt "${#reqvars[@]}" ]; do
allvars=$(( allvars + $(( 2 ** $n )) )) allvars=$(( allvars + 2 ** n ))
n=$((n+1)) n=$((n+1))
done done
@ -219,8 +221,8 @@ if ! [ "$setvars" -eq "$allvars" ]; then
>&2 echo >&2 echo
n=0 n=0
i=1 i=1
while [ $n -lt "${#reqvars[@]}" ]; do while [ "$n" -lt "${#reqvars[@]}" ]; do
if [ $(( $setvars & $i )) -eq 0 ]; then if [ $(( setvars & i )) -eq 0 ]; then
>&2 echo " *${reqvars[$n]}" >&2 echo " *${reqvars[$n]}"
fi fi
i=$((i<<1)) i=$((i<<1))
@ -301,15 +303,13 @@ function config() {
} }
function fetch() { function fetch() {
# Load state local curstate n svcnm varnm service svc svc_counter_var logbinding logfile lognm logmatch prvlines curlines matches extinfo_var
touch "$MUNIN_STATEFILE"
local curstate="$(cat "$MUNIN_STATEFILE")"
local nextstate=() 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 # Set service counters to 0 and set any logbindings that aren't yet set
while read -u 3 -r svc; do while read -u 3 -r svc; do
@ -379,7 +379,7 @@ function fetch() {
if [ "$matches" -gt 0 ]; then if [ "$matches" -gt 0 ]; then
# Aggregate and add to the correct service counter # Aggregate and add to the correct service counter
svc_counter_var="${svcnm}_total" svc_counter_var="${svcnm}_total"
matches=$(($matches + ${!svc_counter_var})) matches=$((matches + ${!svc_counter_var}))
typeset "$svc_counter_var=$matches" typeset "$svc_counter_var=$matches"
# Add this log to extinfo for service # Add this log to extinfo for service
@ -409,11 +409,7 @@ function fetch() {
} }
case "$1" in case "$1" in
config) config ;; config) config ;;
*) fetch ;; *) fetch ;;
esac esac