标签:%s utf-8 word imp cat lse user 告警系统邮件引擎 encode
一、告警系统邮件引擎1?在mail目录下创建mail.py文件,
文件内容如下:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
gserver = ‘smtp.163.com‘
gport = 25
try:
msg = MIMEText(unicode(content).encode(‘utf-8‘))
msg[‘from‘] = mailfrom
msg[‘to‘] = mailto
msg[‘Reply-To‘] = mailfrom
msg[‘Subject‘] = subject
smtp = smtplib.SMTP(gserver, gport)
smtp.set_debuglevel(0)
smtp.ehlo()
smtp.login(username,password)
smtp.sendmail(mailfrom, mailto, msg.as_string())
smtp.close()
except Exception,err:
print "Send mail failed. Error: %s" % err
def main():
to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
##定义QQ邮箱的账号和密码)
sendqqmail(‘riven@163.com‘,‘a123456‘,‘riven@163.com‘,to,subject,content)
if __name__ == "__main__":
main()
首先定义好脚本中的邮箱账号和密码, 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
2、在mail目录下创建脚本mail.sh ,当系统服务异常的时候就会调用mail.sh
内容如下:
log=$1
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
echo $t_s2 > /tmp/$log
fi
t_s2=`tail -1 /tmp/$log|awk ‘{print $1}‘`
echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2]
echo $v
if [ $v -gt 3600 ]
then
./mail.py $1 $2 $3
echo "0" > /tmp/$log.txt
else
if [ ! -f /tmp/$log.txt ]
then
echo "0" > /tmp/$log.txt
fi
nu=`cat /tmp/$log.txt`
nu2=$[$nu+1]
echo $nu2>/tmp/$log.txt
if [ $nu2 -gt 10 ]
then
./mail.py $1 "trouble continue 10 min $2" "$3"
echo "0" > /tmp/$log.txt
fi
fi
1?暂时关闭掉502
vim /usr/local/bin/mon/conf/mon.conf #0为关闭,1为打开
2?暂时注释掉main.sh脚本中日志行:
vim /usr/local/bin/mon/bin/main.sh
3?执行main.sh
sh -x /usr/local/bin/mon/bin/main.sh
标签:%s utf-8 word imp cat lse user 告警系统邮件引擎 encode
原文地址:http://blog.51cto.com/10690709/2147006