首页 > 数据库 >redis-nginx-consul-rocketmq-主机巡检脚本

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

时间:2024-05-07 10:23:35浏览次数:15  
标签:status grep consul redis cpu nginx 192.55 check

架构介绍:

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

}

 

标签:status,grep,consul,redis,cpu,nginx,192.55,check
From: https://www.cnblogs.com/yangx124/p/18176651

相关文章

  • docker-compose+nginx实现服务不中断更新
    思路是写一个脚本,docker-composescale扩容,然后重写nginx配置文件,刷新负载均衡nginx重写的配置文件,文件名service,里面只有upstream,例如:upstreame-chatbot-server-dev{server0.0.0.0:8080;server0.0.0.0:8081;}脚本使用方法:./rollupdate.sh--service=test-serve......
  • Redisson的看门狗机制底层实现
    1.看门狗机制概述看门狗机制是Redission提供的一种自动延期机制,这个机制使得Redission提供的分布式锁是可以自动续期的。privatelonglockWatchdogTimeout=30*1000;1看门狗机制提供的默认超时时间是30*1000毫秒,也就是30秒如果一个线程获取锁后,运行程序到释放锁所花费的时......
  • 宝塔Linux面板redis服务开多个端口
    原文:https://blog.csdn.net/weixin_38272324/article/details/126421073首先找到redis文件夹 ,找到redis.conf文件,复制一份,重命名为redis_6380.conf;#进入redis文件夹cd/www/server/redis#编辑文件vimredis_6380.conf#按i进入insert模式#修改内容#93行port6380#159......
  • redis自学(41)JVM进程缓存
      前置数据库的表规划:数据库的表尽量做分离,是因为一个商品的数据事实上是非常多的,需要对商品的数据进行解耦,字段太多查询的效率会比较低;另一方面,系统需要给数据加缓存,如果都在一张表里,那么作为缓存的话,就只有一整条数据作为缓存,一旦一条数据里的任意一个字段做了修改,整个商品......
  • Redis开源社区持续壮大,华为云为Valkey项目注入新的活力
    摘要:作为Valkey社区的TechnicalSteeringCommitteemember,华为云将持续参与社区建设。一、背景今年3月21日,RedisLabs宣布从Redis7.4版本开始,将原先比较宽松的BSD源码使用协议修改为RSAv2和SSPLv1协议,意味着Redis在OSI(开放源代码促进会)定义下不再是严格的开源产品。Redis......
  • Redis多数据源,看这篇就够了
    背景Redis多数据源常见的场景:分区数据处理:当数据量增长时,单个Redis实例可能无法处理所有的数据。通过使用多个Redis数据源,可以将数据分区存储在不同的实例中,使得数据处理更加高效。多租户应用程序:对于多租户应用程序,每个租户可以拥有自己的Redis数据源,以确保数据隔离和安全性......
  • uwsgi+nginx启动Django静态文件设置
    总体思路:设置好STATIC_ROOT后使用pythonmanager.pycollectstatic命令将django下所有的静态文件搜集到STATIC_ROOT下,然后让nginx的static路由指向这个目录1.Django的settings.py文件中设置如下STATIC_URL='/static/'STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles'......
  • Redis基础篇笔记
    一、Redis入门1.认识NoSQL1.1 什么是NoSQLNoSQL最常见的解释是"non-relational",很多人也说它是"NotOnlySQL"NoSQL仅仅是一个概念,泛指非关系型的数据库区别于关系数据库,它们不保证关系数据的ACID特性NoSQL是一项全新的数据库革命性运动,提倡运用非关系型的数据存储,相对于......
  • 测试 springboot 项目苍穹外卖,解决 Unable to connect to Redis 错误问题
       使用IDEA启动springboot项目苍穹外卖后台项目sky-take-out,测试“菜品批量删除”接口时,能够正常完成操作,但是服务器始终显示下面错误信息:2024-05-0320:54:24.134ERROR24360---[nio-8181-exec-3]o.a.c.c.C.[.[.[/].[dispatcherServlet]  :Servlet.service()fo......
  • Redis常用命令
    Redis数据类型: String字符串Lists列表Sets集合Sortedsets有序集合Hashs哈希表 常用命令#插入数据setnamephp#读取getname#删除键值delname#验证键值是否存在existsname#setnx;,设置key对应的值为string类型的value。如果key已经存在,返回......