作者:玉乔嘉芸孟峰 | 来源:互联网 | 2023-09-25 12:45
需求:由于redis上云,需要统计一批主机上面的主机内存和redis实例内存使用情况。 #!binbashrm-rfredis_mem.logps-ef|grepredis-ser
需求:
由于redis上云,需要统计一批主机上面的主机内存和redis实例内存使用情况。
#!/bin/bash
rm -rf redis_mem.log
ps -ef |grep redis-server |grep -v grep | awk '$NF~/cluster/{print $(NF-1);next}{print $NF}' |awk -F':' '{print $2}' >redis_ports
for i in `cat redis_ports`
do
redis-cli -p $i info > info.log
used_mem=$(grep -w 'used_memory' info.log | awk -F':' '{print $2}' | sed 's/\r//')
max_mem=$(grep -w 'maxmemory' info.log | awk -F':' '{print $2}' | sed 's/\r//')
#role=$(grep role info.log | awk -F':' '{print $2}' | sed 's/\r//')
printf '%10s\t %10s\t %10s\t\n' $i $used_mem $max_mem >> redis_mem.log
done
host_total_mem=$(free -g | sed -n '2p' | awk '{print $2}')
redis_used_mem=$(cat redis_mem.log | awk '{sum+=$2}END{printf "%.2f",sum/1024/1024/1024}')
redis_max_mem=$(cat redis_mem.log | awk '{sum+=$3}END{print sum/1024/1024/1024}')
redis_host_ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | awk -F"/" '{print $1}')
mem_used_pect=$(awk 'BEGIN{printf "%.2f%",'$redis_used_mem'/'$host_total_mem'*100}')
#printf '%20s\t %10s\t %10s\t %10s\t %10s\n' 主机ip 主机物理内存 redis设置最大内存 本机redis已使用内存 redis使用内存比例
printf '%20s\t %10s\t %10s\t %10s\t %10s\n' $redis_host_ip $host_total_mem $redis_max_mem $redis_used_mem $mem_used_pect
假设以上脚本名test1.sh,将脚本分发到需要check的主机/tmp目录下
ansible -i test1 all -m copy -s -a "src=test1.sh dest=/tmp/test1.sh"
执行结果:ansible -i test1 all -m shell -s -a "sh /tmp/test1.sh" | grep -v 'rc'
192.168.100.10 189 120 14.07 7.44%
192.168.100.11 189 140 16.42 8.69%
192.168.100.12 189 120 14.33 7.58%
192.168.100.13 189 120 14.31 7.57%
192.168.100.14 189 120 14.56 7.70%
192.168.100.15 189 120 14.32 7.58%
192.168.100.16 189 130 15.54 8.22%
192.168.100.17 189 130 15.76 8.34%