通用巡检脚本

#COUNT()统计日志内关键字出现次数,默认前一天,每出现一次为一次成功业务,$1指定前几天
#!/bin/bash
. /etc/init.d/functions

LINE="\e[1;37m+-----------------------------------------------------------------+\e[0m"
LOG=/opt/log/`date +%Y%m%d`.log
[ -f /opt/log ] || mkdir -p /opt/log
LOG_URI="/opt/nginx/log/"
K=${1-1}

M="10.1.193.97"
S="172.31.112.173"
IP=`ifconfig |awk -F '[ :]+' 'NR==10{print $3}'`

CPU_MEM_DISK(){
  echo -e ${LINE}
  echo -e "CPU、内存、硬盘"

  CPUS=`top -n1|awk -F "," 'NR==3{print $4}'`
  MEM=`free -m|awk -v OFS="/" 'NR==2{print $2,($3+$7)}'`
  DISK=`df|awk '{if(NR>1){print $4}}'|sort -nr|tail -n1`
  DISK2=`df|awk '{if(NR>1){print $5}}'|sort -nr|head -n1`

  top -n1 -b|head -n 4 >> ${LOG}
  free -m >> ${LOG}
  df -h >> ${LOG}

  echo -e "CPU 未使用百分比为 ${CPUS} ;\n内存总空间与剩余空间为 {MEN};\n磁盘最小剩余空间为`expr ${DISK} /
 1024`M;最高使用率为 ${DISK2}\n"
  echo -e ${LINE}
}

PORTS_CHECK(){
  echo -e ${LINE}
  echo -e "端口状态:"

PORTS="
80
443
22
"

for i in ${PORTS};do
  netstat -anoplt|grep ${i} >> ${LOG} && action ${i} || action "${i} is not work !!!" /bin/false
done
}

PROCESS_CHECK(){
PORCESS="
docker
sshd
"
echo -e ${LINE}
echo -e "进程状态:"

J=0
for i in ${PORCESS};do
  ps -ef |grep ${i}|grep -v grep >> ${LOG} &&action ${i};let J=J+1 ||action ${i} /bin/false
done
  echo -e "目标进程存活${J}个"
}

MYSQL_CHECK(){
echo -e ${LINE}
echo -e "数据库主从状态:"

if [[ ${IP} == ${M} ]];then
  mysql -uroot -p123456 -e "show master status;"
elif [[ ${IP} == ${S} ]];then
   mysql -uroot -p123456 -e "show slave status\G;" |grep -E "Master_Log_File|Read_Master_Log_Pos|Slave_IO
_Running|Last_Err|Seconds_Behind_Master"
else
  echo "IP设置有问题"
fi
}

COUNT(){
  J=1
  while [ $J -le ${K} ];do
    DAY=`date -d "$J days ago" +%Y-%m-%d`
    SUM=0
    for i in ${LIST};do
      NUM=`cat ${i}|grep ${DAY}|grep -E "GET|key2..."|wc -l`
      let SUM=${SUM}+${NUM}
    done
    let J++
    echo "SUM ${DAY} is ${SUM}"
  done
#  echo "SUM is ${SUM}"
}
if [[ ${IP} == ${M} ]];then
CPU_MEM_DISK
PORTS_CHECK
PROCESS_CHECK
MYSQL_CHECK
echo -e ${LINE}
elif [[ ${IP} == ${S} ]];then
CPU_MEM_DISK
MYSQL_CHECK
echo -e ${LINE}
else
  echo "未取到设置的IP"
fi

echo -e "\e[?25h"
echo -e "\e[?25h" >> ${LOG}
echo -e ${LINE} >> ${LOG}

 

posted @ 2023-05-30 10:37  天际之上可有蓝天  阅读(111)  评论(0编辑  收藏  举报