#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
CMDVER="1.1 (2015/03)"

infile="/var/log/fetchmail"

lines=500

[ -f $infile ] || exit 0

warning_message()
{
	local host=$1
	local date=$2
	local postmaster=$(kusa-conf contact.postmaster.email 2>/dev/null) || {
		postmaster="root"
	}
	local uname=$(uname -n)
	local subj="WARNING fetchmail SSL fail ($uname): $host $date"

	echo -e "$subj\n---\n  $CMD $CMDVER" | mail -s "$subj" $postmaster
}


# (MAIN)


# fetchmail: 6.3.9-rc2 querying mail.studior.biz (protocol POP3) at Thu Feb 27 15:18:39 2014: poll started
# 4995:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed:s3_clnt.c:983:

tail -$lines $infile | while read line
do
  case "$line" in
    *querying*' 'at' '*)
    	date=$(echo "X$line" | sed -e 's/.* at //' -e 's/: poll started$//')
	host=$(echo "X$line" | sed -e 's/.* querying //' -e 's/ .*//')
	;;
    *certificate' 'verify' 'failed*)
    	warning_message "$host" "$date"
	exit 0
	;;
  esac
done

exit 0
