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

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

当前标题:zabbixredis自动发现端口的脚本,返回json格式
文章路径:http://azwzsj.com/article/gjesoi.html