File: //usr/lib/python2.7/site-packages/lap/check-oracle-bkp-arch.py
import cx_Oracle
import os
import datetime
import time
from time import localtime, strftime, strptime
import ConfigParser
# check-oracle-bkp-arch.py
#
# to cx_Oracle work, you need:
# 1. set LIBRARY_PATH correctly
# 2. configure yaml file
# 3. echo /u01/app/oracle/product/11.2.0.3/db/lib > /etc/ld.so.conf.d/oracle.conf
# ldconfig
def __run__(params):
conn = None
cf = ConfigParser.ConfigParser()
config_file = params.get('config_file')
cf.readfp(open(config_file))
user = cf.get('oracle', 'user')
password = cf.get('oracle', 'passwd')
oracle_home = cf.get('oracle', 'oracle_home')
instances = cf.get('oracle','instances')
os.environ['ORACLE_HOME'] = oracle_home
bkp_arch_interval = cf.getint('rman', 'bkp_arch_interval')
last_bkp_limit = ''
try:
instances = instances.split();
check_message = ''
nerros = 0
sql = ''
for instance in instances:
os.environ['ORACLE_SID'] = instance
conn = cx_Oracle.connect(user, password, mode=cx_Oracle.SYSDBA)
cursor = conn.cursor()
last_bkp_limit = datetime.datetime.now()-datetime.timedelta(minutes=bkp_arch_interval)
last_bkp_limit2 = datetime.datetime.strftime(last_bkp_limit, "%d/%m/%Y - %H:%M")
last_bkp_limit = time.strptime(last_bkp_limit2,"%d/%m/%Y - %H:%M")
cursor.execute("""select max(j.start_time)
from V$RMAN_BACKUP_JOB_DETAILS j
where j.input_type ='ARCHIVELOG'
and j.status = 'COMPLETED'""")
result = cursor.fetchall()
rowcount = cursor.rowcount
cursor.close()
if rowcount <> 0:
for row in result:
last_executed_bkp = row[0]
last_executed_bkp2 = datetime.datetime.strftime(last_executed_bkp, "%d/%m/%Y - %H:%M")
last_executed_bkp = time.strptime(last_executed_bkp2,"%d/%m/%Y - %H:%M")
if last_executed_bkp < last_bkp_limit:
check_message = check_message + ' Last Archive backup for database : ' + instance + ' is not OK - Minimum expected : %s - Last executed : %s (!!)' % (last_bkp_limit2,last_executed_bkp2)
nerros += 1
elif last_executed_bkp > last_bkp_limit:
check_message = check_message + ' Last Archive backup for database : ' + instance + ' is OK - Minimum expected : %s - Last executed : %s ' % (last_bkp_limit2, last_executed_bkp2)
elif rowcount == 0:
check_message = check_message + ' (!!) Backup Archive not found for database : ' + instance + ' '
nerros += 1
if nerros <> 0:
return [2, str(check_message) ]
else:
return [0, 'All Archive Backups ran correctly : %s ' % (check_message) ]
except Exception, e:
return [2, '{0}'.format(str(e)) ]