1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import os from loguru import logger import datetime import sqlite3 import resultEmail
FileLog = 'log{}.log'.format(datetime.date.today()) logger.add(FileLog,rotation="500MB", encoding="utf-8", enqueue=True)
def getYesterday(): today = datetime.date.today() oneday = datetime.timedelta(days=1) yesterday = today - oneday return yesterday.strftime(r'%Y-%m-%d')
def QuerySystemInfo(yesterday): conn = sqlite3.connect(r'D:\sqlite\********') c = conn.cursor() querySql = c.execute("select * from bd_dbsysteminfo order by email") logger.info('#######待巡检数据库清单查询完成#######') for row in querySql: systemnameList.append(row[1]) systemtypeList.append(row[3]) conn.close()
def CmdQuery(systemname,systemtype,yesterday): logger.info('#######数据库备份巡检#######') if systemtype == 'Oracle': Query = r'cd C:\Program Files\Veritas\NetBackup\bin\ && bplist -C {} -S nbu5230 -s {} -e {} -t 4 -l -R /'.format(systemname,yesterday,yesterday) elif systemtype == 'SqlServer': Query = r'cd C:\Program Files\Veritas\NetBackup\bin\ && bplist -C {} -S nbu5230 -s {} -e {} -t 15 -l -R /'.format(systemname,yesterday,yesterday) else : logger.error('+++++++数据库{},数据库类型{}有误+++++++'.format(systemname,systemtype)) details = os.popen(Query).read() line = details.split('\n') if 'EXIT STATUS 227' in details: logger.error('+++++++数据库{},数据库类型{}在{}未查到备份文件+++++++'.format(systemname,systemtype,yesterday)) InsertDatabase(systemname,yesterday,details,'无备份') elif ('-rw---' in details) or len(line)>=5 : logger.info('#######数据库{},数据库类型{}在{}存在备份文件#######'.format(systemname,systemtype,yesterday)) InsertDatabase(systemname,yesterday,details,'正常') else : logger.error('+++++++数据库{},数据库类型{}在{}查询结果异常+++++++'.format(systemname,systemtype,yesterday)) InsertDatabase(systemname,yesterday,details,'无备份')
def InsertDatabase(systemname,querydate,details,result): insertContent = (systemname,querydate,details,result) conn = sqlite3.connect(r'D:\sqlite\JicAiDatabase') c = conn.cursor() querySql = c.execute("insert into system_nbubakupinfo (systemname,querydate,details,result) values (?,?,?,?)",insertContent) conn.commit() conn.close() logger.info('#######{}日,服务器{}的NBU备份日志已插入数据库#######'.format(querydate,systemname))
if __name__ == '__main__': yesterday = getYesterday() systemnameList = [] systemtypeList = [] logger.info('#######巡检开始#######') QuerySystemInfo(yesterday) for i in range(0,len(systemnameList)): CmdQuery(systemnameList[i],systemtypeList[i],yesterday) logger.info('***开始发送邮件***') resultEmail.sendEmail(yesterday) logger.info('***邮件已发给各系统管理员***') resultEmail.toManager(yesterday) logger.info('***邮件已发给NBU管理员***') logger.info('***邮件发送完成***') logger.info('#######巡检结束#######')
|