1、使用shell脚本获取结果值: 0或1
vim /data/python/monitor-port.sh
INPUT=$1
VALUE=`/usr/bin/netstat -ntl | grep ${INPUT} |awk '{print $4}' | awk -F: '{print $2}' | grep "^${INPUT}$"`
if [ $? -eq 0 ];then
echo "1"
else
echo "0"
fi
2、使用Python脚本push数据,添加到计划任务中执行
!/use/bin/env python
import requests
import time
import json
import commands
val = commands.getoutput('bash /data/python/monitor-port.sh 9667')
ts = int(time.time())
payload = [
{
"endpoint": "node1",
"metric": "wf-sms-callback-port",
"timestamp": ts,
"step": 60,
"value": val,
"counterType": "GAUGE",
},
]
r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payload))
print r.text
3、添加计划任务:
*/1 * * * * /usr/bin/python /data/python/push.py
4、添加报警
|