zabbix redis自动发现端口的脚本返回json格式
程序员文章站
2022-07-09 16:43:08
在我们进行自动发现的时候,总会有一个脚本来获取端口等信息,并返回json格式,那以redis为例,下边是redis进行进行自动发现的脚本redisport=(`ps aux|grep -w redis...
在我们进行自动发现的时候,总会有一个脚本来获取端口等信息,并返回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格式,希望对大家有所帮助