muninlite/muninlite.in

128 lines
3.4 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#
2019-07-23 04:52:37 +02:00
# Simple Bourne Shell script that implements Munin protocoll and
# some common Linux plugins.
#
# For latest version, see http://muninlite.sf.net/
#
# Copyright (c) 2007-2011 Rune Nordb<64>e Skillingstad <rune@skillingstad.no>
2019-07-23 04:52:37 +02:00
#
# Licensed under GPLv2 (see LICENSE file for full License)
#
VERSION="@@VERSION@@"
@@CONF@@
# if plugindir_ is present in $PLUGINS, executables (scripts, binaries) in the specified path
# and matching the pattern will be scanned and operated as plugins
PLUGIN_DIRECTORY="$(dirname "$0")/munin-node-plugin.d"
PLUGINPATTERN="*"
# Remove unwanted plugins from this list
PLUGINS="@@PLUGINS@@"
# ===== LIB FUNCTIONS =====
clean_fieldname() {
echo "$@" | sed -e 's/^[^A-Za-z_]/_/' -e 's/[^A-Za-z0-9_]/_/g'
}
# ===== PLUGINS CODE =====
@@PLSTR@@
# ===== NODE CODE =====
do_list() {
2020-02-07 15:15:06 +01:00
echo "$PLUGINS"
}
do_nodes() {
echo "$HOSTNAME"
echo "."
}
do_config() {
if echo "$PLUGINS" | grep -qwF "$1"; then
2020-02-07 15:15:06 +01:00
"config_$1"
else
echo "# Unknown service"
fi
echo "."
}
do_fetch() {
if echo "$PLUGINS" | grep -qwF "$1"; then
2020-02-07 15:15:06 +01:00
"fetch_$1"
else
echo "# Unknown service"
fi
echo "."
}
do_version() {
echo "munins node on $HOSTNAME version: $VERSION (muninlite)"
}
do_quit() {
exit 0
}
# ===== Runtime config =====
RES=""
for PLUG in $PLUGINS
2019-07-23 04:52:37 +02:00
do
if [ "$PLUG" = "if_" ]; then
for INTER in $(grep -E '^ *(ppp|eth|wlan|ath|ra|ipsec|tap|br-)[^:]{1,}:' /proc/net/dev | cut -f1 -d: | sed 's/ //g');
do
2020-02-07 15:15:06 +01:00
INTERRES=$(echo "$INTER" | sed -e 's/\./VLAN/' -e 's/\-/_/')
RES="$RES if_$INTERRES"
2020-02-07 15:15:06 +01:00
eval 'fetch_if_'"$INTERRES"'() { fetch_if "'"$INTER"'" "$@"; };'
eval 'config_if_'"$INTERRES"'() { config_if "'"$INTER"'" "$@"; };'
done
elif [ "$PLUG" = "if_err_" ]; then
for INTER in $(grep -E '^ *(ppp|eth|wlan|ath|ra|ipsec|tap|br-)[^:]{1,}:' /proc/net/dev | cut -f1 -d: | sed 's/ //g');
do
2020-02-07 15:15:06 +01:00
INTERRES=$(echo "$INTER" | sed -e 's/\./VLAN/' -e 's/\-/_/')
RES="$RES if_err_$INTERRES"
2020-02-07 15:15:06 +01:00
eval 'fetch_if_err_'"$INTERRES"'() { fetch_if_err "'"$INTER"'" "$@"; };'
eval 'config_if_err_'"$INTERRES"'() { config_if_err "'"$INTER"'" "$@"; };'
done
elif [ "$PLUG" = "netstat" ]; then
if netstat -s >/dev/null 2>&1; then
RES="$RES netstat"
fi
elif [ "$PLUG" = "plugindir_" ]; then
for MYPLUGIN in $(if [ -d "$PLUGIN_DIRECTORY" ]; then find -L "$PLUGIN_DIRECTORY" -type f -name "$PLUGINPATTERN"; fi);
do
2020-02-07 15:15:06 +01:00
if [ -f "$MYPLUGIN" ] && [ -x "$MYPLUGIN" ]; then
MYPLUGINNAME=$(basename "$MYPLUGIN")
#ensure we don't have name collision
if echo "$RES" | grep -qwF "$MYPLUGINNAME"; then
MYPLUGINNAME="plugindir_$MYPLUGINNAME"
fi
RES="$RES $MYPLUGINNAME"
2020-02-07 15:15:06 +01:00
eval "fetch_${MYPLUGINNAME}() { '$MYPLUGIN'; };"
eval "config_${MYPLUGINNAME}() { '$MYPLUGIN' config; };"
fi
done
else
2019-07-23 04:52:37 +02:00
RES="$RES $PLUG"
fi
done
# sort plugin names and remove surrounding whitespace
PLUGINS=$(echo "$RES" | xargs -r -n 1 echo | sort | xargs echo)
# ===== MAIN LOOP =====
FUNCTIONS="list nodes config fetch version quit"
HOSTNAME=$(hostname -f 2>/dev/null || hostname)
echo "# munin node at $HOSTNAME"
2019-07-23 04:52:37 +02:00
while read arg0 arg1
do
arg0=$(echo "$arg0" | xargs)
2019-07-23 04:52:37 +02:00
arg1=$(echo "$arg1" | xargs)
if ! echo "$FUNCTIONS" | grep -qwF "$arg0"; then
2020-02-07 15:15:06 +01:00
echo "# Unknown command. Try $(echo "$FUNCTIONS" | sed -e 's/\( [[:alpha:]]\{1,\}\)/,\1/g' -e 's/,\( [[:alpha:]]\{1,\}\)$/ or\1/')"
else
"do_$arg0" "$arg1"
fi
2019-07-23 04:52:37 +02:00
done