如何监视PostFix配置变更并自动重载?

Postfix

1 前言

一个问题,一篇文章,一出故事。
PostFix的Access配置文件是该服务的访问控制列表,于是笔者想要简化他的修改操作,于是整理此文。

2 最佳实践

2.1 创建监视脚本

mkdir -p ~/scripts
vim ~/scripts/autoReloadPostFix.sh

添加如下脚本,

#!/bin/bash

monitorFiles="/etc/postfix/access"
monitorLog="/var/log/autoReloadPostFix.log"
monitorDate=`date +"%Y-%m-%d %H:%M:%S"`
serviceList="postfix.service"

if [ ! -f "$monitorLog" ]; then
        touch "$monitorLog"
fi
if [ ! -f "$monitorLog" ]; then
        echo "$monitorDate"' Failed to create log file!'
        exit 1
fi

for file in $monitorFiles; do
        if [ "$file" == "" ]; then
                echo "$monitorDate"' Variable monitorFiles cannot be empty!' | tee -a "$monitorLog"
                continue
        fi
done

for service in $serviceList; do
        if [ "$service" == "" ]; then
                echo "$monitorDate"' Variable serviceList cannot be empty!' | tee -a "$monitorLog"
                continue
        fi
        checkServer="0"
        checkServer=`/usr/bin/systemctl status "$service" | grep "Active:" | grep "running" | wc -l`
        if [ "$checkServer" != "1" ]; then
                echo "$monitorDate"' Service non-running, this script restarts service!' | tee -a "$monitorLog"
                echo "$monitorDate"' Restarting Service '"$service" | tee -a "$monitorLog"
                /usr/bin/systemctl restart "$service"
        fi
done

oldFileMd5=""
newFileMd5=""
reloadMark="0"
for file in $monitorFiles; do
        if [ "`grep "$file" "$monitorLog" | wc -l`" != "0" ]; then
                oldFileMd5=`grep "$file" "$monitorLog" | grep -v "Failed" | cut -d" " -f3 | tail -n 1`
        else
                echo "$monitorDate"' '`md5sum "$file"` >> $monitorLog
                continue
        fi
        newFileMd5=`md5sum "$file" | cut -d" " -f1`

        if [ "$oldFileMd5" == "" -o "$newFileMd5" == "" ]; then
                echo "$monitorDate"' Failed to get parameters'" $monitorFile" | tee -a "$monitorLog"
                continue
        fi
        if [ "$oldFileMd5" == "$newFileMd5" ]; then
                continue
        fi
        echo "$monitorDate"' '`md5sum "$file"` >> $monitorLog
        reloadMark="1"
done

if [ "$reloadMark" == "0" ]; then
        exit
fi

echo "$monitorDate"' Files updated, this script reload service!' | tee -a "$monitorLog"
for service in $serviceList; do
        if [ "$service" == "" ]; then
                echo "$monitorDate"' Service name cannot be empty!' | tee -a "$monitorLog"
                continue
        fi
        echo "$monitorDate"' Restarting Service '"$service" | tee -a "$monitorLog"
        /usr/sbin/postmap /etc/postfix/access
        /usr/bin/sleep 0.5
        /usr/bin/systemctl reload "$service"
done

2.2 创建脚本触发

crontab -e

加入如下配置,

*/2 * * * * sh ~/scripts/autoReloadPostFix.sh
没有评论

发表回复

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

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

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

1 前言 一个问题,一篇文章,一出故事。 笔者生产中有一套PostFix集群,最近经历了一次邮件延迟 …

Postfix
如何优化PostFix?

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