一、巡检报告shell脚本
#!/bin/bash
# MySQL 连接参数
HOST="10.10.10.1"
PORT="3306"
USER="root"
PASSWORD="rootmysql"
SQL_QUERY="CALL sys.diagnostics(1, 1, 'current')"
# 输出 HTML 文件路径
html_file="/tmp/report_$(date +"%Y-%m-%d_%H-%M").html"
# 执行 SQL 并生成 HTML 文件
mysql -h"$HOST" -P"$PORT" -u"$USER" -p"$PASSWORD" -H -e "$SQL_QUERY" > "$html_file"
# 插入 AWR 风格的 CSS 样式
cat << 'EOF' | sed -i '1i\
<!DOCTYPE html>\
<html lang="en">\
<head>\
<meta charset="UTF-8">\
<title>MySQL Diagnostics Report</title>\
<style type="text/css">\
body { font: bold 10pt Arial, Helvetica, sans-serif; color: black; background: white; }\
table { width: 100%; border-collapse: collapse; }\
th { background-color: #0066CC; color: white; padding: 8px; text-align: left; }\
td { padding: 8px; border: 1px solid #ddd; vertical-align: top; }\
tr:nth-child(even) { background-color: #f2f2f2; }\
h1 { font-size: 20pt; color: #336699; margin-bottom: 10px; }\
h2 { font-size: 18pt; color: #336699; margin-top: 10px; margin-bottom: 5px; }\
</style>\
</head>\
<body>\
<h1 >MySQL Diagnostics Report</h1>' "$html_file"
EOF
# 提取报告内容并创建链接
report_content=$(grep -oP 'The following output is:</TH></TR><TR><TD>\K[^<]+' $html_file | grep -Ev '^(NOW\(\)|\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|[0-9]{4}-[0-9]{2}-[0-9]{2}|^\s*$)')
link_table="<table><tr><ul>"
# 计数器,用于分列
counter=0
max_columns=4
# 创建超链接并将其放入表格中
while IFS= read -r line; do
link_name=$(echo "$line" | tr '[:upper:]' '[:lower:]' | sed 's/[ .]/_/g') # 生成链接名
link_table+="<td><li><a href=\"#$link_name\">$line</a></li></td>"
escaped_line=$(printf '%s\n' "$line" | sed 's/[.*]/\\&/g')
sed -i "s|<TD>$escaped_line</TD>|<TD id=\"$link_name\">$escaped_line</TD>|g" "$html_file"
counter=$((counter + 1))
# 每两列换一行
if (( counter % max_columns == 0 )); then
link_table+="</tr><tr>"
fi
done <<< "$report_content"
# 完成表格
link_table+="</ul></tr></table>"
# 在HTML中插入 Main Report
sed -i "/<h1 >MySQL Diagnostics Report<\/h1>/a $link_table" "$html_file"
# 追加 HTML 结束标签
echo "</body></html>" >> "$html_file"
echo "Report generated: $html_file"