一、背景:
最近在运维一个历史项目,没有任何的运维工具,甲方要求每日进行数据库的运维,而且必须进行相关的巡检项的截图并输出报告。为了节省时间,我们可以把原来的巡检脚本修改成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   巡检人员: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
欢迎关注公众号《小周的数据库进阶之路》,更多精彩知识和干货尽在其中。