1. #!/usr/bin/python  
  2. #coding=gbk  
  3. #2011-08-12  
  4. import os  
  5. import sys  
  6. import smtplib  
  7. import pickle  
  8. import mimetypes   
  9. from email.MIMEText import MIMEText   
  10. from email.MIMEImage import MIMEImage  
  11. from email.MIMEMultipart import MIMEMultipart  
  12. SMTP_SERVER='localhost' 
  13. #EMAIL_USER='root'  
  14. #EMAIL_PASSWD=''  
  15. EMAIL_SUBJECT='dg01.com.cn AWR Report' 
  16. FROM_USER='root@dg01.com.cn' 
  17. TO_USERS=['vcdog@126.com','kevin@hotmail.com']  
  18. def createawr():  
  19.   pipe = os.popen(' su - oracle -c "/u01/oracle/product/10.2.0/db_1/bin/sqlplus /nolog @awrrpt.sql"')  
  20.   print 'create awr report is ok!' 
  21. def mysendmail(fromaddr,toaddrs,subject):  
  22.         COMMASPACE=',' 
  23.         msg = MIMEMultipart()   
  24.         msg['From'] = fromaddr  
  25.         msg['To'] = COMMASPACE.join(toaddrs)  
  26.         msg['Subject'] = subject  
  27.         txt = MIMEText("172.21.1.30 AWR Report, The report be send at 9 AM every day ")   
  28.         msg.attach(txt)   
  29.         fileName = r'/home/oracle/awr.html' 
  30.         ctype, encoding = mimetypes.guess_type(fileName)   
  31.         if ctype is None or encoding is not None:   
  32.             ctype = 'application/octet-stream' 
  33.         maintype, subtype = ctype.split('/'1)   
  34.         att = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)   
  35.         att.add_header('Content-Disposition''p_w_upload', filename = fileName)   
  36.         msg.attach(att)   
  37.         server=smtplib.SMTP(SMTP_SERVER)  
  38.         #server.login(EMAIL_USER,EMAIL_PASSWD)  
  39.         server.sendmail(fromaddr,toaddrs,msg.as_string())  
  40.         server.quit()  
  41. if __name__=='__main__':  
  42.                 createawr()  
  43.                 mysendmail(FROM_USER, TO_USERS, EMAIL_SUBJECT)  
  44.                 print 'send successful'