redis-nginx-consul-rocketmq-主机巡检脚本

架构介绍:

6台服务器(192.55.11.1 192.55.11.2 192.55.11.3 192.55.11.4 192.55.11.5 192.55.11.6)

192.55.11.1 192.55.11.2 只需巡检主机的cpu、内存、磁盘

192.55.11.3需巡检主机的cpu、内存、磁盘,Nginx的连接数

192.55.11.4 192.55.11.5需巡检主机的cpu、内存、磁盘,consul日志、redis哨兵状态、redis内存使用率、redis碎片利用率

192.55.11.6 需巡检主机的cpu、内存、磁盘,redis哨兵状态、redis内存使用率、redis碎片利用率、rocketmq集群状态

脚本只需放在192.55.11.6执行,自动登录到其他服务器上做巡检,对于重复项的巡检使用函数方式简化脚本

 

------------------------------------------脚本正文-------------------------------------------------------------------------------

#!/bin/bash
#

host_ip="192.55.11.1 192.55.11.2 192.55.11.3 192.55.11.4 192.55.11.5 192.55.11.6"
current_time=$(date '+%Y%m%d%H%M%S')
check_logfile="/tmp/${current_time}_check.log"


declare -A redis_status_dict
declare -A redis_sentinel_status_dict

cpu_men_public(){
data=$1

men_usage=$(printf "%s" "$data"|awk 'NR==2{print $3}')
mem_total=$(printf "%s" "$data"|awk 'NR==2{print $2}')
mem_percent=$(printf "%s" "$data"|awk 'NR==2{printf "%.2f", ($3/$2)*100}')
echo "Menory Usage: $men_usage MB / $mem_total MB ($mem_percent%)">>$check_logfile

cpu_usage=$(printf "%s" "$data"|awk NR==4'{print $2 + $4}')
cpu_percent=$(printf "%.2f" $cpu_usage)
echo "CPU Usage: $cpu_percent%">>$check_logfile

}

redis_status_public(){
data=$1
if [ $(printf "%s" "$data"|awk 'NR==5{print}') -eq 1 ];then
redis_status_dict["$1"]="redis-server is running"
else
redis_status_dict["$1"]="redis-server is down"
fi

if [ $(printf "%s" "$data"|awk 'NR==6{print}') -eq 1 ];then
redis_sentinel_status_dict["$1"]="redis-sentinel is running"
else
redis_sentinel_status_dict["$1"]="redis-sentinel is down"
fi

}

cpu_men_disk_satus_6(){
men_usage=$(free -m|awk 'NR==2{print $3}')
mem_total=$(free -m|awk 'NR==2{print $2}')
mem_percent=$(free -m|awk 'NR==2{printf "%.2f", ($3/$2)*100}')

echo "Menory Usage: $men_usage MB / $mem_total MB ($mem_percent%)">>$check_logfile

cpu_usage=$(top -bn1|grep "Cpu(s)"|awk '{print $2 + $4}')
cpu_percent=$(printf "%.2f" $cpu_usage)
echo "CPU Usage: $cpu_percent%">>$check_logfile

echo -e "Disk Usage:\n $(df -h)">>$check_logfile

if [ $(ps -aef|grep redis-server|grep -v grep|wc -l) -eq 1 ];then
redis_status_dict["192.55.11.6"]="redis-server is running"
else
redis_status_dict["192.55.11.6"]="redis-server is down"
fi

if [ $(ps -aef|grep redis-sentinel|grep -v grep|wc -l) -eq 1 ];then
redis_sentinel_status_dict["192.55.11.6"]="redis-sentinel is running"
else
redis_sentinel_status_dict["192.55.11.6"]="redis-sentinel is down"
fi
}

cpu_men_disk_status_other(){

if [ $1 == "192.55.11.3" ];then
data=$(ssh -p 10022 root@$1 "free -m;top -bn1|grep Cpu;echo `netstat -nat|grep :29030|wc -l`+`netstat -nat|grep :29010|wc -l`+`netstat -nat|grep :29011|wc -l`|bc;df -h")

cpu_men_public $data

echo -e "Disk Usage:\n $(printf "%s" "$data"|awk 'NR>5{print}')">>$check_logfile

nginx_current_connections=$(printf "%s" "$data"|awk NR==5'{print}')

elif [ $1 == "192.55.11.4" ] || [ $1 == "192.55.11.5" ] ;then

if [ $1 == "192.55.11.4" ];then

data=$(ssh -p 10022 root@$1 "free -m;top -bn1|grep Cpu;ps -aef|grep redis-server|grep -v grep|wc -l;ps -aef|grep redis-sentinel|grep -v grep|wc -l;tail -192 /u01/hzwq/sbin/consul/log/consul.log;df -h")
cpu_men_public $data
echo -e "Disk Usage:\n $(printf "%s" "$data"|awk 'NR>26{print}')">>$check_logfile
consulLog=$(printf "%s" "$data"|awk 'NR>=7 && NR<=26{print}')
redis_status_public $data

else
data=$(ssh -p 10022 root@$1 "free -m;top -bn1|grep Cpu;ps -aef|grep redis-server|grep -v grep|wc -l;ps -aef|grep redis-sentinel|grep -v grep|wc -l;df -h")
cpu_men_public $data
echo -e "Disk Usage:\n $(printf "%s" "$data"|awk 'NR>6{print}')">>$check_logfile
redis_status_public $data
fi

else
data=$(ssh -p 10022 root@$1 "free -m;top -bn1|grep Cpu;df -h")
cpu_men_public $data
echo -e "Disk Usage:\n $(printf "%s" "$data"|awk 'NR>4{print}')">>$check_logfile

fi
}

main(){
echo " current time: $(date +'%Y-%m-%d %T') ">>$check_logfile
for ip in $host_ip;do
echo "************************************************host: $ip Resources*****************************************">>$check_logfile

if [ $ip != "192.55.11.6" ];then
cpu_men_disk_status_other $ip
else
cpu_men_disk_satus_6
fi

done

#nginx
echo "**************************************************nginx******************************************************">>$check_logfile
echo "nginx current connections Usage: ${nginx_current_connections} / 4096 ($(printf "%.2f" `echo "scale=4;${nginx_current_connections}/4096*100"|bc`)%)">>$check_logfile

#consul
echo "**************************************************consul*****************************************************">>$check_logfile
echo $consulLog>>$check_logfile

#rocketMQ
echo "*************************************************rocketMQ****************************************************">>$check_logfile
/u01/rocketmq/bin/./mqadmin clusterList -n 192.55.11.6:29876>>$check_logfile

#Redis
echo "***************************************************Redis*****************************************************">>$check_logfile

for redis_key in "${!redis_status_dict[@]}"
do
echo " $redis_key ${redis_status_dict[$redis_key]} ">>$check_logfile
done

for redis_sentinel in "${!redis_sentinel_status_dict[@]}"
do
echo " $redis_sentinel ${redis_sentinel_status_dict[$redis_sentinel]} ">>$check_logfile
done

echo "redis-master memory used:">>$check_logfile
redis_pwd=$(cat /etc/redis.conf |grep ^requirepass|awk '{print $2}')
info_memory=`redis-cli -h 192.55.11.6 -p 26379 -a $redis_pwd info memory`
echo $(echo "$info_memory"|grep used_human:)>>$check_logfile
echo $(echo "$info_memory"|grep used_memory_rss_human:)>>$check_logfile
echo $(echo "$info_memory"|grep mem_fragmentation_ratio:)>>$check_logfile

}

 

posted @ 2024-05-07 10:15  瘦阿瘦  阅读(63)  评论(0编辑  收藏  举报