DDR爱好者之家 Design By 杰米

在我们进行自动发现的时候,总会有一个脚本来获取端口等信息,并返回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"
        }
    ]
}

总结

以上所述是小编给大家介绍的zabbix redis自动发现端口的脚本返回json格式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米