Skip to content

Instantly share code, notes, and snippets.

@Jerry-Fix
Created September 26, 2013 14:51
Show Gist options
  • Save Jerry-Fix/6715307 to your computer and use it in GitHub Desktop.
Save Jerry-Fix/6715307 to your computer and use it in GitHub Desktop.
Replace netstat with ss to implement the low level discovery for zabbix
#!/bin/bash
lines=$(sudo ss -lp | grep $1 | awk '{print $3}' | cut -d: -f2 | sort | wc -l)
if [ "$1" == "mongod" ]
then
portarray=(`sudo ss -lp | grep $1 | awk '{print $3}' | cut -d: -f2 | sort | head -n $(($lines/2)) 2>/dev/null`)
else
portarray=(`sudo ss -lp | grep $1 | awk '{print $3}' | cut -d: -f2 | sort 2>/dev/null`)
fi
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf '\n\t\t{ '
printf "\"{#TCP_PORT}\":\"${portarray[$i]}\" }"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
@huangjunque
Copy link

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment