使用doDBA脚本每天自动监控MySQL数据库,并且切分日志压缩

【环境介绍】

CentOS Linux release 7.7 + MySQL 5.7

【背景描述】

需要定时对数据库信息进行监控:会话及innodb信息监控

【脚本部署】

创建目录:
mkdir -p /home/mysql/doDBA/log 日志目录
mkdir -p /home/mysql/doDBA/conf 配置文件目录
mkdir -p /home/mysql/doDBA/scripts 脚本目录


需要监控的数据库日志目录:
mkdir -p /home/mysql/doDBA/log/mysqldb01
mkdir -p /home/mysql/doDBA/log/mysqldb02


设置配置文件:
cd /home/mysql/doDBA/conf
第一个数据库配置文件
cat >mysqldb01_doDBA.conf
{
"Host":"10.0.0.xxx",
"Huser": "root",
"Hport": "22",
"Hpwd": "xxx",
"Muser": "mysql_monitor",
"Mpwd": "Mysql_monitor123",
"Mport":"3307"
}

第二个数据库配置文件
cat >mysqldb02_doDBA.conf
{
"Host":"10.0.0.xxx",
"Huser": "root",
"Hport": "22",
"Hpwd": "xxx",
"Muser": "mysql_monitor",
"Mpwd": "Mysql_monitor123",
"Mport":"3307"
}


部署脚本:
cd /home/mysql/doDBA/scripts

注意每个数据库的日志文件跟配置文件变量

cat >dodba_monitor.sh
#!/bin/bash
v_dodba='/home/mysql/doDBA/doDBA'

mysqldb01_monitor(){
v_config='/home/mysql/doDBA/conf/mysqldb01_doDBA.conf'
v_log_path='/home/mysql/doDBA/log/mysqldb01'
cd $v_log_path
$v_dodba -c $v_config -myall -t=0 -rds -log &
}

mysqldb02_monitor(){
v_config='/home/mysql/doDBA/conf/mysqldb02_doDBA.conf'
v_log_path='/home/mysql/doDBA/log/mysqldb02'
cd $v_log_path
$v_dodba -c $v_config -myall -t=0 -rds -log &
}

mysqldb01_monitor
mysqldb02_monitor

切日志脚本:
cat >cut_dodba_log.sh
#!/bin/bash
v_time=`date -d "-1 hour" +"%Y%m%d_%H"`
v_log_name='dodba.log'

mysqldb01_log(){
v_db_name='mysqldb01'
v_log_path='/home/mysql/doDBA/log/mysqldb01'
v_myall_name=`ls ${v_log_path}|grep dodba_myall`
v_myall_name_new="${v_db_name}_myall_${v_time}.log.gz"
echo $v_myall_name_new
cd $v_log_path
if [ ! -f "$v_myall_name_new" ]; then
mv ${v_log_name} ${v_db_name}_monitor_${v_time}.log
cat ${v_myall_name} > ${v_db_name}_myall_${v_time}.log
echo '' > ${v_myall_name}
gzip ${v_db_name}_monitor_${v_time}.log
gzip ${v_db_name}_myall_${v_time}.log
find $v_log_path -mmin +360 -name "${v_db_name}_myall*.log.gz" -exec rm -rf {} \;
find $v_log_path -mmin +360 -name "${v_db_name}_monitor*.log.gz" -exec rm -rf {} \;
fi
}

mysqldb02_log(){
v_db_name='mysqldb02'
v_log_path='/home/mysql/doDBA/log/mysqldb02'
v_myall_name=`ls ${v_log_path}|grep dodba_myall`
v_myall_name_new="${v_db_name}_myall_${v_time}.log.gz"
echo $v_myall_name_new
cd $v_log_path
if [ ! -f "$v_myall_name_new" ]; then
mv ${v_log_name} ${v_db_name}_monitor_${v_time}.log
cat ${v_myall_name} > ${v_db_name}_myall_${v_time}.log
echo '' > ${v_myall_name}
gzip ${v_db_name}_monitor_${v_time}.log
gzip ${v_db_name}_myall_${v_time}.log
find $v_log_path -mmin +360 -name "${v_db_name}_myall*.log.gz" -exec rm -rf {} \;
find $v_log_path -mmin +360 -name "${v_db_name}_monitor*.log.gz" -exec rm -rf {} \;
fi
}


mysqldb01_log
mysqldb02_log

 

定时任务切割日志
#cut dodba log every hour
1 */1 * * * /home/mysql/doDBA/scripts/cut_dodba_log.sh > /dev/null 2>&1 &

 

杀会话:
kill -9 `ps -ef|grep doDBA|grep -v grep|awk '{print $2}'`

 

效果信息:

posted @ 2020-12-08 10:01  zetan·chen  阅读(204)  评论(0编辑  收藏  举报