shell脚本监控主机cpu,内存,磁盘使用情况脚本

shell脚本监控主机cpu,内存,磁盘使用情况脚本

#! /bin/bash
cd /home/groupop/checkServer

if [ ! -f hostlist ];then
        exit 0;
fi

ip="";
types="";
> monitor.error.log;

CPU_USED_PCT="93";
MEM_USED_PCT="92";
DISK_USE_PCT="80";

for line in `cat hostlist | grep -v '#'`
do
        ip=`echo "$line" | awk -F ';' '{print $1}'`;
        types=`echo "$line" | awk -F ';' '{print $2}'`;
        ssh groupop@$ip "top -b -c -n 1 && free && df -h |grep -v tmpfs|grep -v boot && ps -ef | grep $types |grep -E 'jdk|zookeeper' |grep -v grep" > monitor.log;
        CPU_IDLE_PCT=`grep "Cpu(s):" monitor.log |awk -F "ni, " '{print($2)}'|awk -F "id," '{print($1)}' |awk -F "." '{print($1)}'`;
        MEM_TOTAL=`grep "Mem:" monitor.log |grep -v total | awk '{print ($2)}'`;
        MEM_FREE=`grep "Mem:" monitor.log |awk -F " " '{print($4+$6)}'`;
        #CPU_USED_ACT=`echo "scale=0;(100-$CPU_IDLE_PCT)/1"|bc`;
        #CPU_USED_ACT_TRUE=`echo "scale=1;100-$CPU_IDLE_PCT"|bc`;
        MEN=$((100-MEM_FREE*100/MEM_TOTAL));
        #if [[ $CPU_USED_ACT -ge $CPU_USED_PCT ]] || [[ $MEN -ge $MEM_USED_PCT ]] ; then
		if [[ $MEN -ge $MEM_USED_PCT ]] ; then
		          echo "[$ip]CPU or MEMORY is over threshold" >> monitor.error.log;
		          echo "===== ===== == result == ===== =====" >> monitor.error.log;
		          #echo "CPU_USED_PCT:$CPU_USED_ACT_TRUE%,Threshold is $CPU_USED_PCT%." >> monitor.error.log;
		          echo "MEM_USED_PCT:$((100-MEM_FREE*100/MEM_TOTAL))%,Threshold is $MEM_USED_PCT%." >> monitor.error.log;
		          echo "===== ===== ===== ===== ===== == [${ip}] basic top infomation == ===== ===== ===== ===== =====" >> 		monitor.error.log;
		          head -n 20 monitor.log >> monitor.error.log;
		          echo -e "
		" >> monitor.error.log;
		          echo -e "
		" >> monitor.error.log;
	        fi

        sed -n '/Filesystem/,$ p' monitor.log > disk_data.log
        for line2 in $(cat disk_data.log |grep -v Avail|grep -v rootfs |grep -v shm |grep -v rocketmq|awk '{print $5}'|awk -F \% '{print $1}')
        do
                if [ $line2 -ge $DISK_USE_PCT ];   then
                        echo "the host[$ip]disk use is over threshold: $(cat disk_data.log|grep $line2\%)" 2>&1 >> monitor.error.log
                fi
        done
        case ${
     types} in
        namesrv)
        res=`cat monitor.log | grep $types | wc -l | awk '{if ($1<1) print("${ip};type=${types} process not exist!")}'`;
        if [ ! "$res"x = "x" ];then
                        echo "$ip; type=$types process not exist!">>monitor.error.log;
        fi;;
        broker)
        pidNum=`cat monitor.log | grep $types | wc -l`;
        if [ $pidNum -lt 1 ];then
                        echo "$ip; kill" >>monitor.error.log;
                         echo "$ip; type=$types process not exist!">>monitor.error.log;
        fi;;
        esac
        
done

cat monitor.error.log;

你可能感兴趣的:(shell脚本)