File: //usr/lib/python2.7/site-packages/lap/check-oracle-locks.py
import cx_Oracle
import os
import datetime
from time import localtime, strftime, strptime
import ConfigParser
# check-oracle-locksm.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')
lock_critical = cf.getint('oracle', 'lock_critical')
lock_warning = cf.getint('oracle', 'lock_warning')
instances = cf.get('oracle','instances')
os.environ['ORACLE_HOME'] = oracle_home
try:
instances = instances.split();
check_message = ''
has_critical = 0
has_warning = 0
for instance in instances:
os.environ['ORACLE_SID'] = instance
conn = cx_Oracle.connect(user, password, mode=cx_Oracle.SYSDBA)
cursor = conn.cursor()
cursor.execute("""SELECT count(1) FROM gv$session a , gv$session b WHERE a.BLOCKING_SESSION = b.sid AND a.blocking_instance = b.inst_id""")
result = cursor.fetchall()
rowcount = cursor.rowcount
cursor.close()
for row in result:
locks_count = row[0]
# Se locks_count >= entao alarma critico
if locks_count >= lock_critical :
check_message = check_message + '(!!) Number of locks in instance: ' + instance + ': ' + locks_count
has_critical = has_critical + 1
# Se locks_count >= lock_warning entao alarma warning
elif locks_count >= lock_warning:
check_message = check_message + '(!) Number of locks in instance: ' + instance + ': ' + locks_count
has_warning= has_warning + 1
else:
check_message = check_message + 'Nenhum lock encontrado na instance: ' + instance + ' '
if has_critical <> 0:
return [2, str(check_message) ]
elif has_warning <> 0:
return [1, str(check_message) ]
else:
return [0, str(check_message) ]
except Exception, e:
return [2, '{0}'.format(str(e))]