python脚本自动生成ORACLE AWR报告

#!/usr/bin/python

#coding=gbk

#2011-08-12

import os

import sys

import smtplib

import pickle

import mimetypes 

from email.MIMEText import MIMEText 

from email.MIMEImage import MIMEImage

from email.MIMEMultipart import MIMEMultipart

SMTP_SERVER='localhost'

#EMAIL_USER='root'

#EMAIL_PASSWD=''

EMAIL_SUBJECT='dg01.com.cn AWR Report'

FROM_USER='[email protected]'

TO_USERS=['[email protected]','[email protected]']

def createawr():

  pipe = os.popen(' su - oracle -c "/u01/oracle/product/10.2.0/db_1/bin/sqlplus /nolog @awrrpt.sql"')

  print 'create awr report is ok!'

def mysendmail(fromaddr,toaddrs,subject):

        COMMASPACE=','

        msg = MIMEMultipart() 

        msg['From'] = fromaddr

        msg['To'] = COMMASPACE.join(toaddrs)

        msg['Subject'] = subject

        txt = MIMEText("172.21.1.30 AWR Report, The report be send at 9 AM every day ") 

        msg.attach(txt) 

        fileName = r'/home/oracle/awr.html'

        ctype, encoding = mimetypes.guess_type(fileName) 

        if ctype is None or encoding is not None: 

            ctype = 'application/octet-stream'

        maintype, subtype = ctype.split('/', 1) 

        att = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype) 

        att.add_header('Content-Disposition', 'attachment', filename = fileName) 

        msg.attach(att) 

        server=smtplib.SMTP(SMTP_SERVER)

        #server.login(EMAIL_USER,EMAIL_PASSWD)

        server.sendmail(fromaddr,toaddrs,msg.as_string())

        server.quit()

if __name__=='__main__':

                createawr()

                mysendmail(FROM_USER, TO_USERS, EMAIL_SUBJECT)

                print 'send successful'


你可能感兴趣的:(oracle,数据库,python,服务器,运维)