温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

zabbix redis自动发现端口的脚本,返回json格式

发布时间:2020-07-30 14:25:57 来源:网络 阅读:351 作者:heypon 栏目:系统运维

在我们进行自动发现的时候,总会有一个脚本来获取端口等信息,并返回json格式,那以redis为例,下边是redis进行进行自动发现的脚本

redisport=(`ps aux|grep -w redis-server|grep -v "grep" |awk -F':' '{print $NF}'`)
portnum=${#redisport[@]}

count=`hostname | grep "\-SQ" | wc -l`
if [ $count -eq 1  ];then
address=` /usr/sbin/ip address | grep -w  "inet" | awk -F " " '{print $2}' | egrep "^10."| awk -F"/" '{print $1}'`
else
address=` /usr/sbin/ip address | grep "192.168" | awk '{print $2}' | awk -F "/" '{print $1}'`
fi

printf "{\n"
printf  '\t'"\"data\":["
for ((i=0;i<$portnum;i++))
do
        printf '\n\t\t{\n'
        printf '\t\t\t'
        printf "\"{#REDIS_PORT}\":\"${redisport[$i]}\"",
        printf '\n\t\t\t'
        printf "\"{#REDIS_ADDRESS}\":\"${address}"\",
        printf '\n\t\t\t'
        printf "\"{#REDIS_NAME}\":\"Redis ${address}:${redisport[$i]}\""
        printf '\n\t\t}'
        if [ $i -lt $[$portnum-1] ];then
                printf ','
        fi
done
printf  "\n\t]\n"
printf "}\n"

返回结果如下

{
        "data":[
                {
                        "{#REDIS_PORT}":"6386",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6386"
                },
                {
                        "{#REDIS_PORT}":"6387",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6387"
                },
                {
                        "{#REDIS_PORT}":"6388",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6388"
                },
                {
                        "{#REDIS_PORT}":"6379",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6379"
                },
                {
                        "{#REDIS_PORT}":"6384",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6384"
                },
                {
                        "{#REDIS_PORT}":"6385",
                        "{#REDIS_ADDRESS}":"192.168.43.66",
                        "{#REDIS_NAME}":"Redis 192.168.43.66:6385"
                }
        ]
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI