다른 명령
Oracle Database 모니터링 Python 프로그램
menu_book 프로그램 요구사항
- 1. **언어**: Python
- 2. **DB 구성**: SID가 각각 `a`, `b`, `c`, `d`, `e`
- 3. **DB 접속 정보**: `.json` 파일에서 읽기
- 4. **모니터링 대상**: `.ini` 파일에 명시된 DB만 동작
- 5. **출력**: 각 DB의 테이블 정보를 jQuery DataTables로 출력 (HTML 생성)
프로그램 파일 구성
monitoring_project/ ├── config/ │ ├── db_info.json │ └── target_db.ini ├── main.py ├── templates/ │ └── result.html └── output/ └── tables.html
설정 파일 예시
- DB접속 정보 : config/db_info.json
{ "a": {"user": "scott", "password": "tiger", "host": "localhost", "port": "1521", "sid": "a"}, "b": {"user": "scott", "password": "tiger", "host": "localhost", "port": "1521", "sid": "b"}, "c": {"user": "scott", "password": "tiger", "host": "localhost", "port": "1521", "sid": "c"}, "d": {"user": "scott", "password": "tiger", "host": "localhost", "port": "1521", "sid": "d"}, "e": {"user": "scott", "password": "tiger", "host": "localhost", "port": "1521", "sid": "e"} }
- 작업 수행 DB 대상 : config/target_db.ini`
[MONITOR] targets = a,b,c
Python 메인 로직 (`main.py`)
import json import cx_Oracle import configparser from jinja2 import Template import os def load_db_info(json_path): with open(json_path, 'r') as f: return json.load(f) def load_target_sids(ini_path): config = configparser.ConfigParser() config.read(ini_path) targets = config.get("MONITOR", "targets") return [sid.strip() for sid in targets.split(",")] def get_table_info(conn): cursor = conn.cursor() cursor.execute(""" SELECT owner, table_name, tablespace_name, status FROM all_tables WHERE ROWNUM <= 100 """) columns = [desc[0] for desc in cursor.description] rows = cursor.fetchall() return columns, rows def connect_to_db(info): dsn = cx_Oracle.makedsn(info["host"], info["port"], sid=info["sid"]) conn = cx_Oracle.connect(info["user"], info["password"], dsn) return conn def generate_html(results, output_path): with open('templates/result.html', 'r', encoding='utf-8') as f: template = Template(f.read()) html_content = template.render(db_results=results) with open(output_path, 'w', encoding='utf-8') as f: f.write(html_content) def main(): db_info = load_db_info("config/db_info.json") target_sids = load_target_sids("config/target_db.ini") all_results = {} for sid in target_sids: try: info = db_info[sid] conn = connect_to_db(info) columns, rows = get_table_info(conn) all_results[sid] = {"columns": columns, "rows": rows} conn.close() except Exception as e: print(f"{sid} 연결 실패: {e}") generate_html(all_results, "output/tables.html") print("모니터링 결과가 output/tables.html에 생성되었습니다.") if __name__ == "__main__": main()
HTML 템플릿 (`templates/result.html`)
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title>Oracle DB 모니터링 결과</title> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.css"> <script src="https://code.jquery.com/jquery-3.7.1.js"></script> <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.js"></script> </head> <body> {% for sid, result in db_results.items() %} <h2>DB: {{ sid }}</h2> <table id="table_{{ sid }}" class="display"> <thead> <tr> {% for col in result.columns %} <th>{{ col }}</th> {% endfor %} </tr> </thead> <tbody> {% for row in result.rows %} <tr> {% for val in row %} <td>{{ val }}</td> {% endfor %} </tr> {% endfor %} </tbody> </table> <hr> <script> $(document).ready(function() { $('#table_{{ sid }}').DataTable(); }); </script> {% endfor %} </body> </html>
참조사항
의존 패키지 설치
- cx_Oracle 설치
- jinja2 설치
pip install cx_Oracle jinja2
실행 방법
python main.py
- 결과는 `output/tables.html`에 생성되며, 브라우저에서 열면 DataTables로 출력됩니다.