#!/bin/bash
CPU_us=$(vmstat | awk '{print $13}' | sed -n '$p')
CPU_sy=$(vmstat | awk '{print $14}' | sed -n '$p')
CPU_id=$(vmstat | awk '{print $15}' | sed -n '$p')
CPU_wa=$(vmstat | awk '{print $16}' | sed -n '$p')
CPU_st=$(vmstat | awk '{print $17}' | sed -n '$p')

CPU1=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
sleep 5
CPU2=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
IDLE1=`echo $CPU1 | awk '{print $4}'`
IDLE2=`echo $CPU2 | awk '{print $4}'`
CPU1_TOTAL=`echo $CPU1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
CPU2_TOTAL=`echo $CPU2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
IDLE=`echo "$IDLE2-$IDLE1" | bc`
CPU_TOTAL=`echo "$CPU2_TOTAL-$CPU1_TOTAL" | bc`
#echo -e "IDLE2:$IDLE2\nIDLE1:$IDLE1\nCPU2:$CPU2_TOTAL\nCPU1:$CPU1_TOTAL"
#echo -e        "IDLE:$IDLE\nCPU:$CPU_TOTAL"
RATE=`echo "scale=4;($CPU_TOTAL-$IDLE)/$CPU_TOTAL*100" | bc | awk '{printf "%.2f",$1}'`

#echo -e "us=$CPU_us\tsy=$CPU_sy\tid=$CPU_id\twa=$CPU_wa\tst=$CPU_st"
#echo "CPU_RATE:${RATE}%"
CPU_RATE=`echo $RATE | cut -d. -f1`
#echo   "CPU_RATE:$CPU_RATE"
if      [ $CPU_RATE -ge 90 ]
then    echo "CPU告警：CPU使用率已经超过90%!"
        ps aux | grep -v USER | sort -rn -k3 | head
else echo -e "\033[32mCPU使用率为${RATE}%，处于正常状态!\033[0m"
fi

total=$(free -m | sed -n '2p' | awk '{print $2}')
used=$(free -m | sed -n '2p' | awk '{print $3}')
free=$(free -m | sed -n '2p' | awk '{print $4}')
shared=$(free -m | sed -n '2p' | awk '{print $5}')
buff=$(free -m | sed -n '2p' | awk '{print $6}')
cached=$(free -m | sed -n '2p' | awk '{print $7}')
rate=`echo "scale=2;$used/$total" | bc | awk -F. '{print $2}'`
echo -e "total\tused\tfree\tshared\tbuffer\tavailable"
echo -e "${total}M\t${used}M\t${free}M\t${shared}M\t${buff}M\t${cached}M\nrate:${rate}%"
if    [ $rate -ge 80 ]
then    echo "Memory内存告警：内存使用率超过80%！"
    ps aux | grep -v USER | sort -rn -k4 | head
else echo -e "\033[32m 内存使用率为${rate}%，处于正常状态！ \033[0m"
fi

DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
for I in $DEV
do dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`
mount=`df -Ph | grep $I | awk '{print $6}'`
echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
F=`echo $rate | awk -F% '{print $1}'`
if [ $F -ge 90 ];then
    echo "$mount 磁盘告警：磁盘利用率超过90%！"
else
    echo -e "\033[32m $I磁盘使用率为${rate},正常使用中 \033[0m" 
fi
done

tmp_file=`curl -X GET '127.0.0.1:8848/nacos/v1/ns/service/list?pageNo=1&pageSize=20'`
echo $tmp_file
str2="blade-system blade-auth blade-gateway blade-resource  mes-product mes-warehouse mes-admin mes-qms"
for i in $str2
do
res=$(echo $tmp_file | grep "$i")
if [[ "$res" != "" ]]
x_tag=`docker ps -a | grep $i |awk -F ':' '{print $2}' |awk -F ' ' 'NR==1{print $1}'`
x_id=`docker ps -a | grep $i |awk -F ' ' 'NR==1{print $1}'`
x_created_date=`docker inspect --format '{{ .Created }}' $x_id |xargs -I{} date --date={} +%Y-%m-%d_%H:%M:%S`
then
    echo -e "\033[93m $i已在nacos中注册，$i启动正常, 当前版本:$x_tag, 容器创建时间:$x_created_date \033[0m"
else
    echo -e "\033[31m $i未在nacos中注册，$i启动异常, 当前版本:$x_tag, 容器创建时间:$x_created_date \033[0m"
fi
done
