1
0
Fork 0
scripts-admin-quickndirty-p.../nagios/check_sendmail-mailstats.sh

50 lines
1.6 KiB
Bash
Executable file

#!/bin/sh
# Little Nagios check to save the Sendmail mailstats
CATEGORY=""
OUTPUT_PERFDATA=""
# Stop at the first non-catched error
set -e
# We use the "idiom" suggested by http://www.etalabs.net/sh_tricks.html
# based on "done <<EOF $( command ) EOF"
# to circumvent the creation of a subshell for the while loop
# where the $OUTPUT_PERFDATA variable would be local and we
# would not get the value outside of the loop.
while read LINETYPE msgsfr bytes_from msgsto bytes_to msgsrej msgsdis msgsqur mailer; do
case "$LINETYPE" in
"MSP"|"MTA")
CATEGORY="$LINETYPE"
;;
"T")
# for the sake of Icinga, print the elements in alphabetical order
for i in msgsfr bytes_from msgsto bytes_to msgsrej msgsdis msgsqur mailer; do
OUTPUT_PERFDATA="$( printf "%s %s_msg_%s=%d" "$OUTPUT_PERFDATA" "$CATEGORY" "$i" "$( eval echo "$"$i )" )"
done
;;
"C")
# for the sake of Icinga, print the elements in alphabetical order
for i in msgsfr bytes_from; do
OUTPUT_PERFDATA="$( printf "%s %s_tcp_%s=%d" "$OUTPUT_PERFDATA" "$CATEGORY" "$i" "$( eval echo "$"$i )" )"
done
;;
esac
done <<EOF
$(mailstats -P 2>/dev/null)
EOF
# if not all data is present, we return an unknown status
# (remove this block if you don't mind)
if ! echo "$OUTPUT_PERFDATA" | grep "MTA_msg" >/dev/null 2>&1 \
|| ! echo "$OUTPUT_PERFDATA" | grep "MSP_msg" >/dev/null 2>&1 \
|| ! echo "$OUTPUT_PERFDATA" | grep "MTA_tcp" >/dev/null 2>&1 \
|| ! echo "$OUTPUT_PERFDATA" | grep "MSP_tcp" >/dev/null 2>&1 \
; then
echo "UNKNOWN"
exit 3
fi
# Remove starting space to OUTPUT_PERFDATA
printf "OK|%s\n" "$( echo $OUTPUT_PERFDATA )"