From 1c120fd9444ae78f3fba6a5882dba2fb82d24e58 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 3 May 2018 19:58:08 +0200 Subject: [PATCH] Initial import --- collector.sh | 91 ++++++++++++++++++++++++++++++++++++++++ collector_runner.sh | 13 ++++++ grapher.sh | 100 ++++++++++++++++++++++++++++++++++++++++++++ settings.sh | 7 ++++ 4 files changed, 211 insertions(+) create mode 100755 collector.sh create mode 100755 collector_runner.sh create mode 100755 grapher.sh create mode 100755 settings.sh diff --git a/collector.sh b/collector.sh new file mode 100755 index 0000000..6e076ed --- /dev/null +++ b/collector.sh @@ -0,0 +1,91 @@ +#!/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 diff --git a/collector_runner.sh b/collector_runner.sh new file mode 100755 index 0000000..511b9f7 --- /dev/null +++ b/collector_runner.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Go into appropriate directory +cd `dirname $0` + +# Load settings +. ./settings.sh + +# Run the collector +while true; do + ./collector.sh; + sleep 30; +done diff --git a/grapher.sh b/grapher.sh new file mode 100755 index 0000000..b61ba8f --- /dev/null +++ b/grapher.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# Go into appropriate directory +cd `dirname $0` + +# Load settings +. ./settings.sh + +rrdtool graph $GRAPHDIR/hour.png \ + --start end-1h \ + --step 60 \ + --title "Mail statistics for `hostname -f`" \ + --vertical-label "mails/minute" \ + -h 200 -w 800 --slope-mode \ + DEF:log_lines=$RRDFILE:log_lines:AVERAGE \ + DEF:mail_sent=$RRDFILE:mail_sent:AVERAGE \ + DEF:mail_deferred=$RRDFILE:mail_deferred:AVERAGE \ + DEF:mail_bounced_total=$RRDFILE:mail_bounced_total:AVERAGE \ + DEF:mail_bounced_spam=$RRDFILE:mail_bounced_spam:AVERAGE \ + DEF:mail_reputation=$RRDFILE:mail_reputation:AVERAGE \ + AREA:mail_deferred#77777733: \ + LINE:mail_deferred#777777:'deferred': \ + AREA:mail_sent#00DD0055: \ + LINE:mail_sent#00DD00:'sent': \ + AREA:mail_bounced_total#0000FF77: \ + LINE:mail_bounced_total#0000FF:'bounced total': \ + AREA:mail_bounced_spam#FF0000AA: \ + LINE:mail_bounced_spam#FF0000:'bounced due to SPAM': \ + AREA:mail_reputation#FF00FFFF: \ + LINE:mail_reputation#FF00FFFF:'reputation event': + +rrdtool graph $GRAPHDIR/6hour.png \ + --start end-6h \ + --step 60 \ + --title "Mail statistics for `hostname -f`" \ + --vertical-label "mails/minute" \ + -h 200 -w 800 --slope-mode \ + DEF:log_lines=$RRDFILE:log_lines:AVERAGE \ + DEF:mail_sent=$RRDFILE:mail_sent:AVERAGE \ + DEF:mail_deferred=$RRDFILE:mail_deferred:AVERAGE \ + DEF:mail_bounced_total=$RRDFILE:mail_bounced_total:AVERAGE \ + DEF:mail_bounced_spam=$RRDFILE:mail_bounced_spam:AVERAGE \ + DEF:mail_reputation=$RRDFILE:mail_reputation:AVERAGE \ + AREA:mail_deferred#77777733: \ + LINE:mail_deferred#777777:'deferred': \ + AREA:mail_sent#00DD0055: \ + LINE:mail_sent#00DD00:'sent': \ + AREA:mail_bounced_total#0000FF77: \ + LINE:mail_bounced_total#0000FF:'bounced total': \ + AREA:mail_bounced_spam#FF0000AA: \ + LINE:mail_bounced_spam#FF0000:'bounced due to SPAM': \ + AREA:mail_reputation#FF00FFFF: \ + LINE:mail_reputation#FF00FFFF:'reputation event': + +rrdtool graph $GRAPHDIR/day.png \ + --start end-24h \ + --step 60 \ + --title "Mail statistics for `hostname -f`" \ + --vertical-label "mails/minute" \ + -h 200 -w 800 --slope-mode \ + DEF:log_lines=$RRDFILE:log_lines:AVERAGE \ + DEF:mail_sent=$RRDFILE:mail_sent:AVERAGE \ + DEF:mail_deferred=$RRDFILE:mail_deferred:AVERAGE \ + DEF:mail_bounced_total=$RRDFILE:mail_bounced_total:AVERAGE \ + DEF:mail_bounced_spam=$RRDFILE:mail_bounced_spam:AVERAGE \ + DEF:mail_reputation=$RRDFILE:mail_reputation:AVERAGE \ + AREA:mail_deferred#77777733: \ + LINE:mail_deferred#777777:'deferred': \ + AREA:mail_sent#00DD0055: \ + LINE:mail_sent#00DD00:'sent': \ + AREA:mail_bounced_total#0000FF77: \ + LINE:mail_bounced_total#0000FF:'bounced total': \ + AREA:mail_bounced_spam#FF0000AA: \ + LINE:mail_bounced_spam#FF0000:'bounced due to SPAM': \ + AREA:mail_reputation#FF00FFFF: \ + LINE:mail_reputation#FF00FFFF:'reputation event': + +rrdtool graph $GRAPHDIR/week.png \ + --start end-7d \ + --step 60 \ + --title "Mail statistics for `hostname -f`" \ + --vertical-label "mails/minute" \ + -h 200 -w 800 --slope-mode \ + DEF:log_lines=$RRDFILE:log_lines:AVERAGE \ + DEF:mail_sent=$RRDFILE:mail_sent:AVERAGE \ + DEF:mail_deferred=$RRDFILE:mail_deferred:AVERAGE \ + DEF:mail_bounced_total=$RRDFILE:mail_bounced_total:AVERAGE \ + DEF:mail_bounced_spam=$RRDFILE:mail_bounced_spam:AVERAGE \ + DEF:mail_reputation=$RRDFILE:mail_reputation:AVERAGE \ + AREA:mail_deferred#77777733: \ + LINE:mail_deferred#777777:'deferred': \ + AREA:mail_sent#00DD0055: \ + LINE:mail_sent#00DD00:'sent': \ + AREA:mail_bounced_total#0000FF77: \ + LINE:mail_bounced_total#0000FF:'bounced total': \ + AREA:mail_bounced_spam#FF0000AA: \ + LINE:mail_bounced_spam#FF0000:'bounced due to SPAM': \ + AREA:mail_reputation#FF00FFFF: \ + LINE:mail_reputation#FF00FFFF:'reputation event': + diff --git a/settings.sh b/settings.sh new file mode 100755 index 0000000..d94b5a2 --- /dev/null +++ b/settings.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +### Configuration ### +LOGFILE="/var/log/mail.info" +DBFILE="/root/mailstats.db" +RRDFILE="/root/mailstats.rrd" +GRAPHDIR="/var/www/html/stats"