动态转换bash命令输出转换成JSON和服务它通过HTTP [英] Converting a bash command output into JSON and serving it over http on the fly
问题描述
我想的输出命令转换ifstat
成JSON和服务于它通过HTTP动态用于一个JavaScript图形应用程序。是否有任何轻量级 - awk或者sed - 我可以使用命令行的解决方案?我不想JSON输出存储磁盘上,这将是一件好事,如果Web服务器是一个小型轻量化的命令行工具,到了我可以通过管道JSON输出。
I want to convert the output of ifstat
command into JSON and serve it over http on the fly to be used for a javascript graph app. Are there any lightweight -- sed or awk -- command-line solutions which I can use? I do not want to store JSON output on the disk and it would be good if the web-server was a small lightweight command line tool into which I can pipe JSON output.
编辑1:
这是将使用这些数据直播图表库。我并不热衷特定的Web服务器上; ,没有工作的任何Web服务器就可以了。
This is the live streaming chart library which will use the data. I'm not keen on a specific web server; any webserver that does the job would be fine.
推荐答案
这是我都试过了。
ifstat -n | awk 'NR>2{print systime(),$0; fflush()}' | tee ifstat.log
终端#2
while :
do
{
echo -e "HTTP/1.1 200 OK"
echo -e "Content-Type: application/json\n"
tail -n1 ifstat.log | awk '{ printf("{\"time\":%s, \"in\":%s, \"out\":%s}\n", $1, $2, $3) }'
} | nc -l 8000
done
火狐
{"time":1332052321, "in":1.24, "out":2.62}
我知道一点关于 JSON
。也许输出无效。你应该重写 AWK
命令。
I know little about JSON
. Maybe the output is invalid. You should rewrite the awk
command.
这篇关于动态转换bash命令输出转换成JSON和服务它通过HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!