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

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

SQL Server中行转列方法详细讲解

《SQLServer中行转列方法详细讲解》SQL行转列、列转行可以帮助我们更方便地处理数据,生成需要的报表和结果集,:本文主要介绍SQLServer中行转列方法的相关资料,需要的朋友可以参考下... 目录前言一、为什么需要行转列二、行转列的基本概念三、使用PIVOT运算符进行行转列1.创建示例数据表并插入数

SpringBoot返回文件让前端下载的几种方式

《SpringBoot返回文件让前端下载的几种方式》文章介绍了开发中文件下载的两种常见解决方案,并详细描述了通过后端进行下载的原理和步骤,包括一次性读取到内存和分块写入响应输出流两种方法,此外,还提供... 目录01 背景02 一次性读取到内存,通过响应输出流输出到前端02 将文件流通过循环写入到响应输出流

SpringBoot+Vue3整合SSE实现实时消息推送功能

《SpringBoot+Vue3整合SSE实现实时消息推送功能》在日常开发中,我们经常需要实现实时消息推送的功能,这篇文章将基于SpringBoot和Vue3来简单实现一个入门级的例子,下面小编就和大... 目录前言先大概介绍下SSE后端实现(SpringBoot)前端实现(vue3)1. 数据类型定义2.

MySQL MHA集群详解(数据库高可用)

《MySQLMHA集群详解(数据库高可用)》MHA(MasterHighAvailability)是开源MySQL高可用管理工具,用于自动故障检测与转移,支持异步或半同步复制的MySQL主从架构,本... 目录mysql 高可用方案:MHA 详解与实战1. MHA 简介2. MHA 的组件组成(1)MHA