我正在尝试使用bash脚本从服务器收集基本磁盘空间信息,并以JSON格式存储输出。我期待记录可用的&用过的磁盘空间。
df -h的示例输出:
Filesystem Size Used Avail Use% Mounted on
udev 2.0G 4.0K 2.0G 1% /dev
tmpfs 394M 288K 394M 1% /run
/dev/mapper/nodequery--vg-root 45G 1.4G 41G 4% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 2.0G 0 2.0G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda2 237M 47M 178M 21% /boot
/dev/sda1 511M 3.4M 508M 1% /boot/efi
作为一个例子,我希望看到最终输出。
{
"diskarray": [{
"mount": "/dev/disk1",
"spacetotal": "35GB",
"spaceavail": "1GB"
},
{
"mount": "/dev/disk2",
"spacetotal": "35GB",
"spaceavail": "4GB"
}]
}
到目前为止,我尝试过使用awk:
df -P -B 1 | grep '^/' | awk '{ print $1" "$2" "$3";" }'
使用以下输出:
/dev/mapper/nodequery--vg-root 47710605312 1439592448;
/dev/sda2 247772160 48645120;
/dev/sda1 535805952 3538944;
但我不确定如何获取该数据并将其存储为JSON格式。
答案 0 :(得分:2)
以下是您想要的,bash外部唯一的要求是Python解释器:
python_script=$(cat <<'EOF'
import sys, json
data = {'diskarray': []}
for line in sys.stdin.readlines():
mount, avail, total = line.rstrip(';').split()
data['diskarray'].append(dict(mount=mount, spacetotal=total, spaceavail=avail))
sys.stdout.write(json.dumps(data))
EOF
)
df -Ph | awk '/^\// { print $1" "$2" "$3";" }' | python -c "$python_script"
使用jq
的替代实现可能如下所示:
df -Ph | \
jq -R -s '
[
split("\n") |
.[] |
if test("^/") then
gsub(" +"; " ") | split(" ") | {mount: .[0], spacetotal: .[1], spaceavail: .[2]}
else
empty
end
]'
答案 1 :(得分:1)
你可以这样做:
$ df -Ph | awk '/^\// {print $1"\t"$2"\t"$4}' | python -c 'import json, fileinput; print json.dumps({"diskarray":[dict(zip(("mount", "spacetotal", "spaceavail"), l.split())) for l in fileinput.input()]}, indent=2)'
{
"diskarray": [
{
"mount": "/dev/disk1",
"spacetotal": "931Gi",
"spaceavail": "623Gi"
},
{
"mount": "/dev/disk2s2",
"spacetotal": "1.8Ti",
"spaceavail": "360Gi"
}
]
}
答案 2 :(得分:1)
替代Oneliner
$ df -hP | awk 'BEGIN {printf"{\"discarray\":["}{if($1=="Filesystem")next;if(a)printf",";printf"{\"mount\":\""$6"\",\"size\":\""$2"\",\"used\":\""$3"\",\"avail\":\""$4"\",\"use%\":\""$5"\"}";a++;}END{print"]}";}'
{
"discarray":[
{
"mount":"/",
"size":"3.9G",
"used":"2.2G",
"avail":"1.5G",
"use%":"56%"
},
{
"mount":"/dev",
"size":"24G",
"used":"0",
"avail":"24G",
"use%":"0%"
}
]
}
答案 3 :(得分:1)
Xidel和一些XQuery魔术可以完成您想要的操作(我使用了df -h
的输出)。
df -h | xidel -s - --xquery '
{
"diskarray":[
for $x in x:lines($raw)[starts-with(.,"/")]
let $a:=tokenize($x,"\s+")
return {
"mount":$a[1],
"spacetotal":$a[2],
"spaceavail":$a[4]
}
]
}
'
{
"diskarray": [
{
"mount": "/dev/mapper/nodequery--vg-root",
"spacetotal": "45G",
"spaceavail": "41G"
},
{
"mount": "/dev/sda2",
"spacetotal": "237M",
"spaceavail": "178M"
},
{
"mount": "/dev/sda1",
"spacetotal": "511M",
"spaceavail": "508M"
}
]
}