代码
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
endpoint="10.0.123.130"
echo stat | nc 127.0.0.1 2181 > tmp.stat
echo wchs | nc 127.0.0.1 2181 > tmp.wchs
echo ruok | nc 127.0.0.1 2181 > tmp.ruok
zookeeper_stat_received=`cat tmp.stat | grep "Received:" | awk '{print $2}'`
zookeeper_stat_sent=`cat tmp.stat | grep "Sent:" | awk '{print $2}'`
zookeeper_stat_connections=`cat tmp.stat | grep "Connections:" | awk '{print $2}'`
zookeeper_stat_outstanding=`cat tmp.stat | grep "Outstanding:" | awk '{print $2}'`
zookeeper_stat_nodecount=`cat tmp.stat | grep "Node count:" | awk '{print $3}'`
zookeeper_wchs_connections=`cat tmp.wchs | head -n1 | awk '{print $1}'`
zookeeper_wchs_watchingpaths=`cat tmp.wchs | head -n1 | awk '{print $4}'`
zookeeper_wchs_totalwatches=`cat tmp.wchs | grep 'Total watches' | awk -F\: '{print $2}'`
zookeeper_ruok=`cat tmp.ruok | grep 'imok' | wc -l`
rm -f tmp.stat tmp.wchs tmp.ruok
endpoint="10.0.123.130"
timenow=`date +%s`
function upload() {
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://127.0.0.1:1988/v1/push -vv --data @<(cat <<EOF
[{
"metric": "$name", "endpoint": "$endpoint", "timestamp": $timenow, "step": 60,"value": "$value", "counterType": "$type", "tags": ""
}]
EOF
)
}
name="zookeeper_stat_received"
value="$zookeeper_stat_received"
type="COUNTER"
upload
name="zookeeper_stat_sent"
value="$zookeeper_stat_sent"
type="COUNTER"
upload
name="zookeeper_stat_connections"
value="$zookeeper_stat_connections"
type="GAUGE"
upload
name="zookeeper_stat_outstanding"
value="$zookeeper_stat_outstanding"
type="COUNTER"
upload
name="zookeeper_stat_nodecount"
value="$zookeeper_stat_nodecount"
type="GAUGE"
upload
name="zookeeper_wchs_connections"
value="$zookeeper_wchs_connections"
type="GAUGE"
upload
name="zookeeper_wchs_watchingpaths"
value="$zookeeper_wchs_watchingpaths"
type="GAUGE"
upload
name="zookeeper_wchs_totalwatches"
value="$zookeeper_wchs_totalwatches"
type="GAUGE"
upload
name="zookeeper_ruok"
value="$zookeeper_ruok"
type="GAUGE"
upload
配置
指标解释
报警设置
关键技术
curl发送带有变量的json
function upload() {
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://127.0.0.1:1988/v1/push -vv --data @<(cat <<EOF
[{
"metric": "$name", "endpoint": "$endpoint", "timestamp": $timenow, "step": 60,"value": "$value", "counterType": "$type", "tags": ""
}]
EOF
)
}
- 必须带有[],要不openfalcon会decode error