#CPU、内存、硬盘部分,部分awk取值需要根据实际情况修改
#进程需要选择要关注的进程
#数据库部分,如无巡检备机状态需要,可修改为 mysql -u -p -h [备机ip] 来远程检查
#会在指定位置生成一个当日的.log文件,内部有命令执行的结果部分
1 #!/bin/bash 2 3 . /etc/init.d/functions 4 5 LINE="\e[1;37m+-----------------------------------------------------------------+\e[0m" 6 LOG=/opt/log/`date +%Y%m%d`.log 7 [ -f /opt/log ] || mkdir -p /opt/log 8 9 M="172.31.112.173" 10 S="10.1.193.97" 11 IP=`ifconfig |awk -F '[ :]+' 'NR==10{print $3}'` 12 13 CPU_MEM_DISK(){ 14 echo -e ${LINE} 15 echo -e "CPU、内存、硬盘" 16 17 CPUS=`top -n1|awk -F "," 'NR==3{print $4}'` 18 MEM=`free -m|awk -v OFS="/" 'NR==2{print $2,($3+$7)}'` 19 DISK=`df|awk '{if(NR>1){print $4}}'|sort -nr|tail -n1` 20 DISK2=`df|awk '{if(NR>1){print $5}}'|sort -nr|head -n1` 21 22 top -n1|head -n 4 >> ${LOG} 23 free -m >> ${LOG} 24 df -h >> ${LOG} 25 26 echo -e "CPU 未使用百分比为 ${CPUS} ;\n内存总空间与剩余空间为 {MEN};\n磁盘最小剩余空间为`expr ${DISK} / 1024`M;最高使用率为 ${DISK2}\n" 27 echo -e ${LINE} 28 } 29 30 PORTS_CHECK(){ 31 echo -e ${LINE} 32 echo -e "端口状态:" 33 34 PORTS=" 35 80 36 443 37 22 38 " 39 40 for i in ${PORTS};do 41 netstat -anoplt|grep ${i} >> ${LOG} && action ${i} || action "${i} is not work !!!" /bin/false 42 done 43 } 44 45 PROCESS_CHECK(){ 46 PORCESS=" 47 docker 48 sshd 49 " 50 echo -e ${LINE} 51 echo -e "进程状态:" 52 53 J=0 54 for i in ${PORCESS};do 55 ps -ef |grep ${i}|grep -v grep >> ${LOG} &&action ${i};let J=J+1 ||action ${i} /bin/false 56 done 57 echo -e "目标进程存活${J}个" 58 } 59 60 MYSQL_CHECK(){ 61 echo -e ${LINE} 62 echo -e "数据库主从状态:" 63 64 if [[ ${IP} == ${M} ]];then 65 mysql -uroot -p123456 -e "show master status;" 66 elif [[ ${IP} == ${S} ]];then 67 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" 68 else 69 echo "IP设置有问题" 70 fi 71 } 72 73 if [[ ${IP} == ${M} ]];then 74 CPU_MEM_DISK 75 PORTS_CHECK 76 PROCESS_CHECK 77 MYSQL_CHECK 78 echo -e ${LINE} 79 elif [[ ${IP} == ${S} ]];then 80 CPU_MEM_DISK 81 MYSQL_CHECK 82 echo -e ${LINE} 83 else 84 echo "未取到设置的IP" 85 fi 86 87 echo -e "\e[?25h" 88 echo -e "\e[?25h" >> ${LOG}
89 echo -e ${LINE} >> ${LOG}
标签:脚本,巡检,通用,LOG,IP,CPU,echo,LINE,CHECK From: https://www.cnblogs.com/zy2271/p/17442586.html