Initial import

This commit is contained in:
Jan Philipp Timme 2018-05-03 19:58:08 +02:00
commit 1c120fd944
4 changed files with 211 additions and 0 deletions

91
collector.sh Executable file
View File

@ -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

13
collector_runner.sh Executable file
View File

@ -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

100
grapher.sh Executable file
View File

@ -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':

7
settings.sh Executable file
View File

@ -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"