cx_Oracle环境配置

export ORACLE_BASE=/u01/app/oracle

export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1

export LD_LIBRARY_PATH=$ORACLE_HOME/lib/

export TNS_ADMIN=$ORACLE_HOME/network/admin/

脚本正本

#!/usr/bin/python
# coding:utf-8 __author__ = 'Jipu FANG' import cx_Oracle
import sys # 主库状态
def pdb():
conn = cx_Oracle.connect('user/passwd@rac01/orcl')
cursor = conn.cursor ()
cursor.execute ("select switchover_status from v$database")
row = cursor.fetchone ()
status = row[0]
if status == 'TO STANDBY' or status == 'SESSIONS ACTIVE':
cursor.close ()
conn.close ()
return "0"
else:
return "200" # 备库状态
def sdb():
conn = cx_Oracle.connect('user/passwd@rdata01/phydb')
cursor = conn.cursor ()
cursor.execute ("select switchover_status from v$database")
row = cursor.fetchone ()
status = row[0]
if status == 'NOT ALLOWED':
cursor.close ()
conn.close ()
return "0"
else:
return "200" # DG延迟,基于SCN
class dg_delay():
def rac1_scn(self):
conn = cx_Oracle.connect('user/passwd@rac01/orcl')
cursor = conn.cursor ()
cursor.execute ("select current_scn from v$database")
row = cursor.fetchone ()
if row:
scn = row[0]
else:
scn = 0
cursor.close()
conn.close ()
return scn def sdb_scn(self):
conn = cx_Oracle.connect('user/passwd@rdata01/phydb')
cursor = conn.cursor ()
cursor.execute ("select current_scn from v$database")
row = cursor.fetchone ()
if row:
scn = row[0]
else:
scn = 0
cursor.close()
conn.close ()
return scn def run(self):
scn = int(self.rac1_scn()) - int(self.sdb_scn())
return scn item = sys.argv[1] if __name__ == '__main__':
if item == 'pdb':
print pdb()
elif item == 'sdb':
print sdb()
elif item == 'dg':
a = dg_delay()
print a.run()

脚本执行

# ./check_dg_delay_status.py dg
5
04-25 00:07