1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

Addressed PR suggestions (round 1)

This commit is contained in:
Kael Shipman 2018-11-15 00:18:47 -06:00
parent 0d4ad57fc9
commit 19a3cbe397
2 changed files with 52 additions and 41 deletions

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
set -e
: << =cut : << =cut
=head1 NAME =head1 NAME
@ -91,17 +93,18 @@ function config() {
echo "graph_title ${title}" echo "graph_title ${title}"
echo "graph_args --base 1000 -l 0" echo "graph_args --base 1000 -l 0"
echo "graph_vlabel ${title}" echo "graph_vlabel ${title}"
echo "graph_category logevents" echo "graph_category other"
echo "graph_info Lists number of times the given regex is matched in the given log files per period" echo "graph_info Lists number of times the given regex is matched in the given log files per period"
local nm logfile lbl local var_prefix var logfile lbl
while read -u 3 -r logfile; do while read -u 3 -r logfile; do
nm="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" var_prefix="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
lbl="$(eval "echo \$${nm}_label")" var="${var_prefix}_label"
echo "$nm.label $([ -n "$lbl" ] && echo "$lbl" || echo "$logfile")" lbl="${!var:-0}"
print_warning "$nm" echo "$var_prefix.label $([ -n "$lbl" ] && echo "$lbl" || echo "$logfile")"
print_critical "$nm" print_warning "$var_prefix"
echo "$nm.info Lines that match '${regex}' in log file '$logfile'" print_critical "$var_prefix"
echo "$var_prefix.info Lines that match '${regex}' in log file '$logfile'"
done 3< <(echo "$LOGFILES") done 3< <(echo "$LOGFILES")
} }
@ -111,21 +114,23 @@ function config() {
function fetch() { function fetch() {
# Load state # Load state
touch "$MUNIN_STATEFILE" touch "$MUNIN_STATEFILE"
. "$MUNIN_STATEFILE" local curstate="$(cat "$MUNIN_STATEFILE")"
local nextstate=()
local nm 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
nm="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" # Convert current logfile path to variable prefix
var_prefix="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
# Get running number of lines to determine whether or not the file may have been rotated # Get running number of lines to determine whether or not the file may have been rotated
prvlines="$(eval "echo \$${nm}_lines")" prvlines="$(echo "$curstate" | grep "^${var_prefix}_lines=" | cut -f 2 -d "=")"
if [ -z "$prvlines" ]; then if [ -z "$prvlines" ]; then
prvlines=0 prvlines=0
fi fi
# Get the current number of lines in the file # Get the current number of lines in the file
curlines="$(wc -l "$logfile" | egrep -o '^[0-9]+')" curlines="$(wc -l < "$logfile")"
if ! [ "$curlines" -eq "$curlines" ] &>/dev/null; then if [ -z "$curlines" ]; then
curlines=0 curlines=0
fi fi
@ -138,15 +143,18 @@ function fetch() {
fi fi
# Get current number of incidents # Get current number of incidents
matches="$(tail -n +"$prvlines" "$logfile" | egrep -c "${regex}" || true)" matches="$(tail -n +"$prvlines" "$logfile" | grep -Ec "${regex}" || true)"
# Echo the value # Echo the value
echo "$nm.value $matches" echo "$var_prefix.value $matches"
# Update the statefile # Push onto next state
sed -i "/^$nm.*/d" "$MUNIN_STATEFILE" nextstate+=("${var}_lines=$curlines")
echo "${nm}_lines=$curlines" >> "$MUNIN_STATEFILE"
done 3< <(echo "$LOGFILES") done 3< <(echo "$LOGFILES")
# Write state to munin statefile
(IFS=$'\n'; echo "${nextstate[*]}" > "$MUNIN_STATEFILE")
return 0 return 0
} }
@ -155,7 +163,6 @@ function fetch() {
case "$1" in case "$1" in
autoconf) echo no; exit 0 ;;
config) config ;; config) config ;;
*) fetch ;; *) fetch ;;
esac esac

View file

@ -154,7 +154,7 @@ 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" | egrep -q "${reqvars[$n]}$"; then if echo "$v" | grep -Eq "${reqvars[$n]}$"; then
!((setvars|=$(( 2 ** $n )) )) !((setvars|=$(( 2 ** $n )) ))
fi fi
!((n++)) !((n++))
@ -203,7 +203,7 @@ fi
LOGFILES= LOGFILES=
declare -a LOGFILEMAP declare -a LOGFILEMAP
while read -u 3 -r v; do while read -u 3 -r v; do
if echo "$v" | egrep -q "_logfiles$"; then if echo "$v" | grep -Eq "_logfiles$"; then
# Get the name associated with these logfiles # Get the name associated with these logfiles
logfiletype="${v%_logfiles}" logfiletype="${v%_logfiles}"
# This serves to expand globs while preserving spaces (and also appends the necessary newline) # This serves to expand globs while preserving spaces (and also appends the necessary newline)
@ -244,15 +244,16 @@ function config() {
echo "graph_title ${title}" echo "graph_title ${title}"
echo "graph_args --base 1000 -l 0" echo "graph_args --base 1000 -l 0"
echo "graph_vlabel ${vlabel}" echo "graph_vlabel ${vlabel}"
echo "graph_category logevents" echo "graph_category other"
echo "graph_info Lists number of matching lines found in various logfiles associated with each service" echo "graph_info Lists number of matching lines found in various logfiles associated with each service"
local var_prefix
while read -u 3 -r svc; do while read -u 3 -r svc; do
nm="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" var_prefix="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
echo "$nm.label $svc" echo "$var_prefix.label $svc"
print_warning "$nm" print_warning "$var_prefix"
print_critical "$nm" print_critical "$var_prefix"
echo "$nm.info Number of event occurrences for $svc" echo "$var_prefix.info Number of event occurrences for $svc"
done 3< <(IFS=$'\n'; echo "${services[*]}") done 3< <(IFS=$'\n'; echo "${services[*]}")
} }
@ -262,18 +263,19 @@ function config() {
function fetch() { function fetch() {
# Load state # Load state
touch "$MUNIN_STATEFILE" touch "$MUNIN_STATEFILE"
. "$MUNIN_STATEFILE" local curstate="$(cat "$MUNIN_STATEFILE")"
local nextstate=()
local n svcnm service svc svc_counter logbinding logfile lognm logmatch prvlines curlines matches local n svcnm varnm service svc svc_counter logbinding logfile lognm logmatch prvlines curlines matches
# 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
svcnm="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" svcnm="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
typeset "${svcnm}_total=0" typeset "${svcnm}_total=0"
v="${svcnm}_logbinding" varnm="${svcnm}_logbinding"
if [ -z "${!v}" ]; then if [ -z "$(echo "$curstate" | grep "^${varnm}=" | cut -f 2 -d "=")" ]; then
typeset "$v=$svc" typeset "$varnm=$svc"
fi fi
done 3< <(IFS=$'\n'; echo "${services[*]}") done 3< <(IFS=$'\n'; echo "${services[*]}")
@ -288,7 +290,7 @@ function fetch() {
service= service=
while read -u 4 -r svc; do while read -u 4 -r svc; do
logbinding="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')_logbinding" logbinding="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')_logbinding"
if echo "$logfile" | egrep -q "${!logbinding}"; then if echo "$logfile" | grep -Eq "${!logbinding}"; then
service="$svc" service="$svc"
break break
fi fi
@ -305,13 +307,13 @@ function fetch() {
lognm="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" lognm="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
# Get previous line count to determine whether or not the file may have been rotated # Get previous line count to determine whether or not the file may have been rotated
prvlines="$(eval "echo \$${lognm}_lines")" prvlines="$(echo "$curstate" | grep "^${lognm}_lines=" | cut -f 2 -d "=")"
if [ -z "$prvlines" ]; then if [ -z "$prvlines" ]; then
prvlines=0 prvlines=0
fi fi
# Get the current number of lines in the file # Get the current number of lines in the file
curlines="$(wc -l "$logfile" | egrep -o '^[0-9]+')" curlines="$(wc -l < "$logfile")"
if ! [ "$curlines" -eq "$curlines" ] &>/dev/null; then if ! [ "$curlines" -eq "$curlines" ] &>/dev/null; then
curlines=0 curlines=0
fi fi
@ -326,19 +328,22 @@ function fetch() {
# Get incidents starting at the line after the last line we've seen # Get incidents starting at the line after the last line we've seen
logmatch="${LOGFILEMAP[$n]}_regex" logmatch="${LOGFILEMAP[$n]}_regex"
matches="$(tail -n +"$prvlines" "$logfile" | egrep -c "${!logmatch}" || true)" matches="$(tail -n +"$prvlines" "$logfile" | grep -Ec "${!logmatch}" || true)"
# Aggregate and add to the correct service counter # Aggregate and add to the correct service counter
svc_counter="${svcnm}_total" svc_counter="${svcnm}_total"
!((matches+=${!svc_counter})) !((matches+=${!svc_counter}))
typeset "$svc_counter=$matches" typeset "$svc_counter=$matches"
# Update the statefile # Push onto next state
sed -i "/^${lognm}_.*/d" "$MUNIN_STATEFILE" nextstate+=("${lognm}_lines=$curlines")
echo "${lognm}_lines=$curlines" >> "$MUNIN_STATEFILE"
!((n++)) !((n++))
done 3< <(echo "$LOGFILES") done 3< <(echo "$LOGFILES")
# Write state to munin statefile
(IFS=$'\n'; echo "${nextstate[*]}" > "$MUNIN_STATEFILE")
# Now echo values # Now echo values
while read -u 3 -r svc; do while read -u 3 -r svc; do
svcnm="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')" svcnm="$(echo "$svc" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
@ -354,7 +359,6 @@ function fetch() {
case "$1" in case "$1" in
autoconf) echo no; exit 0 ;;
config) config ;; config) config ;;
*) fetch ;; *) fetch ;;
esac esac