答案1
我怎样才能(简单地)将此输出包装在 XML/JSON 上?
假设您不想要total
、used
等的单独标签free
shared
,您可以根据需要将整个输出包装在封闭标签中:
XML
以下脚本可以保存为(例如)memoryinfo-xml.sh
:
#!/bin/bash
# memoryinfo-xml.sh - wrap output of free + vmstat in XML tags
echo "<output>"
echo -e "\t<date>$(date)</date>"
echo -e "\t<free>$(free)</free>"
echo -e "\t<vmstat>$(vmstat)</vmstat>"
echo "</output>"
示例输出:
<output>
<date>Thu 30 Mar 16:21:18 BST 2017</date>
<free> total used free shared buffers cached
Mem: 3853532 3721596 131936 100868 227652 3024584
-/+ buffers/cache: 469360 3384172
Swap: 1182716 2512 1180204</free>
<vmstat>procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 2512 132084 227652 3024584 0 0 3 2 4 4 1 0 99 0 0</vmstat>
</output>
正如您所见,它并不漂亮!
JSON
与之前非常相似,另存为(例如)memoryinfo-json.sh
:
/bin/bash #!/bin/bash #memoryinfo-json.sh - 将 free + vmstat 的输出包装在 json 中 # 感谢 https://stackoverflow.com/a/1252191 提供的 \n 替换 回显“{ \“输出\”:” echo -e "\t { \"date\": \"$(date)\", " echo -e "\t \"free\": \"$(free | sed ':a;N;$!ba;s/\n/||/g')\", " echo -e "\t \"vmstat\": \"$(vmstat| sed ':a;N;$!ba;s/\n/||/g')\" " 回声“}”
示例输出:
{ “输出”: { “日期”:“2017 年 3 月 30 日星期四 16:48:51 BST”, “free”:“已使用的可用共享缓冲区缓存总数 || 内存:3853532 3722428 131104 100868 227888 3024844 || -/+ 缓冲区/缓存:469696 3383836 || 交换:1182716 2512 1180204”, “vmstat”:“procs-- -- -- -- -- -- 内存-- -- -- -- -- -- -- 交换-- -- -- -- io-- -- - 系统-- -- -- --cpu-- -- - || rb swpd 释放 buff 缓存 si so bi bo in cs us sy id wa st || 1 0 2512 131096 227888 3024844 0 0 3 2 4 4 1 0 99 0 0” } }
答案2
我创建了一个名为的工具,可以将许多命令(包括和)jc
的输出转换为 JSON:free
vmstat
$ jc free | jq
[
{
"type": "Mem",
"total": 3993360,
"used": 293248,
"free": 3185992,
"shared": 1196,
"buff_cache": 514120,
"available": 3465280
},
{
"type": "Swap",
"total": 2097148,
"used": 0,
"free": 2097148
}
]
$ vmstat | jc --vmstat | jq
[
{
"runnable_procs": 2,
"uninterruptible_sleeping_procs": 0,
"virtual_mem_used": 0,
"free_mem": 2794468,
"buffer_mem": 2108,
"cache_mem": 741208,
"inactive_mem": null,
"active_mem": null,
"swap_in": 0,
"swap_out": 0,
"blocks_in": 1,
"blocks_out": 3,
"interrupts": 29,
"context_switches": 57,
"user_time": 0,
"system_time": 0,
"idle_time": 99,
"io_wait_time": 0,
"stolen_time": 0,
"timestamp": null,
"timezone": null
}
]
解析器vmstat
还支持流式传输,输出 JSON 行:
$ vmstat 1 | jc --vmstat-s -u | jq -c
{"runnable_procs":2,"uninterruptible_sleeping_procs":0,"virtual_mem_used":0,"free_mem":2794468,"buffer_mem":2108,"cache_mem":741208,"inactive_mem":null,"active_mem":null,"swap_in":0,"swap_out":0,"blocks_in":1,"blocks_out":3,"interrupts":29,"context_switches":57,"user_time":0,"system_time":0,"idle_time":99,"io_wait_time":0,"stolen_time":0,"timestamp":null,"timezone":null}
...
答案3
free
命令解决方案
正常free
输出
$ free -m -w
total used free shared buffers cache available
Mem: 16017 901 476 140 169 14469 14652
Swap: 4095 133 3962
带管道的 Json 输出awk
$ free -m -w | xargs | awk '{print"[{\"type\": \"ram\",\""$1"\": "$9",\""$2"\": "$10",\""$3"\": "$11",\""$4"\": "$12",\""$5"\": "$13",\""$6"\": "$14",\""$7"\": "$15"},{\"type\": \"swap\",\""$1"\": "$17",\""$2"\": "$18",\""$3"\": "$19"}]"}' | jq
[
{
"type": "ram",
"total": 16017,
"used": 901,
"free": 476,
"shared": 140,
"buffers": 169,
"cache": 14469,
"available": 14651
},
{
"type": "swap",
"total": 4095,
"used": 133,
"free": 3962
}
]