ITPub博客

首页 > 数据库 > Oracle > oracle数据库自动巡检(三)python脚本

oracle数据库自动巡检(三)python脚本

Oracle 作者:robotshen 时间:2014-03-14 10:48:37 0 删除 编辑

1、  读数据库服务配置文件

配置文件conf/config.xml

 

       

                ORACLE-A

                192.168.21.XX

                192.168.21.XX

                ABC

                XXXX

                22

                $

                /home/XXX/XXX/checkcli.sh

                #数据库服务器巡检脚本存放位置

       

 

Python脚本

 

        import ElementTree as ET

    root = ET.parse(proConf).getroot()

    allservice = root.findall('ServerInfo')

    for server in allservice:

        sIP = server.find('IP').text

        sName = server.find('Name').text

        sFIP = server.find('FIP').text

        sPort = server.find('Port').text

        sUser = server.find('UserName').text

        #sPwd = base64.b64decode(server.find('PassWord').text)

        sPwd = server.find('PassWord').text

        sPoi = server.find('Pointing').text

            sCom = server.find('Commands').text

1、  写文本文件(日志、巡检结果)

def WriteLog(filename,tmpstr):

    time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

    logstr = str(time_str) + str(tmpstr) + ' '

    f = open(filename,"a")

    f.write(logstr)

    f.close()

def ReWriteLog(filename,tmpstr): #用写模式找开文件

    time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

    logstr = str(tmpstr) + ' '

    f = open(filename,"w")

    f.write(logstr)

        f.close()

2、  通过SSH联接数据库服务器执行巡检脚本并将结果存成XML文件

def SSH(host,user,pwd,coms,outprintFile,logfile):

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    print host+' '

    print user+' '

    print pwd

    print "ssh.connect(hostname=%s,username=%s,password=%s)"%(host,user,pwd)

    print coms

    try:

        ssh.connect(hostname=host,username=user,password=pwd)

    except:

        WriteLog(logfile," SSH2 connect "+host +" error.")

    f = open(outprintFile,"a")

    stdin,stdout,stderr = ssh.exec_command(coms)

    #print stdin + " "

    #print stdout.read() + " "

    #print stderr + " "

    f.write(stdout.read())

    #f.write(stdin.read())

    f.write(stderr.read())

    f.close()

        ssh.close()

3、  解析XML文件,生成EXECL

4、  发送报告邮件

def getMailConfig(configFile):

    coms=[]

    Config = ConfigParser.ConfigParser()

    Config.read(configFile)

    host = Config.get("FROM","HOST")

    user = Config.get("FROM","USERNAME")

    subject = Config.get("FROM","SUBJECT")

    pwd = base64.b64decode(Config.get("FROM","PASSWORD"))

    commands = Config.items("TO")

    for com in commands:

        coms.append(com[1])

    return host,user,pwd,subject,coms

def SendMail(configFile,sendfile,logFile,content):

    host,user,pwd,subject,coms=getMailConfig(configFile)

    _tos=""

    for _to in coms:

        _tos = _tos + _to + ";"

    msg = MIMEMultipart()

       # att = MIMEText("test mail")

    att = MIMEText(open(sendfile,'rb').read(),'base64','gb2312')

   att["Content-Type"] ='application/octet-stream'

    att["Content-Disposition"]='attachment;filename="'+sendfile+'"'

      msg.attach(att)

      msg['to']=coms

    msg['from']=user

    msg['to']=_tos

    msg['subject']=subject+" "+ time.strftime("%Y-%m-%d",time.localtime())

    msg.attach(MIMEText(content,'html','gb2312'))   

    msg.attach(att)

    try:

        smtp_svr=smtplib.SMTP()

        smtp_svr.connect(host,"25")

    except (smtplib.SMTPConnectError,socket.error):

        print "connect error"

        WriteLog(logFile," SendMail connect error")

    try:

        smtp_svr.login(user,pwd)

    except smtplib.SMTPAuthenticationError:

        print "authentication error "

        WriteLog(logFile," SendMail authentication error")

    try:

        smtp_svr.sendmail(user,coms,msg.as_string())

    except (smtplib.SMTPRecipientsRefused,smtplib.SMTPDataError,smtplib.SMTPServerDisconnected):

        print "send mail error"

        WriteLog(logFile," SendMail send mail error")

    smtp_svr.close

def SendMailForHtml(configFile,tableContent,mailtitle,logFile):

    host,user,pwd,subject,coms=getMailConfig(configFile)

    _tos=""

    for _to in coms:

        _tos = _tos + _to + ";"

    #

   smtp_server = smtplib.SMTP(smtp_host) 

    msg = MIMEMultipart()

    html='

'+tableContent+'

    msg = MIMEText(html, 'html') 

    msg['from']=user

    msg['to']=_tos

    msg['subject']=subject + " " + mailtitle

    try:

        smtp_svr=smtplib.SMTP()

        smtp_svr.connect(host,"25")

    except (smtplib.SMTPConnectError,socket.error):

        print "connect error"

        WriteLog(logFile," SendMailForHtml connect error")

    try:

        smtp_svr.login(user,pwd)

    except smtplib.SMTPAuthenticationError:

        print "authentication error "

        WriteLog(logFile," SendMailForHtml authentication error")

    try:

        smtp_svr.sendmail(user,coms,msg.as_string())

    except (smtplib.SMTPRecipientsRefused,smtplib.SMTPDataError,smtplib.SMTPServerDisconnected):

        print "send mail error"

        WriteLog(logFile," SendMailForHtml send mail error")

    smtp_svr.close

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220222/viewspace-1117336/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论