我试图在家里的路由器上运行一个脚本来发送接口流量到我的InfluxDB。路由器没有curl功能,所以我使用WGET来发送数据。我的脚本看起来是这样的,但它目前是以字符串的形式发送数据--所以,我无法对数据进行数学运算。
RxOctets=`cat /proc/net/dev | grep eth2.2 | awk '{print $2}'`
#echo $RxOctets
TxOctets=`cat /proc/net/dev | grep eth2.2 | awk '{print $10}'`
#echo $TxOctets
wget --header="Accept-Encoding: gzip, deflate" --header='Accept-Charset: UTF-8' --header='Content-Type: application/json' -O response.json --post-data 'routerstats,host=10.10.10.1 rxOctets="'${RxOctets}'",txOctets="'${TxOctets}'"' http://10.10.10.2:9086/write\?db\=telegraf
我试着在变量后面加了一个 "i",以迫使influx把它看作是interger,正如他们的文档中所指出的那样,但它所做的只是在变量后面插入一个i字母。
我试图改变这种语法,我得到的结果是
wget: server returned error: HTTP/1.1 400 Bad Request
我肯定是语法问题,只是搞不清楚哪里出了问题。
将命令设置为变量,然后执行eval $command应该可以解决这个问题。
RxOctets=`cat /proc/net/dev | grep eth2.2 | awk '{print $2}'`
TxOctets=`cat /proc/net/dev | grep eth2.2 | awk '{print $10}'`
Data="routerstats,host=10.10.10.1 RxOctets=${RxOctets},TxOctets=${TxOctets}"
Command="wget --header=\"Accept-Encoding: gzip, deflate\" --header='Accept-Charset: UTF-8' --header='Content-Type: application/json' -O response.json --post-data \"$Data\" http://10.10.10.2:9086/write\?db\=telegraf"
eval "$Command"