暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

基于python脚本编写自动生成数据库巡检报告(具有框架性,只需修改链接信息即可兼容所有数据库)

原创 zhoushao12 2023-05-26
1173

功能实现

1、目前仅完成初版,可对数据库进行自动化巡检并生成html版本巡检报告

2、后续计划通过使用yaml配置文件实现自动对mysql、Postgresql、Kingbase、Redis等数据库进行检查

Python脚本源码

#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @文件 :oradb_check.py @说明 :zhoushao12@163.com @时间 :2023/05/25 14:26:10 @作者 :Jaja @版本 :1.0 ''' import cx_Oracle from jinja2 import Environment, FileSystemLoader import time CheckTitle = "*******" CheckUserName = "*******" CheckCompany = "*******" CheckScriptVersion = "V1.0" CheckExplain = "这是SQL查询结果的巡检说明。" current_datetime = time.strftime("%Y-%m-%d", time.localtime()) # 连接到 Oracle 数据库 conn = cx_Oracle.connect('system', 'oracle', '192.168.98.136:1521/test') queries = [ { 'query': "SELECT * FROM dba_tables where rownum < 100", # 替换为第一个查询语句 'columns_var': 'columns1', 'rows_var': 'rows1', 'name': '第一个查询', 'description': '这是第一个查询的备注说明信息。' }, { 'query': "SELECT * FROM dba_indexes where rownum < 100", # 替换为第二个查询语句 'columns_var': 'columns2', 'rows_var': 'rows2', 'name': '第二个查询', 'description': '这是第二个查询的备注说明信息。' } # 添加更多查询语句和结果集变量 ] # 执行查询并获取结果集的列信息和数据行 results = [] for query in queries: cursor = conn.cursor() cursor.execute(query['query']) columns = [col[0] for col in cursor.description] rows = cursor.fetchall() results.append({ 'columns_var': query['columns_var'], 'columns': columns, 'rows_var': query['rows_var'], 'rows': rows, 'name': query['name'], 'description': query['description'] }) # 关闭数据库连接 cursor.close() conn.close() # 加载 Jinja2 模板 env = Environment(loader=FileSystemLoader('.')) template = env.get_template('testjinjia2_v1.0.html') # 替换为实际的模板文件路径 # 渲染模板并生成 HTML 文件 output = template.render(results=results,current_datetime=current_datetime,CheckTitle=CheckTitle,CheckUserName=CheckUserName,CheckCompany=CheckCompany,CheckScriptVersion=CheckScriptVersion,CheckExplain=CheckExplain) with open('output.html', 'w') as file: file.write(output) print("HTML 文件已生成。")

jinjia2模板

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> {{ CheckTitle }} </title> <style> .logo { width: 200px; /* 自定义宽度 */ height: auto; /* 自定义高度或使用 auto 保持纵横比 */ margin-right: 10px; } body { font-family: Arial, sans-serif; margin: 20px; } header { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; text-align: center; margin-bottom: 30px; } h1 { text-align: center; font-size: 40px; margin-bottom: 30px; } h2 { margin-top: 40px; margin-bottom: 16px; } table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #4CAF50; color: white; } ul { list-style-type: none; margin-left: 20px; } li { margin-bottom: 10px; } #top-link { position: fixed; bottom: 20px; right: 20px; padding: 10px; background-color: #4CAF50; color: white; text-decoration: none; display: none; } .result-set { margin-bottom: 40px; } .result-set-title { margin-bottom: 20px; } .result-set-title a { font-size: 18px; } .result-set-remark { font-style: italic; margin-top: 10px; } </style> <script> window.addEventListener('scroll', function() { var topLink = document.getElementById('top-link'); if (window.pageYOffset > 200) { topLink.style.display = 'block'; } else { topLink.style.display = 'none'; } }); </script> </head> <body> <header> <img class="logo" src="log.png" alt="LOGO"> <h1>{{ CheckTitle }}</h1> </header> <header1> <p>巡检人员: {{ CheckUserName }}</p> <p>巡检公司: {{ CheckCompany }}</p> <p>巡检版本: {{ CheckScriptVersion }}</p> <p>巡检日期: {{ current_datetime }}</p> <p>巡检说明: {{ CheckExplain }}</p> </header1> <a href="#top" id="top-link">返回顶部</a> <h2>目录</h2> <ul> {% for i in range(results|length) %} <li><a href="#result-set-{{ i+1 }}">{{ results[i]['name'] }}</a></li> {% endfor %} </ul> {% for i in range(results|length) %} <div class="result-set"> <h2 id="result-set-{{ i+1 }}" class="result-set-title">{{ results[i]['name'] }} <a href="#top">返回顶部</a></h2> <table> <tr> {% for column in results[i]['columns'] %} <th>{{ column }}</th> {% endfor %} </tr> {% for row in results[i]['rows'] %} <tr> {% for value in row %} <td>{{ value }}</td> {% endfor %} </tr> {% endfor %} </table> <p class="result-set-remark">{{ results[i]['description'] }}</p> </div> {% endfor %} </body> </html>
最后修改时间:2023-05-31 14:24:00
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论