#!/bin/sh # Munin plugin for monitoring Postfix per-service and per-instance # usage. Symlink/rename to 'postfix_sasl_$SERVICE_$SYSLOGNAME'. # Copyright © 2015 Guilhem Moulin # # 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 3 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, see . PATH=/usr/bin:/bin:/usr/sbin:/sbin set -u LOGFILE="/var/log/${logfile:-mail.log}" STATEFILE="$MUNIN_PLUGSTATE/${0##*/}.offset" name="${0##*/}" name="${name#postfix_stats_}" if [ "${name#postscreen_}" != "$name" ]; then postfix="${name#postscreen_}" service=postscreen fields='connected accepted passed' elif [ "${name#smtpd_}" != "$name" ]; then postfix="${name#smtpd_}" service=smtpd fields='connected accepted queued' elif [ "${name#qmgr_}" != "$name" ]; then postfix="${name#qmgr_}" service=qmgr fields='queued' elif [ "${name#smtp_}" != "$name" ]; then postfix="${name#smtp_}" service=smtp fields='sent deliverable deferred undeliverable bounced' elif [ "${name#lmtp_}" != "$name" ]; then postfix="${name#lmtp_}" service=lmtp fields='sent deliverable deferred undeliverable bounced' elif [ "${name#pipe_}" != "$name" ]; then postfix="${name#pipe_}" service=pipe fields='sent deliverable deferred undeliverable bounced' fi case "${1:-}" in config) cat <<- EOF graph_title Postfix $service message throughput ($([ -n "$postfix" -a "$postfix" != postfix ] && echo "'${postfix#postfix-}'" || echo default) instance) graph_vlabel mails per \${graph_period} graph_args --base 1000 -l 0 graph_scale no graph_period minute graph_category postfix EOF for field in $fields; do echo "$field.label $field" echo "$field.type DERIVE" echo "$field.draw AREA" echo "$field.min 0" done exit 0 ;; esac if [ ! -f "$LOGFILE" ]; then for field in $fields; do echo "$field.value U" done exit 0; fi tmpfile="$(mktemp --tmpdir)" trap 'rm -f "$tmpfile"' EXIT logtail -f"$LOGFILE" -o"$STATEFILE" | sed -nr "s#^\w{3} [ :[:digit:]]{11} [._[:alnum:]-]+ $postfix/$service\[[[:digit:]]+\]: ##p" >"$tmpfile" case "$service" in postscreen) connected=$(grep -Ec "^CONNECT from\s" "$tmpfile") rejected=$( grep -Ec "^NOQUEUE: reject:\s" "$tmpfile") passed=$( grep -Ec "^(PASS (OLD|NEW)|WHITELISTED)\s" "$tmpfile") echo connected.value $connected echo accepted.value $(($rejected + $passed)) echo passed.value $passed ;; smtpd) connected=$(grep -Ec "^connect from\s" "$tmpfile") rejected=$( grep -Ec "^NOQUEUE: reject:\s" "$tmpfile") queued=$( grep -Ec "^[[:xdigit:]]+: client=" "$tmpfile") echo connected.value $connected echo accepted.value $(($rejected + $queued)) echo queued.value $queued ;; qmgr) queued=$(grep -Ec "^[[:xdigit:]]+: from=<\S+>, .*\(queue active\)$" "$tmpfile") echo queued.value $queued ;; smtp|lmtp|pipe) for field in $fields; do v=$(grep -Ec "^[[:xdigit:]]+:( [^ =]+=\S+,)+ status=$field(\s.*)?$" "$tmpfile") echo $field.value $v done ;; esac