postfix-stats/collector.sh

92 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
#
# This script is supposed to be called periodically.
# It goes through the last few lines of a logfile and counts some events.
# The events are then reported to something.
#
# Debugging option
#set -x
# Go into appropriate directory
cd `dirname $0`
# Load configuration
. ./settings.sh
### COUNTERS ###
log_lines=0
mail_sent=0
mail_deferred=0
mail_bounced_total=0
mail_bounced_spam=0
mail_reputation=0
### First time setup (if required)
if [ ! -f "$DBFILE" ]; then
# Get initial log position
new_log_position=$(wc -l $LOGFILE | cut -d ' ' -f 1)
# Write initial log position
echo $new_log_position > $DBFILE
# Create rrd file
rrdtool create $RRDFILE \
--start `date +%s` \
--step 60 \
DS:log_lines:GAUGE:60:0:U \
DS:mail_sent:GAUGE:60:0:U \
DS:mail_deferred:GAUGE:60:0:U \
DS:mail_bounced_total:GAUGE:60:0:U \
DS:mail_bounced_spam:GAUGE:60:0:U \
DS:mail_reputation:GAUGE:60:0:U \
RRA:AVERAGE:0.5:1:1440 \
RRA:AVERAGE:0.5:15:1344 \
RRA:AVERAGE:0.5:30:720 \
RRA:AVERAGE:0.5:720:730 \
RRA:MAX:0.5:1:1440 \
RRA:MAX:0.5:15:1344 \
RRA:MAX:0.5:30:720 \
RRA:MAX:0.5:720:730 \
# Quick fix so rrdtool does not complain about updating too soon
sleep 1
fi
### Action ###
# Get old log position
old_log_position=$(cat $DBFILE)
# Get new log position
new_log_position=$(wc -l $LOGFILE | cut -d ' ' -f 1)
# Write new log position
echo $new_log_position > $DBFILE
# Get date for new log position
db_last_modified=$(stat --terse $DBFILE | cut -d ' ' -f 13)
# Make sure we do not have a log rotation going on
# This is visible when old_log_position > new_log_position.
if [ $new_log_position -lt $old_log_position ]; then
# In this case, we set old_log_position to 0
old_log_position=0
fi
# Calculate number of log lines
log_lines=`expr $new_log_position - $old_log_position`
# Get lines and analyse them a bit, do the counting
mail_sent=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep 'status=sent' | grep -v 'relay=127.0.0.1' | wc -l)
mail_deferred=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep 'status=deferred' | wc -l)
mail_bounced_total=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep 'status=bounced' | wc -l)
mail_bounced_spam=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep 'status=bounced' | grep -i 'spam' | wc -l)
mail_reputation=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep -i 'poor reputation' | wc -l)
# Report counter results
rrdtool update $RRDFILE \
-t log_lines:mail_sent:mail_deferred:mail_bounced_total:mail_bounced_spam:mail_reputation \
$db_last_modified:$log_lines:$mail_sent:$mail_deferred:$mail_bounced_total:$mail_bounced_spam:$mail_reputation
# That's it.
exit 0