前一阵花了点时间学习python,近段时间完成了一个监控服务器基本信息的项目,都是为了满足大家监控的欲望,特殊日志并报警的分布式系统,单台服务器采集粒度为1次/1分钟,一天大约1440条,目前监控了20多台服务器,一天大约31680条日志,现在单点监控中心服务器在性能上还绰绰有余,有更多的服务器来测试就好了,估计可以支持到100台以上服务器监控的级别。
现在遇到一个需求是发现报警时实时发送消息给相关人员,由于公司短信网关只买了上海电信用户没有上海电信的号码,汗一个,只好通过发邮件来实施。
支持发送GB18030编码的文本内容,任意编码附件,可以做出适当修改支持群发。
 
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
 
 
    -   
-   
- import os   
- import sys   
- from smtplib import SMTP   
- from email.MIMEMultipart import MIMEMultipart   
- from email.mime.application import MIMEApplication   
- from email.MIMEText import MIMEText   
- from email.MIMEBase import MIMEBase   
- from email import Utils,Encoders   
- import mimetypes   
- import time   
-   
- STMP_SERVER = "mail.×××.com"  
- STMP_PORT = "25"  
- USERNAME = "×××@×××.com"  
- USERPASSWORD = "×××"  
- FROM = "MonitorCenterWarning@×××.com"  
- TO = "×××@gmail.com"  
-   
- def sendFildByMail(config):   
-     print 'Preparing...'  
-     message = MIMEMultipart( )   
-     message['from'] = config['from']   
-     message['to'] = config['to']   
-     message['Reply-To'] = config['from']   
-     message['Subject'] = config['subject']   
-     message['Date'] = time.ctime(time.time())   
-     message['X-Priority'] =  '3'  
-     message['X-MSMail-Priority'] =  'Normal'  
-     message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180'  
-     message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180'  
-        
-     if 'file' in config:   
-           
-         f=open(config['file'], 'rb')   
-         file = MIMEApplication(f.read())   
-         f.close()   
-         file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))   
-         message.attach(file)   
-        
-     if 'content' in config:   
-           
-         f=open(config['content'], 'rb')   
-         f.seek(0)   
-         content = f.read()   
-         body = MIMEText(content, 'base64', 'gb2312')   
-         message.attach(body)   
-   
-     print 'OKay'  
-     print 'Logging...'  
-     smtp = SMTP(config['server'], config['port'])   
-       
-     smtp.login(config['username'], config['password'])   
-     print 'OK'  
-        
-     print 'Sending...',   
-     smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())   
-     print 'OK'  
-     smtp.close()   
-     time.sleep(1)   
-   
- if __name__ == "__main__":   
-     if len(sys.argv) < 2:   
-         print 'Usage: python %s contentfilename' % os.path.basename(sys.argv[0])   
-         print 'OR Usage: python %s contentfilename attachfilename' % os.path.basename(sys.argv[0])   
-         wait=raw_input("quit.")   
-         sys.exit(-1)   
-     elif len(sys.argv) == 2:   
-         sendFildByMail({   
-             'from': FROM,   
-             'to': TO,   
-             'subject': '[MonitorCenter]Send Msg %s' % sys.argv[1],   
-             'content': sys.argv[1],   
-             'server': STMP_SERVER,   
-             'port': STMP_PORT,   
-             'username': USERNAME,   
-             'password': USERPASSWORD})   
-     elif len(sys.argv) == 3:   
-         sendFildByMail({   
-             'from': FROM,   
-             'to': TO,   
-             'subject': '[MonitorCenter]Send Msg and File %s %s' % (sys.argv[1], sys.argv[2]),   
-             'content': sys.argv[1],   
-             'file': sys.argv[2],   
-             'server': STMP_SERVER,   
-             'port': STMP_PORT,   
-             'username': USERNAME,   
-             'password': USERPASSWORD})   
-     wait=raw_input("end.")  
 
 
windows xp下:

 linux ubuntu,suse下:

收到的结果:
