跳到主要內容

Postfix mailbox quota check

因為敝公司近幾年來新人都用WebMail,又因為OpenWebMail只能用mailbox,因此偶爾會發生用戶mailbox爆掉的情形。
搜尋後並沒找到Postfix內建有警告功能,網路上只有Postfix Checking Maildir Disk Usage,是駱駝文寫的,我不會改 =.=

最後決定自己動手做,雖然不會大蟒蛇還是勉強寫出個小程式,用Crontab排程每天檢查一次,再自動通知用戶。

#!/usr/bin/env python
#coding: utf-8
import os
import smtplib
from email.mime.text import MIMEText

def sendWarnMail(account, name):
    fromaddr="Email系統 <postmaster@example.com.tw>";
    toaddrs=name +" "+ "<"+account+"@example.com.tw>";
    msg =  MIMEText(name +" 您好:\n\t您的email容量即使用完畢, 請將伺服器上的郵件搬移一部份至其他郵件匣,以免無法收信,謝謝。", 'plain','utf-8');
    msg['Subject'] = "Email容量不足通知";
    msg['From'] = fromaddr;
    msg['To'] = toaddrs;
    server = smtplib.SMTP('localhost');
    #server.set_debuglevel(1);
    server.sendmail(fromaddr, toaddrs, msg.as_string());
    server.quit();
    return;
    
def getPostfixMailBoxLimit(config):
    f=open(config,'r');
    limit = 0;
    for line in f:
        line=line.lstrip();
        if line.startswith("mailbox_size_limit"):
            arrs = line.split('=');
            limit = int(arrs[1]);
            break;
    f.close()
    return limit;

#主程式
postfixconfig = "/usr/local/etc/postfix/main.cf";
mailboxdir = "/var/mail";
postfixlimit = getPostfixMailBoxLimit(postfixconfig);
#設定提醒上限減100MB
sizelimit = postfixlimit-100000000;
f = open('/etc/passwd', 'r');
for line in f:
    arrs = line.split(':');
    if not arrs[0].startswith('#'):
        mailbox = mailboxdir+"/"+arrs[0];
        if os.path.exists(mailbox):
            filesize = os.path.getsize(mailbox);
            if(filesize > sizelimit):
                sendWarnMail(arrs[0], arrs[4]);
f.close();

留言

這個網誌中的熱門文章

DBeaver 介面語言

DBeaver是我個人頗常用的一套跨平台Database管理工具,最近升級後發現Windows版本居然變成簡體中文,而且無法切換為英文。

自然人憑證讀卡機驅動程式

鳥毅用的是第一代的自然人憑證讀卡機,EZ100PU(後來有同事買EZmini可以讀SIM卡似乎更好),每年報稅時用一次。 本來只是要申請些政府業務,一時之間找不到光碟,沒想到在 驅動程式下載 居然看到Linux和Mac的驅動程式,剩下的就是政府單位的網頁和程式應該改版了吧!!!

如何將較高版本SQL Server複製到低版本SQL Server (降級為舊版)並保留權限及資料庫圖表

一般若是要將SQL Server裡的Database轉往其他Server時,最簡單的方式就是備份(Backup)後再還原(Restore),或者是䣃離(detach)後附加(attach)。 但是很不幸地,若是由較低版本(e.g. 2008)到較高版本(e.g. 2012)要怎麼辦呢?