Skip to content

Instantly share code, notes, and snippets.

@domgiles
Last active April 20, 2021 17:49
Show Gist options
  • Save domgiles/97a464daeeaaa95f9cbc91a1c1315b49 to your computer and use it in GitHub Desktop.
Save domgiles/97a464daeeaaa95f9cbc91a1c1315b49 to your computer and use it in GitHub Desktop.
Simple parse for Oracle Alert Log Files
from __future__ import print_function
import argparse
import os
import time
import humanize
import psutil
from colorama import Fore, Back
from colorama import Style, init
from dateutil.parser import parse
def terminal_width():
rows, columns = os.popen('stty size', 'r').read().split()
return columns
def print_date(text):
tw = terminal_width()
print(Fore.RED + Back.WHITE + Style.DIM + '{0: <{1}}'.format(text, tw))
def print_text(text):
tw = terminal_width()
if text.lower().startswith("warning"):
print(Fore.WHITE + Back.BLUE + '{0: <{1}}'.format(text.strip(), tw))
elif text.lower().startswith("error"):
print(Fore.WHITE + Back.RED + '{0: <{1}}'.format(text.strip(), tw))
print(Fore.BLUE + text, end='')
def print_footer(env):
tw = terminal_width()
text = "Oracle SID = {0: <10} Alert Log = {4: <20} CPU Load = {1: <8} Memory Usage = {2}/{3}".format(
env["ORACLE_SID"],
str(psutil.cpu_percent()) + '%',
str(psutil.virtual_memory()[1] / (1024 * 1024 * 1024)) + 'GB',
str(psutil.virtual_memory()[0] / (1024 * 1024 * 1024)) + 'GB',
os.path.split(env['ORACLE_ALERT_FILE'])[1],
Fore.GREEN,
Fore.RED
)
output = '{3}{2}{0: <{1}}\r'.format(text, tw, Fore.GREEN, Back.BLACK)
print(output, end='')
def guess_at_alert_file(sid, env_variables):
alert_file = os.path.join(env_variables["ORACLE_BASE"],
os.path.join("diag/rdbms",
os.path.join("{}/{}".format(sid, sid),
os.path.join("trace/alert_{}.log".format(env_variables["ORACLE_SID"])))))
return alert_file
def get_env_variables():
env_variables = dict()
env_variables["ORACLE_SID"] = os.getenv("ORACLE_SID")
env_variables["ORACLE_BASE"] = os.getenv("ORACLE_BASE")
# Make a guess at the alert log... Much better ways to do this
env_variables["ORACLE_ALERT_FILE"] = guess_at_alert_file(env_variables["ORACLE_SID"], env_variables)
return env_variables
if __name__ == '__main__':
init(autoreset=True)
env = get_env_variables()
parser = argparse.ArgumentParser(description='Pretty print Oracle Database alert log file')
parser.add_argument('-s', '--sid', help='SID of oracle database', required=False, default='orcl')
parser.add_argument('-a', '--alert_file', help='alert file to be parsed', required=False)
args = parser.parse_args()
if args.sid is not None:
env["ORACLE_SID"] = args.sid
env["ORACLE_ALERT_FILE"] = guess_at_alert_file(args.sid, env)
if args.alert_file is not None:
env["ORACLE_ALERT_FILE"] = args.alert_file
last_date = None
with open(env['ORACLE_ALERT_FILE']) as f:
while True:
line = f.readline()
if line:
try:
date = parse(line) # Lazy!!
diff = None
if last_date is not None:
diff = last_date - date
print_date("{0} : {1} since the last entry".format(date.strftime("%b %d %Y %H:%M:%S.%f"), humanize.naturaldelta(diff)))
last_date = date
except:
print_text(line)
else:
time.sleep(1)
print_footer(env)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment