1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-25 10:28:36 +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
set -e
: << =cut
=head1 NAME
@ -91,17 +93,18 @@ function config() {
echo "graph_title ${title}"
echo "graph_args --base 1000 -l 0"
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"
local nm logfile lbl
local var_prefix var logfile lbl
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')"
lbl="$(eval "echo \$${nm}_label")"
echo "$nm.label $([ -n "$lbl" ] && echo "$lbl" || echo "$logfile")"
print_warning "$nm"
print_critical "$nm"
echo "$nm.info Lines that match '${regex}' in log file '$logfile'"
var_prefix="$(echo "$logfile" | sed -r 's/^[^a-zA-Z]+//g' | sed -r 's/[^a-zA-Z0-9]+/_/g')"
var="${var_prefix}_label"
lbl="${!var:-0}"
echo "$var_prefix.label $([ -n "$lbl" ] && echo "$lbl" || echo "$logfile")"
print_warning "$var_prefix"
print_critical "$var_prefix"
echo "$var_prefix.info Lines that match '${regex}' in log file '$logfile'"
done 3< <(echo "$LOGFILES")
}
@ -111,21 +114,23 @@ function config() {
function fetch() {
# Load state
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
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
prvlines="$(eval "echo \$${nm}_lines")"
prvlines="$(echo "$curstate" | grep "^${var_prefix}_lines=" | cut -f 2 -d "=")"
if [ -z "$prvlines" ]; then
prvlines=0
fi
# Get the current number of lines in the file
curlines="$(wc -l "$logfile" | egrep -o '^[0-9]+')"
if ! [ "$curlines" -eq "$curlines" ] &>/dev/null; then
curlines="$(wc -l < "$logfile")"
if [ -z "$curlines" ]; then
curlines=0
fi
@ -138,15 +143,18 @@ function fetch() {
fi
# 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 "$nm.value $matches"
echo "$var_prefix.value $matches"
# Update the statefile
sed -i "/^$nm.*/d" "$MUNIN_STATEFILE"
echo "${nm}_lines=$curlines" >> "$MUNIN_STATEFILE"
# Push onto next state
nextstate+=("${var}_lines=$curlines")
done 3< <(echo "$LOGFILES")
# Write state to munin statefile
(IFS=$'\n'; echo "${nextstate[*]}" > "$MUNIN_STATEFILE")
return 0
}
@ -155,7 +163,6 @@ function fetch() {
case "$1" in
autoconf) echo no; exit 0 ;;
config) config ;;
*) fetch ;;
esac