如何实现监视PostFix的延迟邮件警告?

Postfix

1 前言

一个问题,一篇文章,一出故事。
笔者生产中有一套PostFix集群,最近经历了一次邮件延迟事件,因此我们需要实现监视PostFix如果有延迟邮件则及时通过Tigase的API发出警告消息。
如果你需要了解该集群的搭建,请参阅如下章节,

如何使用HAProxy配置PostFix集群?

2 最佳实践

2.1 创建备份脚本

mkdir ~/scripts/
vim ~/scripts/monitorPostfixDeferred.sh

加入如下配置,

#!/bin/bash

warningThreshold="30"
alertInterval="600"
tigaseAppID="10086"
tigaseAppPassword="*********"
tigaseEmployeeID="will,jeff"
tigaseApiURL="https://tigase.cmdschool.org:8092/api/pushText"
logFile="/var/log/monitorPostfixDeferred.log"

deferredMailCount=$(find /var/spool/postfix/deferred -type f | wc -l)
if [ "$warningThreshold" -gt "$deferredMailCount" ]; then
        exit 0
fi

if [ -f "$logFile" ]; then
        logWarningCount=$(egrep "Delayed mail queue .* is too big" "$logFile" | wc -l)
fi

if [ "$logWarningCount" -ne "0" ]; then
        if [ -f "$logFile" ]; then
                logTime=$(egrep "Delayed mail queue .* is too big" "$logFile" | tail -n 1 | awk -F ' ' '{print $1" "$2}')
        fi
        date -d "$logTime" +'%Y-%m-%d %H:%M:%S' &> /dev/null
        if [ "$?" -ne "0" ]; then
                echo `date +'%Y-%m-%d %H:%M:%S'`' "The obtained date format is incorrect"' >> "$logFile"
                exit 1
        fi
        lastTime=$(date -d "$logTime" +%s)
        currentTime=$(date +%s)
        timeDifference=$((currentTime - lastTime))
        if [ "$timeDifference" -lt "$alertInterval" ]; then
                exit 0
        fi
fi

tigaseMessageBody="`hostname -s`:Delayed mail queue $deferredMailCount is too big%0d%0aEvent date:`date +%Y.%m.%d`"
curlData="appId=$tigaseAppID&password=$tigaseAppPassword&employee_id=$tigaseEmployeeID&body=$tigaseMessageBody"
logMessageBody=`date +'%Y-%m-%d %H:%M:%S'`' "Delayed mail queue '$deferredMailCount' is too big"'
curl -X POST -d "$curlData" "$tigaseApiURL" &> /dev/null
echo "$logMessageBody" >> "$logFile"
exit 0

– 参数“warningThreshold”定义延迟的邮件数量,范例为”30″延迟邮件
– 参数“alertInterval”定义警告消息的间隔,范例为”600″秒,即10分钟
– 参数“tigaseAppID”定义消息软件的应用号ID,范例为”10086″
– 参数“tigaseAppPassword”定义消息软件的应用号密码,范例为”*********”
– 参数“tigaseEmployeeID”定义消息软件中的员工工号,范例为”will”和“jeff”,使用逗号分隔
– 参数“tigaseApiURL”定义消息软件的接口地址,范例为”https://tigase.cmdschool.org:8092/api/pushText”
– 参数“logFile”定义脚本日志的路径,范例为”/var/log/monitorPostfixDeferred.log”

2.2 创建脚本触发

crontab -e

加入如下配置,

*/1 * * * * sh ~/scripts/monitorPostfixDeferred.sh

基于以上脚本运行后,如果符合条件,消息软件将收到如下警告消息,

postfix01:Delayed mail queue 980 is too big
Event date:2024.03.29
没有评论

发表回复

Postfix
如何指定PostFix的默认下一跳地址?

1 前言 一个问题,一篇文章,一出故事。 笔者想要指定邮件的默认下一跳地址,以便邮件可以被送到特定的 …

Postfix
如何优化PostFix?

1 前言 一个问题,一篇文章,一出故事。 笔者最近生产服务器遇到队列问题,因此需要通过优化PostF …

Postfix
如何优化PostFix邮件队列?

1 前言 一个问题,一篇文章,一出故事。 笔者最近生产服务器遇到队列问题,因此需要通过优化PostF …