#!/bin/bash
#usage:巡检脚本
#author:zhangzhufeng
#date:2022.03.25

system_information(){
echo "检查时间:`date +%F`"
echo "ip:`ifconfig | awk -F' ' 'NR==2{print $2}'`"
echo "主机名:`hostname`"
}


average_load(){
load1=`uptime | awk -F"[, ]+" '{print $(NF-2)}'`
load5=`uptime | awk -F"[, ]+" '{print $(NF-1)}'`
load15=`uptime | awk -F"[, ]+" '{print $NF}'`
average=`printf "%.2f" "$(echo "scale=2;((${load1}+${load5}+${load15})/3)" | bc)"`
cpu_core=`cat /proc/cpuinfo| grep "cpu cores"| uniq | awk -F":" '{print $2}'`
cpu_number=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
all_cpu_core=`echo "($cpu_core*$cpu_number*2)" | bc`
load_com=`expr "$average > $all_cpu_core" | bc`

if [ "$load_com" -eq 1 ];then
  echo "!!!系统负载过高，请注意:$average!!!"
else
  echo "系统负载正常:$average"
fi
}

mem_status(){
total_mem=`free | awk -F" " 'NR==2{print $2}'`
availabe=`free | awk -F" " 'NR==2{print $7}'`
used=`echo "($total_mem-$availabe)" | bc`
percentused=`printf "%.0f" "$(echo "scale=2;($used/$total_mem)*100" | bc)"`
C6_buffer_cache=$(free  | awk -F" " 'NR==3{print $4}')
c6_used=$(echo "($total_mem-$C6_buffer_cache)" | bc)
c6_percentused=`printf "%.0f" "$(echo "scale=2;(${c6_used}/${total_mem})*100" | bc)"`
version=`cat /etc/redhat-release | awk -F" " '{print $(NF-1)}' | awk -F"." '{print $1}'`


if [ "$version" == "7" ];then
  if [ "$percentused" -ge 70 ];then
    echo "!!!系统内存使用率已达:$percentused%,请注意!!!"
  elif [ "$percentused" -ge 60 ] && [ "$percentused" -lt 70 ];then
    echo "!!系统内存使用率已达:$percentused%,请注意!!"
  elif [ "${percentused}" -ge 50 ] && [ "${percentused}" -lt 60 ];then
    echo "系统内存使用率已达:$percentused%,请注意!"
  else
    echo "系统内存使用率正常:$percentused%"
  fi
elif [ "$version" == "6" ];then
  if [ "$c6_percentused" -ge 70 ];then
    echo "!!!系统内存使用率已达:c6_percentused,请注意!!!"
  elif [ "$c6_percentused" -ge 60 ] && [ "$c6_percentused" -lt 70 ];then
    echo "!!系统内存使用率已达:c6_percentused,请注意!!"
  elif [ "$c6_percentused" -ge 50 ] && [ "$c6_percentused" -lt 60 ];then
    echo "!系统内存使用率已达:c6_percentused,请注意!#"
  else
    echo "系统内存使用率正常:$c6_percentused%"
  fi
fi
}

Disk_status(){
Disk_value=`df -hP | awk 'NR>2{print line}{line=$0} END{print line}' | awk -F" " '{print $5}' | awk -F"%" '{print $1}' | sort -nr | head -1`
Disk_used_70=`df -hP | awk -F"[ %]+" '{if($5>70) print }' | grep -v Filesystem | awk -F" " '{ print "mounted:" $6 ":" $5 " " "Filesys:"$1 }'  | sort -k3 -nr -t":"` 
Disk_used_60=`df -h | awk -F"[ %]+" '{if($5>60) print }' | grep -v Filesystem | awk -F" " '{ print "mounted:" $6 ":" $5 " " "Filesys:"$1 }'  | sort -k3 -nr -t":" `
Disk_used_50=`df -h | awk -F"[ %]+" '{if($5>50) print }' | grep -v Filesystem | awk -F" " '{ print "mounted:" $6 ":" $5 " " "Filesys:"$1 }'  | sort -k3 -nr -t":" `

if [ "$Disk_value" -lt 50 ];then
  echo "各分区使用率正常"
elif [ "$Disk_value" -ge 50 ] && [ "$Disk_value" -lt 60 ];then
  echo "!分区使用率已高于50%,请注意!"
  echo "$Disk_used_50"
elif [ "$Disk_value" -ge 60 ] && [ "$Disk_value" -lt 70 ];then
  echo "!!分区使用率已高于50%,请注意!!"
  echo "$Disk_used_60"
elif [ "$Disk_value" -ge 70 ];then
  echo "!!!分区使用率已高于70%,请注意!!!"
  echo "$Disk_used_70"
fi
}

zombie=`top -b -n  1 | awk -F"[, ]+" 'NR==2{print $(NF-1)}' | awk -F" " 'NR==1{print $1}'`
Zombie_status(){
if [ "$zombie" -eq 0 ];then
  echo "系统不存在僵尸进程"
else
  echo "!!!存在僵尸进程${zombie}个,请注意!!!"
fi
}


log_error(){
log=`grep -i error /var/log/messages`
if [ -n "$log" ];then
  echo "$log"
  echo "!日志存在报错,请检查!"
else
  echo "日志无报错" 
fi
}

cmd(){
echo "+++++++++++++++++++++++++6+2重要系统巡检情况++++++++++++++++++++++++++++"
echo   "   "
echo "--------------------------------检查开始--------------------------------"
sleep 2
echo   "                                                                      "
echo "################################系统信息################################"
sleep 2
system_information
echo "   "
echo "##############################负载负载情况##############################"
sleep 2
average_load 
echo "   "
echo "###############################内存情况#################################"
sleep 2
mem_status
echo "   "
echo "#############################分区使用情况###############################"
sleep 2
Disk_status
echo "   "
echo "#############################僵尸进程情况###############################"
sleep 2
Zombie_status
echo "   "
echo "#############################错误日志情况###############################"
sleep 2
log_error
echo "   "
sleep 2
echo "+++++++++++++++++++++++++检查结束,请关注问题++++++++++++++++++++++++++++"
}

cmd
