다른 명령
DB 모니터링 툴 개발 (WEB)
- Flask 웹서버를 이용해 Oracle DB 모니터링 결과를 실시간으로 jQuery DataTables에 표시하는 웹 애플리케이션 개발
menu_book 프로그램 요구사항
- `.json`: DB 접속 정보
- `.ini`: 모니터링 대상 SID 목록
- Flask 서버에서 Oracle 접속 후 테이블 정보 조회
- jQuery DataTables를 이용해 웹에서 시각화
- 웹브라우저로 `/` 접속 시 결과 확인
프로그램 파일 구성
monitoring_flask/
├── app.py
├── config/
│ ├── db_info.json
│ └── target_db.ini
├── templates/
│ └── index.html
└── static/
└── datatables/
└── (CDN 대체용, 오프라인용 구성 시 사용)
---
설정 파일 예시
- 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
---
Flask 메인 로직 (`app.py`)
from flask import Flask, render_template
import cx_Oracle
import json
import configparser
app = Flask(__name__)
def load_db_info():
with open("config/db_info.json", "r") as f:
return json.load(f)
def load_target_sids():
config = configparser.ConfigParser()
config.read("config/target_db.ini")
return [sid.strip() for sid in config.get("MONITOR", "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(info):
dsn = cx_Oracle.makedsn(info["host"], info["port"], sid=info["sid"])
return cx_Oracle.connect(info["user"], info["password"], dsn)
@app.route("/")
def index():
db_info = load_db_info()
target_sids = load_target_sids()
results = {}
for sid in target_sids:
try:
info = db_info[sid]
conn = connect(info)
columns, rows = get_table_info(conn)
results[sid] = {"columns": columns, "rows": rows}
conn.close()
except Exception as e:
results[sid] = {"columns": ["ERROR"], "rows": [[str(e)]]}
return render_template("index.html", db_results=results)
if __name__ == "__main__":
app.run(debug=True)
HTML 템플릿 (`templates/index.html`)
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Oracle 모니터링</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>
<h1>Oracle DB 모니터링</h1>
{% for sid, result in db_results.items() %}
<h2>DB SID: {{ 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 item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
<hr>
<script>
$(document).ready(function () {
$('#table_{{ sid }}').DataTable();
});
</script>
{% endfor %}
</body>
</html>
---
참조사항
의존 패키지 설치
pip install flask cx_Oracle
- Oracle Instant Client 가 설치되어 있어야 cx\_Oracle 사용 가능합니다.
---
실행 방법
python app.py
- 웹 브라우저에서 [1](http://localhost:5000) 접속
- 오프라인 환경에서 실행방법
- DataTables와 jQuery 라이브러리는 `/static/` 디렉터리에 직접 다운로드하여 사용 가능
- `index.html`의 CDN 주소를 static 경로로 변경하면 됩니다