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

158 lines
5.4 KiB
Bash
Executable file

#!/bin/bash
#
# smtp_hello_ - munin plugin for measuring smtp hello response
# Copyright (C) 2008 Marek Mahut <mmahut@fedoraproject.org>
# Copyright (C) 2022 Andreas Perhab, WT-IO-IT GmbH <a.perhab@wtioit.at>
#
# Usage:
# ln -s /usr/share/munin/plugins/smtp_hello_ /etc/munin/plugins/smtp_hello_mysmtpserver.example.com
#
# Environment variables (optional):
# SMTP_PORT: port number to use for connection, default is 25
# SMTP_COMMAND: command to use to test the connection, default is "HELO localhost"
# TIMEOUT_SECONDS: seconds to take at most for the test, default is 120
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#$log$
#Revision 2.0 2022/05/03 09:15:32 Andreas Perhab (ap-wtioit)
#Improve compatiblity.
#
#%# family=auto
#%# capabilities=autoconf
. "$MUNIN_LIBDIR/plugins/plugin.sh"
if [ "${MUNIN_DEBUG:-0}" == "1" ]; then
set -x
fi
SMTP_COMMAND=${SMTP_COMMAND:-HELO localhost}
SMTP_PORTS=${SMTP_PORTS:-25}
TIMEOUT_SECONDS=${TIMEOUT_SECONDS:-120}
host=$(basename "$0" | sed 's/^smtp_hello_//g')
time_executable=""
if which time >/dev/null; then
# use time executable found in PATH
time_executable=$(which time)
elif [ -x /usr/bin/time ] ; then
# use time executable found in /usr/bin
time_executable="/usr/bin/time"
fi
if [ "$time_executable" != "" ] ; then
function take_the_time() {
"$time_executable" -f "%e" "$@" 2>&1 | tail -n 1
return "${PIPESTATUS[0]}"
}
elif { time echo; } >/dev/null 2>&1; then
# if we cannot find time executable but time keyword is successful we use the keyword
function take_the_time() {
# we use LC_ALL=C to force . as decimal separator on systems where LC_ is set to a language that uses ,
# to limit the effect of LC_ALL=C and TIMEFORMAT, as well as to be able to redirect stderr of the time keyword
# we use a subshell here
(
TIMEFORMAT='%R'
LC_ALL=C
time "$@" 1>/dev/null
) 2>&1
return $?
}
fi
nc_executable=""
if which nc >/dev/null ; then
# use nc from PATH
nc_executable=$(which nc)
elif [ -x /usr/bin/nc ] ; then
# as fallback use /usr/bin/nc if found
nc_executable="/usr/bin/nc"
fi
if which curl >/dev/null && curl_version_info=$(curl --version) && [[ "$curl_version_info" == *" smtp "* ]]; then
curl_with_smtp_support=1
fi
if [ "$1" == "config" ]; then
echo "graph_title $host smtp response time"
echo "graph_vlabel response in sec"
echo "graph_period minute"
echo "graph_category network"
echo "graph_args --base 1000 --lower-limit 0"
for port in $SMTP_PORTS; do
if [[ $port ]] ; then
if [[ $port = "25" ]] ; then
suffix=""
else
suffix="_$port"
fi
echo "host$suffix.label smtp port $port"
# default warning time begins at half the timeout
eval "export host${suffix}_warning=\${host${suffix}_warning:-:$((TIMEOUT_SECONDS / 2))}"
# default when we get values bigger than 99% of timeout we mark the value as critical
eval "export host${suffix}_critical=\${host${suffix}_critical:-:$((TIMEOUT_SECONDS * 99 / 100))}"
print_warning "host${suffix}"
print_critical "host${suffix}"
if [ "$curl_with_smtp_support" == "1" ] ; then
# if we have curl, all non-zero exit codes indicate an error
eval "export status${suffix}_warning=\${status${suffix}_warning:-0:0}"
echo "status$suffix.label smtp port $port check status"
print_warning "status${suffix}"
print_critical "status${suffix}"
fi
fi
done
elif [ "$1" == "autoconf" ]; then
if [ "$take_the_time" != "" ] && [ -x "$nc_executable" ]; then
echo "yes"
else
echo "no (/usr/bin/time or /usr/bin/nc missing)"
fi
exit 0
else
for port in $SMTP_PORTS; do
if [[ $port ]] ; then
if [[ $port = "25" ]] ; then
suffix=""
else
suffix="_$port"
fi
if [ "$curl_with_smtp_support" == "1" ] ; then
value=$(take_the_time curl --silent -X "$SMTP_COMMAND" --max-time "$TIMEOUT_SECONDS" "smtp://$host:$port/")
status=$?
else
# Note: "HELO localhost" only works if the SMTP server terminates the connection when presenting the HELO line
# if you have troubles here, try installing curl (with smtp support), and we will use a proper smtp client
# implementation. alternatively you can also set the SMTP_COMMAND environment variable to QUIT
value=$(echo "$SMTP_COMMAND" | take_the_time "$nc_executable" -C -w "$TIMEOUT_SECONDS" "$host" "$port")
# with the simple echo command we would need to check a lot of conditions to determine a successful status
# if you want this feature, install curl instead
status=""
fi
echo "host$suffix.value $value"
if [ -n "$status" ] ; then
echo "status$suffix.value $status"
fi
fi
done
fi