如何监视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,于是笔者需要熟悉Po …

Postfix
如何监视PostFix配置变更并自动同步?

1 前言 一个问题,一篇文章,一出故事。 PostFix的Access配置文件是该服务的访问控制列表 …

Postfix
如何修改PostFix的queue_directory?

1 前言 一个问题,一篇文章,一出故事。 笔者想要修改PostFix的目录队列目录到数据分区,以便可 …