学着站在巨人的肩膀上

金融数学,InformationSearch,Compiler,OS,

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  12 随笔 :: 0 文章 :: 8 评论 :: 0 Trackbacks

前一阵花了点时间学习python,近段时间完成了一个监控服务器基本信息的项目,都是为了满足大家监控的欲望,特殊日志并报警的分布式系统,单台服务器采集粒度为1次/1分钟,一天大约1440条,目前监控了20多台服务器,一天大约31680条日志,现在单点监控中心服务器在性能上还绰绰有余,有更多的服务器来测试就好了,估计可以支持到100台以上服务器监控的级别。

现在遇到一个需求是发现报警时实时发送消息给相关人员,由于公司短信网关只买了上海电信用户没有上海电信的号码,汗一个,只好通过发邮件来实施。

支持发送GB18030编码的文本内容,任意编码附件,可以做出适当修改支持群发。

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #coding=utf-8   
  2. #!/usr/lib/python2.5/bin/python   
  3. import os   
  4. import sys   
  5. from smtplib import SMTP   
  6. from email.MIMEMultipart import MIMEMultipart   
  7. from email.mime.application import MIMEApplication   
  8. from email.MIMEText import MIMEText   
  9. from email.MIMEBase import MIMEBase   
  10. from email import Utils,Encoders   
  11. import mimetypes   
  12. import time   
  13.   
  14. STMP_SERVER = "mail.×××.com"  
  15. STMP_PORT = "25"  
  16. USERNAME = "×××@×××.com"  
  17. USERPASSWORD = "×××"  
  18. FROM = "MonitorCenterWarning@×××.com"  
  19. TO = "×××@gmail.com"  
  20.   
  21. def sendFildByMail(config):   
  22.     print 'Preparing...'  
  23.     message = MIMEMultipart( )   
  24.     message['from'] = config['from']   
  25.     message['to'] = config['to']   
  26.     message['Reply-To'] = config['from']   
  27.     message['Subject'] = config['subject']   
  28.     message['Date'] = time.ctime(time.time())   
  29.     message['X-Priority'] =  '3'  
  30.     message['X-MSMail-Priority'] =  'Normal'  
  31.     message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180'  
  32.     message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180'  
  33.        
  34.     if 'file' in config:   
  35.         #添加附件   
  36.         f=open(config['file'], 'rb')   
  37.         file = MIMEApplication(f.read())   
  38.         f.close()   
  39.         file.add_header('Content-Disposition''attachment', filename= os.path.basename(config['file']))   
  40.         message.attach(file)   
  41.        
  42.     if 'content' in config:   
  43.         #添加文本内容   
  44.         f=open(config['content'], 'rb')   
  45.         f.seek(0)   
  46.         content = f.read()   
  47.         body = MIMEText(content, 'base64''gb2312')   
  48.         message.attach(body)   
  49.   
  50.     print 'OKay'  
  51.     print 'Logging...'  
  52.     smtp = SMTP(config['server'], config['port'])   
  53.     #如果SMTP服务器发邮件时不需要验证登录则对下面这行加上注释   
  54.     smtp.login(config['username'], config['password'])   
  55.     print 'OK'  
  56.        
  57.     print 'Sending...',   
  58.     smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())   
  59.     print 'OK'  
  60.     smtp.close()   
  61.     time.sleep(1)   
  62.   
  63. if __name__ == "__main__":   
  64.     if len(sys.argv) < 2:   
  65.         print 'Usage: python %s contentfilename' % os.path.basename(sys.argv[0])   
  66.         print 'OR Usage: python %s contentfilename attachfilename' % os.path.basename(sys.argv[0])   
  67.         wait=raw_input("quit.")   
  68.         sys.exit(-1)   
  69.     elif len(sys.argv) == 2:   
  70.         sendFildByMail({   
  71.             'from': FROM,   
  72.             'to': TO,   
  73.             'subject''[MonitorCenter]Send Msg %s' % sys.argv[1],   
  74.             'content': sys.argv[1],   
  75.             'server': STMP_SERVER,   
  76.             'port': STMP_PORT,   
  77.             'username': USERNAME,   
  78.             'password': USERPASSWORD})   
  79.     elif len(sys.argv) == 3:   
  80.         sendFildByMail({   
  81.             'from': FROM,   
  82.             'to': TO,   
  83.             'subject''[MonitorCenter]Send Msg and File %s %s' % (sys.argv[1], sys.argv[2]),   
  84.             'content': sys.argv[1],   
  85.             'file': sys.argv[2],   
  86.             'server': STMP_SERVER,   
  87.             'port': STMP_PORT,   
  88.             'username': USERNAME,   
  89.             'password': USERPASSWORD})   
  90.     wait=raw_input("end.")  

 

windows xp下:

例子

 linux ubuntu,suse下:

1

收到的结果:

2

posted on 2010-03-15 19:24 学者站在巨人的肩膀上 阅读(651) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理