Skip to content

Instantly share code, notes, and snippets.

@cj1324
Last active December 19, 2015 15:08
Show Gist options
  • Save cj1324/5973784 to your computer and use it in GitHub Desktop.
Save cj1324/5973784 to your computer and use it in GitHub Desktop.
xively.com push port 9494 status
#!/usr/local/bin/bash
if [ ${1}x = "testx" ];then
set -x
fi
PATH=$PATH:/usr/local/bin/
FEEDID="1302940704"
APIKEY="5hlDL4RAdtM0NjSYovKrAJPqD2rY1yKDTQ3vkfvHBimPI9Ku"
APIURL="http://api.xively.com/v2/feeds/"${FEEDID}
APIHEADER="X-ApiKey: "${APIKEY}
TEMPDIR="/tmp/"
JSONFILE=${TEMPDIR}"temp_put.json"
echo -n "{\"datastreams\":[" >>${JSONFILE};
netstat -anf inet -p tcp |awk 'BEGIN{ getline;getline;arr["CLOSED"]=0;arr["SYN_SENT"]=0;arr["CLOSE_WAIT"]=0;arr["LISTEN"]=0; arr["SYN_RCVD"]=0;arr["ESTABLISHED"]=0;arr["TIME_WAIT"]=0;arr["CLOSING"]=0;arr["FIN_WAIT_1"]=0;arr["FIN_WAIT_2"]=0;arr["LAST_ACK"]=0;} {arr[$NF]+=1; arr["ALL-TOTAL"]+=1;} END{ for ( i in arr){print i,arr[i]};}'| while read line
do
cid=`echo $line | cut -d " " -f 1`;
cval=`echo $line | cut -d " " -f 2`;
echo -n "{\"id\":\""TCP-STAT-${cid}"\",\"current_value\":\""${cval}"\"}" >>${JSONFILE};
echo -n ",">>${JSONFILE};
done
conn=`netstat -an -p tcp | grep '112.30.9494' | wc -l`
echo -n "{\"id\":\""ALL_CONN"\",\"current_value\":\""${conn}"\"}" >>${JSONFILE};
client=`netstat -p tcp -an | grep '112.30.9494' | grep -v LISTEN | awk 'BEGIN{num=0 } { dc=NF-1;sub(/\.[[:digit:]]+$/,"",$dc);arr[$dc]+=1} END{for (i in arr){num+=1};print num}'`
echo -n ",">>${JSONFILE};
echo -n "{\"id\":\""ALL_CLIENT"\",\"current_value\":\""${client}"\"}" >>${JSONFILE};
echo "]}" >>${JSONFILE};
if [ ${1}x = "testx" ];then
cat ${JSONFILE}
else
curl --request PUT --data-binary @${JSONFILE} -A 'HanChen Customize cURL UA' --header "$APIHEADER" $APIURL
echo "curl ret:$? data:$(date) "
fi
rm -f ${JSONFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment