首页 > 数据库 >如何将MySQL巡检内容转换成PDF格式报告呢?

如何将MySQL巡检内容转换成PDF格式报告呢?

时间:2024-10-22 18:18:26浏览次数:7  
标签:巡检 like show variables MYSQL echo MySQL PDF QUERY

一、背景:

  最近在运维一个历史项目,没有任何的运维工具,甲方要求每日进行数据库的运维,而且必须进行相关的巡检项的截图并输出报告。为了节省时间,我们可以把原来的巡检脚本修改成HTML格式输出后,进行PDF的转换。

二、脚本内容:

[root@postgresql ~]# chmod +x mysqlcheckhtml.sh 
[root@postgresql ~]# ./mysqlcheckhtml.sh 
#!/bin/bash
MYSQL_USER="root"
MYSQL_PASS="123456"
MYSQL_HOST="192.168.59.136"
MYSQL_PORT="3306"
MYSQL_QUERY="/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -h$MYSQL_HOST -P$MYSQL_PORT -e"
HTML_FILE="./mysql_Healthcheck_$MYSQL_HOST.html"
DATE=$(date "+%Y-%m-%d %H:%M:%S")
check_mysql_status() {
    $MYSQL_QUERY "SHOW DATABASES;" > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "MySQL服务运行异常,退出巡检!"
        exit 1
    else
        echo "MySQL服务运行正常,执行巡检!"
    fi
}

echo_head() {
    echo "<html lang=\"zh-CN\"><meta charset=\"UTF-8\"><head><title>MySQL健康巡检报告</title></head><body>"
    echo "<style>"
    echo "body { font-family: Arial, sans-serif; }"
    echo "table { width: 100%; border-collapse: collapse; }"
    echo "th, td { border: 1px solid #ddd; padding: 6px; text-align: left; }"
    echo "th {background-color: #f2f2f2; }"
    echo "</style>"
    echo "<body>"
    echo "<h1>XXXX项目</h1>"
    echo "<h2>MySQL健康巡检报告 - $MYSQL_HOST</h2>"
    echo "<h2>巡检时间:$DATE &nbsp;&nbsp;&nbsp;&nbsp 巡检人员:zhh</h2>"
}

inspect_mysql() {
    echo "<table border='1'>"
    echo " <tr>"
    echo " <th>巡检项</th>"
    echo " <th>巡检命令</th>"
    echo " <th>结果</th>"
    echo " </tr>"
    echo " <tbody>"
    # MySQL版本信息
    echo " <tr>"
    echo " <td>MySQL版本信息</td>"
    echo " <td>SELECT VERSION();</td>"
    echo " <td>"
    $MYSQL_QUERY "SELECT VERSION();" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # MySQL连接数
    echo " <tr>"
    echo " <td>MySQL连接数</td>"
    echo " <td>SHOW STATUS LIKE 'Threads_connected';</td>"
    echo " <td>"
    $MYSQL_QUERY "SHOW STATUS LIKE 'Threads_connected';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 侦听队列的大小
    echo " <tr>"
    echo " <td>侦听队列大小</td>"
    echo " <td>show variables like 'back_log%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'back_log%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # max_allowed_packet包的值
    echo " <tr>"
    echo " <td>max_allowed_packet值</td>"
    echo " <td>show variables like 'max_allowed_packet%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'max_allowed_packet%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 交互式连接超时时间
    echo " <tr>"
    echo " <td>交互超时时间</td>"
    echo " <td>show variables like 'interactive_timeout%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'interactive_timeout%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # skip_name_resolve
    echo " <tr>"
    echo " <td>skip_name_resolve</td>"
    echo " <td>show variables like 'skip_name_resolve%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'skip_name_resolve%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 最大连接数检查(当前值)
    echo " <tr>"
    echo " <td>最大连接数(当前)</td>"
    echo " <td>show global status like 'max_used_connections';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global status like 'max_used_connections';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 最大连接数检查(最大值)
    echo " <tr>"
    echo " <td>最大连接数(最大)</td>"
    echo " <td>show global variables like 'max_connections';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global variables like 'max_connections';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 当前连接数检查
    echo " <tr>"
    echo " <td>当前连接数</td>"
    echo " <td>show global status like 'Threads_connected';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global status like 'Threads_connected';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 异常连接检查
    echo " <tr>"
    echo " <td>异常连接</td>"
    echo " <td>show global status like 'aborted%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global status like 'aborted%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # binlog保留天数
    echo " <tr>"
    echo " <td>binlog保留天数</td>"
    echo " <td>show variables like 'expire_logs_days%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'expire_logs_days%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 文件打开限制数
    echo " <tr>"
    echo " <td>文件打开限制</td>"
    echo " <td>show variables like 'open_files_limit%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'open_files_limit%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 线程池缓存大小
    echo " <tr>"
    echo " <td>线程池缓存</td>"
    echo " <td>show variables like 'thread_cache_size%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'thread_cache_size%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 排序缓冲区大小
    echo " <tr>"
    echo " <td>排序缓冲区</td>"
    echo " <td>show variables like 'sort_buffer_size%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'sort_buffer_size%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 内连接缓冲区大小
    echo " <tr>"
    echo " <td>内连接缓冲区</td>"
    echo " <td>show variables like 'join_buffer_size%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'join_buffer_size%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 存储引擎缓存分配大小
    echo " <tr>"
    echo " <td>InnoDB缓存分配</td>"
    echo " <td>show global variables like 'innodb_buffer_pool_size';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global variables like 'innodb_buffer_pool_size';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 引擎的 I/O 读写速率
    echo " <tr>"
    echo " <td>InnoDB I/O读写速率</td>"
    echo " <td>show global variables like 'innodb_io_capacity';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global variables like 'innodb_io_capacity';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 表缓存检查(打开表数量)
    echo " <tr>"
    echo " <td>表缓存(打开表数)</td>"
    echo " <td>show global status like '%opened_tables%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show global status like '%opened_tables%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 表缓存检查(缓存大小)
    echo " <tr>"
    echo " <td>表缓存(缓存大小)</td>"
    echo " <td>show variables like '%table_open_cache%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like '%table_open_cache%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 独立表空间
    echo " <tr>"
    echo " <td>InnoDB独立表空间</td>"
    echo " <td>show variables like 'innodb_file_per_table%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_file_per_table%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 打开文件数
    echo " <tr>"
    echo " <td>InnoDB打开文件数</td>"
    echo " <td>show variables like 'innodb_open_files%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_open_files%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 并发线程
    echo " <tr>"
    echo " <td>InnoDB并发线程</td>"
    echo " <td>show variables like 'innodb_thread_concurrency%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_thread_concurrency%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 将缓存中的 redo 日志回写到日志文件的设置
    echo " <tr>"
    echo " <td>InnoDB日志回写设置</td>"
    echo " <td>show variables like 'innodb_flush_log_at_trx_commit%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_flush_log_at_trx_commit%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 日志缓冲大小-sync_binlog
    echo " <tr>"
    echo " <td>InnoDB日志缓冲(sync_binlog)</td>"
    echo " <td>show variables like 'sync_binlog%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'sync_binlog%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 日志缓冲大小-innodb_log_buffer_size
    echo " <tr>"
    echo " <td>InnoDB日志缓冲(innodb_log_buffer_size)</td>"
    echo " <td>show variables like 'innodb_log_buffer_size%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_log_buffer_size%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 日志文件大小
    echo " <tr>"
    echo " <td>InnoDB日志文件大小</td>"
    echo " <td>show variables like 'innodb_log_file_size%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_log_file_size%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # InnoDB 日志文件组
    echo " <tr>"
    echo " <td>InnoDB日志文件组</td>"
    echo " <td>show variables like 'innodb_log_files_in_group%';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'innodb_log_files_in_group%';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # QPS 检查
    echo " <tr>"
    echo " <td>QPS检查</td>"
    echo " <td>show status like 'queries';</td>"
    echo " <td>"
    $MYSQL_QUERY "show status like 'queries';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 数据库字符集
    echo " <tr>"
    echo " <td>数据库字符集</td>"
    echo " <td>show variables like 'character_set_database';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'character_set_database';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 安装路径
    echo " <tr>"
    echo " <td>安装路径</td>"
    echo " <td>show variables like 'basedir';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'basedir';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 慢查询日志
    echo " <tr>"
    echo " <td>慢查询日志</td>"
    echo " <td>show variables like 'slow_query_log';</td>"
    echo " <td>"
    $MYSQL_QUERY "show variables like 'slow_query_log';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    # 锁表信息
    echo " <tr>"
    echo " <td>锁表信息(立即)</td>"
    echo " <td>show status like 'Table_locks_immediate';</td>"
    echo " <td>"
    $MYSQL_QUERY "show status like 'Table_locks_immediate';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    echo " <tr>"
    echo " <td>锁表信息(等待)</td>"
    echo " <td>show status like 'Table_locks_waited';</td>"
    echo " <td>"
    $MYSQL_QUERY "show status like 'Table_locks_waited';" | tail -n +2
    echo "</td>"
    echo " </tr>"
    echo "</table>"
}

# 数据库大小
function DB_size(){
    echo "<h1>数据库大小</h1>"
    echo "<table border='1'>"
    echo "<tr><th>数据库名</th><th>表行数</th><th>库大小</th><th>索引大小(MB)</th></tr>"
    # 执行MySQL查询并生成HTML表格
    $MYSQL_QUERY "SELECT table_schema 'Database name', sum( table_rows ) 'No. of rows', sum( data_length ) / 1024 / 1024 'Size data (MB)', sum( index_length)/ 1024 / 1024 'Size index (MB)' FROM information_schema.TABLES GROUP BY table_schema;" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td></tr>"}'
    echo "</table>"
}
#数据盘物理大小检查
function SHOW_datadisksize(){
    echo "<h1>数据盘物理大小</h1>"
    echo "<table border='1'>"
    echo "<tr><th>大小</th></tr>"
    DATA_PATH_SIZE=$(du -sh /data/mysql | awk '{print $1}')
    echo "<tr><td>$DATA_PATH_SIZE</td></tr>"
    echo "</table>"
}
#连接数检查
function SHOW_connectioninfo(){
    echo "<h1>连接信息</h1>"
    echo "<table border='1'>"
    echo "<tr><th>类型</th><th>值</th></tr>"
    max_used_connections=$($MYSQL_QUERY "show global status like 'max_used_connections';" | tail -n +2)
    max_connections=$($MYSQL_QUERY "show global variables like 'max_connections';" | tail -n +2)
    threads_connected=$($MYSQL_QUERY "show global status like 'Threads_connected';" | tail -n +2)
    aborted_connections=$($MYSQL_QUERY "show global status like 'aborted%';" | tail -n +2)
    echo "<tr><td>最大连接数(当前)</td><td>$max_used_connections</td></tr>"
    echo "<tr><td>最大连接数(最大)</td><td>$max_connections</td></tr>"
    echo "<tr><td>当前连接数</td><td>$threads_connected</td></tr>"
    echo "<tr><td>异常连接</td><td>$aborted_connections</td></tr>"
    echo "</table>"
}
# 存储引擎不是 innodb 的表
function NO_innodb(){
    echo "<h1>存储引擎不是 innodb 的表</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Database name</th><th>Table name</th><th>Engine</th></tr>"
    $MYSQL_QUERY "SELECT TABLE_SCHEMA, TABLE_NAME,ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE!= 'innodb' AND TABLE_SCHEMA NOT IN ( 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA', 'MYSQL', 'SYS' );" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td></tr>"}'
    echo "</table>"
}

# 线程状态查询
function SHOW_processlist(){
    echo "<h1>运行线程状态查询</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Id</th><th>User</th><th>Host</th><th>db</th><th>Command</th><th>Time</th><th>State</th><th>Info</th></tr>"
    $MYSQL_QUERY "SHOW FULL PROCESSLIST;" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td><td>"$8"</td></tr>"}'
    echo "</table>"
}

# 开启 binlog 日志
function SHOW_binlog(){
    echo "<h1>开启 binlog 日志</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Variable_name</th><th>Value</th></tr>"
    $MYSQL_QUERY "SHOW VARIABLES LIKE 'log_bin%';" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td></tr>"}'
    echo "</table>"
}

# 查询缓存检查
function SHOW_query_cache(){
    echo "<h1>查询缓存检查</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Variable_name</th><th>Value</th></tr>"
    $MYSQL_QUERY "SHOW VARIABLES LIKE '%query_cache%';" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td></tr>"}'
    echo "</table>"
}
# 锁表信息
function SHOW_slowtime(){
    echo "<h1>慢查询检查</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Variable_name</th><th>Value</th></tr>"
    $MYSQL_QUERY "show variables like 'slow_query_log';" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td></tr>"}'
    echo "</table>"
}
# 主从复制状态
function SHOW_MS_STATUS(){
    echo "<h1>主从复制状态</h1>"
    echo "<table border='1'>"
    echo "<tr><th>Slave_IO_Running</th><th>Slave_SQL_Running</th><th>Seconds_Behind_Master</th></tr>"
    $MYSQL_QUERY "show slave status\G" | \
    awk '{print "<tr><td>"$1"</td><td>"$2"</td></tr>"}'
    echo "</table>"
}
# 错误日志
function SHOW_errorlog(){
    echo "<h1>错误日志</h1>"
    echo "<table border='1'>"
    echo "<tr><th>日志内容</th></tr>"
    ERROR_LOG=$(tail -n 5 /data/mysql/mysql.err)
    echo "<tr><td>$ERROR_LOG</td></tr>"
    echo "</table>"
}
function SHOW_lockinfo(){
    echo "<h1>锁表信息</h1>"
    echo "<table border='1'>"
    echo "<tr><th>类型</th><th>值</th></tr>"
    immediate_locks=$($MYSQL_QUERY "show status like 'Table_locks_immediate';" | tail -n +2)
    waited_locks=$($MYSQL_QUERY "show status like 'Table_locks_waited';" | tail -n +2)
    echo "<tr><td>立即锁表数量</td><td>$immediate_locks</td></tr>"
    echo "<tr><td>等待锁表数量</td><td>$waited_locks</td></tr>"
    echo "</table>"
}
function echo_foot(){
    echo "</body></html>"
}

# 输出html头部
echo_head > $HTML_FILE

# 执行巡检并生成HTML文件
inspect_mysql >> $HTML_FILE

# 数据库大小
DB_size >> $HTML_FILE
SHOW_datadisksize >> $HTML_FILE
SHOW_connectioninfo >> $HTML_FILE
SHOW_processlist >> $HTML_FILE
SHOW_binlog >> $HTML_FILE
SHOW_query_cache >> $HTML_FILE
NO_innodb >> $HTML_FILE
SHOW_slowtime >> $HTML_FILE
SHOW_MS_STATUS >> $HTML_FILE
SHOW_errorlog >> $HTML_FILE
SHOW_lockinfo >> $HTML_FILE
# 输出html底部
echo_foot >> $HTML_FILE

脚本完成后,会输出为mysql_Healthcheck_192.168.59.136.html,FTP到本地用浏览器打开

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
按CTRL+P打印为PDF
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、总结

这样就可以完成每日的巡检日报交差了,再也不用把脚本巡检的内容一张一张截图放在WORD里面了。当然这个格式还很LOW,后面会继续进行完善。
参考链接:https://www.163.com/dy/article/JEDT63QT05314EKW.html
欢迎关注公众号《小周的数据库进阶之路》,更多精彩知识和干货尽在其中。
在这里插入图片描述

标签:巡检,like,show,variables,MYSQL,echo,MySQL,PDF,QUERY
From: https://blog.csdn.net/qq_36936192/article/details/143124943

相关文章

  • MySQL 集群部署
    MySQL集群部署环境集群模式:双主集群服务器:192.168.93.100、192.168.93.101版本:mysql-5.7.44配置192.168.93.101my.cnf[mysqld]skip-host-cacheskip-name-resolve#修改数据磁盘data目录datadir=/var/lib/mysqlsocket=/var/run/mysqld/mysqld.socksecure-file-pri......
  • MySQL的 主从同步
    1.概述MySQL主从同步(Replication)是一种数据备份和灾难恢复的解决方案,同时也可以用于负载均衡和读写分离。通过主从同步,可以将一个MySQL数据库服务器(主服务器)的数据实时复制到另一台或多台MySQL服务器(从服务器)。本文档将介绍如何配置MySQL主从同步,并提供示例操作。2.......
  • django+mysql怎么开局
    后端开局:django+mysql思路是这篇:vue3+django+mysql实现一个简单的前后端分离的小案例-CSDN博客首先,用pycharm创建的时候我选的是anaconda3虚拟环境然后创建项目的时候选择django项目,会帮你创建好基础的东西。然后你需要在终端使用命令来创建apps文件夹。(看这篇:django创建......
  • 火锅店管理系统/火锅店管理软件/餐饮管理系统/火锅店收银系统/餐厅管理软件/火锅店ERP
    博主介绍......
  • 小型诊疗预约平台/小型诊疗/预约平台/医疗预约/诊所预约/医生预约/网上预约/医疗服务/
    博主介绍......
  • MySQL基于gtid同步,新增slave节点
    环境说明:当前MySQL集群为一主一从,新增加Slave节点,将架构变更为一主两从,集群已经运行了很长时间,主节点得binlog早就被purged,启动slave得时候会报错,1236、1062等操作步骤:备份master数据,从节点resetmaster,导入数据1.备份主节点数据:在进行任何操作之前,首先需要对主节点的数据进......
  • mysql建议单表2000万条数据的由来
    Mysql在建表之初就要考虑到他的存储量和性能问题,所以一般Mysql数据库建议单表最大两千万,但是为啥是两千万呢这里我们解释一下,知其然还要知其所以然!这一块的知识解释起来会涉及一点存储引型的相关知识了这里给提供一个基本概念,但是了解完之后会对InnonDB引型会有一定的了解数据......
  • 保姆级 | MySQL的安装配置教程(非常详细)
    一、下载Mysql从官网下载MySQL,这里我选用的是Mysql8.0.34版本   二、安装Mysql下载完成后直接双击进行安装,打开后的页面如下所示:“DeveloperDefault”是开发者默认“Serveronly”仅作为服务器安装“Clientonly”仅作为客户端安装“Full”是完整安装“Custom”......
  • 用MySQL5.7的客户端连接MySQL8.0的服务端竟然报错,不是说好向下兼容吗?
    在默认条件下,用MySQL5.7的客户端连接MySQL8.0的服务端竟然报错,不是说好向下兼容吗?WHAT?报错如下:[root@node234~]#mysql-ushukuinfo-p'123456'-h172.16.1.223mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.ERROR2026(HY000):SSLconn......
  • mysql innodb_data_file_path参数忘记设置或者重新调整办法
    目录mysqlinnodb_data_file_path参数忘记设置或者重新调整办法mysqlinnodb_data_file_path参数忘记设置或者重新调整办法my.cnf文件中,默认配置为innodb_data_file_path=ibdata1:10M:autoextend目前该文件已经扩大到了1g多:-rw-r-----1mysqlmysql14701035524月161......