1.通用类脚本
#!/bin/bash
#by wangcc
#2019/0307 14:31
date=`date +%Y%m%d`
Now=`date +%Y/%m/%d-%H:%M`
dir=/root/wangcc/monitor/log
mobile=150xxxx5210,183xxxx0681,138xxxx5964
#日志
function writeLog(){
Now=`date +%Y%m%d-%H:%M`
echo "${Now}:$*" >> ${dir}/monitor_${date}.log
chmod 757 ${dir}/monitor_${date}.log
}
#sms
function sms_send(){
mobile_one=`echo ${mobile}|sed 's/,/ /g'`
for sms in ${mobile_one}
do
smswarn ${sms} $1
echo "${Now}:$*,sms_send to [$sms]" >> ${dir}/sms_${date}.log
done
}
2.端口监控脚本
#!/bin/bash
#by wangcc
date=`date +%Y%m%d`
Now=`date +%Y/%m/%d-%H:%M`
dir=/root/wangcc/monitor/log
. ~/monitor/monitor_mobile.sh
#检查端口连接数
function connection_number(){
port_number_one=`netstat -nat | grep -i ${1} |grep ESTABLISHED | wc -l`
pid=`netstat -tlnp|grep dbproxy|awk -F'[:| |/]+' '{if($5=='$1')print $(NF-2)}'`
cpu=`top -b -n 1 |grep dbproxy |grep ${pid} |awk '{print $9}'`
echo "当前端口>>:[ ${1} ] 连接数 >>:[ ${port_number_one} ] CPU 负载 >>:[ ${cpu} ]">>${dir}/monitor_${date}.log
if [[ ${port_number_one} -gt 80 ]] || [[ ${cpu%.*} -gt 85 ]]
then
sms_send "${Now}-->port:[${1}] number:[${port_number_one}] CPU>>:[${cpu}]"
fi
}
port_number=`cat /root/wangcc/monitor/port| wc -l`
host_dbpproxy=`ps -ef | grep -v grep | grep /data/dbsec/dbproxy/bin/dbproxy |wc -l`
for i in `cat port`
do
connection_number ${i}
sleep 1
done
if [[ ${port_number} -eq ${host_dbpproxy} ]]
then
writeLog "当前脱敏端口数量 >>>>>> 25 <<<<<"
elif [[ ${port_number} -ne ${host_dbpproxy} ]]
then
for i in `cat /root/wangcc/monitor/port`
do
writeLog "当前检查 端口 ${i} "
netstat -tlnp| grep ${i}
if [[ $? -eq 0 ]]
then
echo "${i} existence !!" >/dev/null
else
writeLog "-------->>> ${i} is not existence !! <-------------"
sms_send "!!!!!----->port:[${i}] is not existence !!!!! "
fi
done
sh /data/dbsec/dbproxy/script/restart.sh restart
sleep 1
sms_send "${Now}----->restart all port "
echo "`date +%Y/%m/%d-%H:%M`: --------> restart all port <--------" >>/root/wangcc/monitor/port_1.log
writeLog "-------->>> restart all port <-------------"
fi
3.主机状态监控
#!/bin/bash
#by wangcc
date=`date +%Y%m%d`
dir=/root/wangcc/monitor/log
. ~/monitor/monitor_mobile.sh
writeLog "========================================================================="
#check port
cd /root/wangcc/monitor/ && ./monitor_port.sh
#获取磁盘使用率
data_name="/data"
diskUsage=`df -h | grep $data_name | awk -F '[ %]+' '{print $0}'`
#获取内存情况
mem_used=`free -m`
writeLog "-------------------------获取磁盘使用率-------------------------"
writeLog ${diskUsage}
writeLog "-------------------------获取内存情况-------------------------"
writeLog ${mem_used}
- 日志清理压缩脚本
#!/bin/bash
#by wangcc
yesterday=`date -d "1 day ago" +"%Y%m%d"`
dir_log='~/monitor/log'
function writeLog(){
Now=`date +%Y%m%d-%H:%M`
echo "${Now}-->$*" >> ${dir_log}/bak.txt
}
function check_excute(){
if [[ $? -ne 0 ]];then
writeLog "${1} file tar --> fail <--"
exit
else
writeLog "${1} file tar --> success && rm -rf ${1} <--"
fi
}
function tar_clear(){
for file in `ls ${1}`
do
if [[ ${file} =~ "tar.gz" ]];then
writeLog "${file} unwantend tar !! "
else
writeLog " -------> `du -sh ${file}` <-------- "
tar -zcf ${file}_${yesterday}.tar.gz ${file}
check_excute ${file}
rm -rf ${file}
mv ${file}_${yesterday}.tar.gz old_file
writeLog "mv ${file}_${yesterday}.tar.gz ---> old_file"
fi
done
}
function main(){
cd /root/wangcc/monitor/log
tar_clear "monitor_${yesterday}.log"
sleep 20
tar_clear "sms_${yesterday}.log"
sleep 20
cd /data/dbsec/dbproxy/log
tar_clear "errlog_*.${yesterday}*"
sleep 20
tar_clear "errlog.${yesterday}"
writeLog " ========== ${yesterday} file cleaned up !! ========= "
}
main