#!/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`

DBFILE="current-logline.db"

# Load configuration
. ./settings.sh


### COUNTERS ###
log_lines_total=0
log_lines_smtp=0
log_lines_smtpd=0

mo_sent=0
mo_deferred=0
mo_bncd_total=0
mo_bncd_spam=0
mo_bncd_reputation=0
mo_deliverable=0
mo_undeliverable=0

mi_accept=0
mi_reject=0
mi_proxy_accept=0
mi_proxy_reject=0

mi_connects=0
mi_connects_tls=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
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

# Collect number of log lines
log_lines_total=`expr $new_log_position - $old_log_position`
log_lines_smtp=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | wc -l)
log_lines_smtpd=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | wc -l)

# Get lines and analyse them a bit, do the counting

# Collect outbound email statistics - localhost is excluded.
mo_sent=$(              sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=sent'          | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_deferred=$(          sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=deferred'      | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_deliverable=$(       sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=deliverable'   | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_undeliverable=$(     sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=undeliverable' | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_bncd_total=$(        sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=bounced'       | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_bncd_spam=$(         sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep 'status=bounced' | grep -i 'spam' | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)
mo_bncd_reputation=$(   sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtp\[' | grep -i 'reputation'        | grep -v 'relay=127\.0\.0\.1\|relay=localhost\|relay=::1' | wc -l)

# Collect inbound email statistics
mi_accept=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep '\/smtpd\[' | grep ': [0-9A-F]*: client=' | wc -l)
mi_reject=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep ': NOQUEUE: reject:' | wc -l)
mi_proxy_accept=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep ': proxy-accept: END-OF-MESSAGE: ' | wc -l)
mi_proxy_reject=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep ': proxy-reject: END-OF-MESSAGE: ' | wc -l)

# Collect connection data
mi_connects=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep ': connect from' | wc -l)
mi_connects_tls=$(sed -n "$old_log_position","$new_log_position"p $LOGFILE | grep '\/smtpd\[' | grep 'Anonymous TLS connection established from' | wc -l)


# Report counter results
echo "P postfixdetails sent=$mo_sent|deferred=$mo_deferred|deliverable=$mo_deliverable|undeliverable=$mo_undeliverable|bounced=$mo_bncd_total|bounced-spam=$mo_bncd_spam|bounced-reputation=$mo_bncd_reputation|accepted=$mi_accept|proxy-accepted=$mi_proxy_accept|rejected=$mi_reject|proxy-rejected=$mi_proxy_reject|connections=$mi_connects|tls-connections=$mi_connects_tls Additional metrics related to postfix"

# That's it.
exit 0