首页 > 脚本编程 > SMTP进程监控
您的足迹
  • 你没有浏览过任何文章或者你没有开启cookies。

SMTP进程监控

公司用的邮件服务器SMTP有时候会诡异的死去,最郁闷 的软件本身还认为自己是活,不会探测到去重新启动。话说这个进程非常有意思,用nc -v -z ip 25探测是succeed,但是telnet会失败,起初用telnet写脚本会遇到telnet无法退出的情况,后来发现这样是可以的:

echo "mail.sheriy.com 25"|xargs telnet

当然后来也没用这样的方法,后来想了下用python模拟SMTP每2分钟发信,如果失败重启SMTP服务。
Python的脚本如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#document by yinxuexiang

import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
import os

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):
    strFrom = fromAdd
    strTo = ', '.join(toAdd)

    server = authInfo.get('server')
    user = authInfo.get('user')
    passwd = authInfo.get('password')
    if not (server and user and passwd) :
        print 'incomplete login info, exit now'
        return
    # 设定root信息
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    # Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    #设定纯文本信息
    msgText = MIMEText(plainText, 'plain', 'utf-8')
    msgAlternative.attach(msgText)

    #设定HTML信息
    msgText = MIMEText(htmlText, 'html', 'utf-8')
    msgAlternative.attach(msgText)

    #发送邮件
    smtp = smtplib.SMTP()
    #设定调试级别,依情况而定
#        smtp.set_debuglevel(1)
    smtp.connect(server)
    smtp.login(user, passwd)
    smtp.sendmail(strFrom, strTo, msgRoot.as_string())
    smtp.quit()
    return

if __name__ == '__main__' :
    try :
        authInfo = {}
        authInfo['server'] = 'mail.sheriy.com'
        authInfo['user'] = 'admin@sheriy.com'
        authInfo['password'] = '123456'
        fromAdd = 'admin@sheriy.com
        toAdd = ['monitor@made-in-china.com']
        subject = '监控测试邮件'
        plainText = '发送邮件正常'
        htmlText = 'HTML文本'
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)
    except :
        os.system("/bin/bash /root/tool/restart_smtp.sh")

SMTP重启脚本如下:

#!/bin/bash
/opt/icewarp/icewarpd.sh --stop smtp
#pidof smtp
if ps axu |grep smtp |grep -v grep
then
  kill -9 $(ps axu |grep smtp |grep -v grep |awk -F ' ' '{print $2}')
  /opt/icewarp/icewarpd.sh --start smtp
else
  /opt/icewarp/icewarpd.sh --start smtp
fi
echo $(date +%F\ %T) "sheriy.com smtp restart  successfully" >>smtp.log

看起来效果还不错,至今运行稳定 。


相关博文

  1. 还没有评论
评论提交中, 请稍候...

留言

可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks
Feed