mysql自动化巡检脚本生成html报告

2024-06-13 23:38

本文主要是介绍mysql自动化巡检脚本生成html报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、 前言

1、之前出了一篇oracle自动巡检脚本生成html报告和一篇Linux服务器系统自动巡检脚本生成html报告,本篇文章出一篇mysql自动巡检脚本生成html报告。
2、脚本只提供部分简单的巡检内容,如binlog信息、数据库运行信息,还有些mysql的一些重要参数配置,其他的巡检内容大家根据实际需要编辑修改,增加符合自己需求的巡检内容。
3、项目已经上传到我的github上
项目地址:mywatch.git

二、注意事项与报告部分截图

注意阅读git上的README.md说明
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、README.md

1、需要使用root用户执行
2、使用说明

多实例下运行此脚本时,要注意区分不同实例的root用户密码与对应端口号
myuser="root"
mypasswd="XXXXX"
myip="localhost"
myport="3306"

3、执行完巡检之后,将在脚本所在的路径下生成html巡检结果报告,如下192.168.35.244os_mysql_simple_summary.html

4、巡检项信息如下(其他统计项可根据实际需要自行添加)

0)、巡检ip信息

1)、数据库基本信息与binlog参数信息

2)、数据库运行状况信息

3)、数据库一些重要参数配置信息

四、脚本内容

#!/bin/bash
ipaddress=`ip a|grep "global"|awk '{print $2}' |awk -F/ '{print $1}'`
file_output=${ipaddress}'os_mysql_simple_summary.html'
td_str=''
th_str=''
myuser="root"
mypasswd="XXXXX"
myip="localhost"
myport="3306"
mysql_cmd="mysql -u${myuser} -p${mypasswd} -h${myip} -P${myport} --protocol=tcp --silent"
#yum -y install bc sysstat net-tools
create_html_css(){echo -e "<html>
<head>
<style type="text/css">body        {font:12px Courier New,Helvetica,sansserif; color:black; background:White;}table,tr,td {font:12px Courier New,Helvetica,sansserif; color:Black; background:#FFFFCC; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} th          {font:bold 12px Courier New,Helvetica,sansserif; color:White; background:#0033FF; padding:0px 0px 0px 0px;} h1          {font:bold 12pt Courier New,Helvetica,sansserif; color:Black; padding:0px 0px 0px 0px;} 
</style>
</head>
<body>"
}
create_html_head(){
echo -e "<h1>$1</h1>"
}
create_table_head1(){echo -e "<table width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_table_head2(){echo -e "<table width="100%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_td(){td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'`
}
create_th(){th_str=`echo $1|awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<th>"$i"</th>";i++}}'`
}
create_tr1(){create_td "$1"echo -e "<tr>$td_str</tr>" >> $file_output
}
create_tr2(){create_th "$1"echo -e "<tr>$th_str</tr>" >> $file_output
}
create_tr3(){echo -e "<tr><td><pre style=\"font-family:Courier New; word-wrap: break-word; white-space: pre-wrap; white-space: -moz-pre-wrap\" >`cat $1`</pre></td></tr>" >> $file_output
}
create_table_end(){echo -e "</table>"
}
create_html_end(){echo -e "</body></html>"
}
NAME_VAL_LEN=12
name_val () {printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
}
get_netinfo(){echo "interface | status | ipadds     |      mtu    |  Speed     |     Duplex" >>/tmp/tmpnet_h1_`date +%y%m%d`.txtfor ipstr in `ifconfig -a|grep ": flags"|awk  '{print $1}'|sed 's/.$//'`doipadds=`ifconfig ${ipstr}|grep -w inet|awk '{print $2}'`mtu=`ifconfig ${ipstr}|grep mtu|awk '{print $NF}'`speed=`ethtool ${ipstr}|grep Speed|awk -F: '{print $2}'`duplex=`ethtool ${ipstr}|grep Duplex|awk -F: '{print $2}'`echo "${ipstr}"  "up" "${ipadds}" "${mtu}" "${speed}" "${duplex}"\|awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6}'  >>/tmp/tmpnet1_`date +%y%m%d`.txtdone}
my_base_info(){${mysql_cmd} -e "select now(),current_user(),version()\G"${mysql_cmd} -e "show global variables like 'autocommit';"|grep -i ^auto|awk '{print $1,":",$2}'${mysql_cmd} -e "show variables like '%binlog%';"|awk '{print $1,":",$2}'${mysql_cmd} -e "show variables like 'innodb_flush%';"|awk '{print $1,":",$2}'
}
my_stat_info(){${mysql_cmd} -e status >>/tmp/tmpmy_stat_`date +%y%m%d`.txt
}
my_param_info(){echo "Variable_name|Value" >>/tmp/tmpmy_param_h1_`date +%y%m%d`.txt${mysql_cmd} -e "show global variables"|egrep -w "innodb_buffer_pool_size|innodb_file_per_table|innodb_flush_log_at_trx_commit|innodb_io_capacity|\innodb_lock_wait_timeout|innodb_data_home_dir|innodb_log_file_size|innodb_log_files_in_group|log_slave_updates|long_query_time|lower_case_table_names|\max_connections|max_connect_errors|max_user_connections|query_cache_size|query_cache_type |server_id|slow_query_log|slow_query_log_file|innodb_temp_data_file_path|\sql_mode|gtid_mode|enforce_gtid_consistency|expire_logs_days|sync_binlog|open_files_limit|myisam_sort_buffer_size|myisam_max_sort_file_size"\|awk '{print $1,"|",$2}' >>/tmp/tmpmy_param_t1_`date +%y%m%d`.txt
}
create_html(){rm -rf $file_outputtouch $file_outputcreate_html_css >> $file_outputcreate_html_head "Network Info Summary" >> $file_outputcreate_table_head1 >> $file_outputget_netinfowhile read linedocreate_tr2 "$line" done < /tmp/tmpnet_h1_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpnet1_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_html_head "Basic Database && binlog Information" >> $file_outputcreate_table_head1 >> $file_outputmy_base_info >>/tmp/tmpmy_base_`date +%y%m%d`.txtsed -i -e '1d' -e 's/:/|/g' /tmp/tmpmy_base_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done </tmp/tmpmy_base_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_html_head "Running Status of Database" >> $file_outputcreate_table_head1 >> $file_outputmy_stat_info  create_tr3 "/tmp/tmpmy_stat_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "Important Parameters" >> $file_outputcreate_table_head1 >> $file_outputmy_param_infowhile read linedocreate_tr2 "$line" done < /tmp/tmpmy_param_h1_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpmy_param_t1_`date +%y%m%d`.txtcreate_table_end >> $file_output create_html_end >> $file_outputsed -i 's/BORDER=1/width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse"/g' $file_outputrm -rf /tmp/tmp*_`date +%y%m%d`.txt
}
# This script must be executed as root
RUID=`id|awk -F\( '{print $1}'|awk -F\= '{print $2}'`
if [ ${RUID} != "0" ];thenecho"This script must be executed as root"exit 1
fi
PLATFORM=`uname`
if [ ${PLATFORM} = "HP-UX" ] ; thenecho "This script does not support HP-UX platform for the time being"
exit 1
elif [ ${PLATFORM} = "SunOS" ] ; thenecho "This script does not support SunOS platform for the time being"
exit 1
elif [ ${PLATFORM} = "AIX" ] ; thenecho "This script does not support AIX platform for the time being"
exit 1
elif [ ${PLATFORM} = "Linux" ] ; then
echo -e "
###########################################################################################
#Make sure that the following parameters at the beginning of the script are correct.
#myuser="root"      (Database Account)
#mypasswd="XXXXXX"  (Database password)
#myip="localhost"   (Database native IP)
#myport="3306"      (Database port)
#--> Otherwise, the script cannot be executed properly.
#GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' identified by 'XXXXX' WITH GRANT OPTION;
#flush privileges;
###########################################################################################
"create_html
fi

这篇关于mysql自动化巡检脚本生成html报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1058756

相关文章

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地