- A: Use the modern
oracledbmodule in Thin mode (default), which does not require installing Oracle Instant Client.
- A: Use Python's built-in
getpassmodule or pull credentials dynamically from secure environment variables viaos.environ.
or
Step-by-Step Implementation
- Install Library: Run
pip install oracledb. - Import and Connect: Establish a secure connection pool or single connection using credentials.
- Execute Query: Fetch critical alerts from
V$ALERT_TYPESorGV$DIAG_ALERT_EXT. - Close Connection: Safely release database handles.
python
import oracledb
import sys
def check_exadata_alerts(user, password, dsn):
try:
connection = oracledb.connect(user=user, password=password, dsn=dsn)
cursor = connection.cursor()
sql = "SELECT MESSAGE_TEXT FROM GV$DIAG_ALERT_EXT WHERE ORIGINATING_TIMESTAMP > SYSDATE - 1 AND MESSAGE_TEXT LIKE '%ORA-%'"
cursor.execute(sql)
alerts = cursor.fetchall()
return alerts
except Exception as e:
print(f"Connection failed: {e}")
sys.exit(1)
finally:
cursor.close()
connection.close()
Test Cases
- Test Case 1 (Positive): Pass valid Exadata/Oracle DSN and credentials; assert return type is a
list. - Test Case 2 (Negative): Pass invalid password; assert exception handling catches
oracledb.DatabaseErrorand exits gracefully without crashing. - Test Case 3 (Boundary): Zero critical alerts found; assert function returns an empty list
[]instead ofNone.
Python for Exadata: Health Check Task Example
Step-by-Step Implementation
- Install Driver: Run
pip install oracledb. - Establish Connection: Connect to the database using Thin mode.
- Run Query: Query storage cell metrics or alert logs.
- Process Output: Fetch and parse results.
python
import oracledb
def check_exadata_cells(user, password, dsn):
connection = oracledb.connect(user=user, password=password, dsn=dsn)
cursor = connection.cursor()
# Query checking ASM diskgroup or cell status mock view
cursor.execute("SELECT CELL_NAME, STATUS FROM V$SMART_SCAN_METRIC")
results = cursor.fetchall()
cursor.close()
connection.close()
return results
Test Cases
- Positive Test Case: Valid credentials and active Exadata grid return a list of tuples with
ONLINEstatus. - Negative Test Case: Invalid DSN or incorrect credentials trigger a
oracledb.DatabaseError, which is caught via exception handling. - Boundary Test Case: Empty result set when no cells match criteria returns an empty list without throwing an index error.
No comments:
Post a Comment