Issue
I am attempting to gather basic disk space information from a server using a bash script, and store the output in JSON format. I am looking to record the available & used disk space.
An example output of 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
As an example this is how I would like the final output to look.
{
"diskarray": [{
"mount": "/dev/disk1",
"spacetotal": "35GB",
"spaceavail": "1GB"
},
{
"mount": "/dev/disk2",
"spacetotal": "35GB",
"spaceavail": "4GB"
}]
}
So far I've tried using awk:
df -P -B 1 | grep '^/' | awk '{ print $1" "$2" "$3";" }'
with the following output:
/dev/mapper/nodequery--vg-root 47710605312 1439592448;
/dev/sda2 247772160 48645120;
/dev/sda1 535805952 3538944;
But I'm not sure how I take that data and store it in the JSON format.
Solution
The following does what you want, with the only requirement external to bash being a Python interpreter:
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"
An alternate implementation using jq
might look like this:
df -Ph | \
jq -R -s '
[
split("\n") |
.[] |
if test("^/") then
gsub(" +"; " ") | split(" ") | {mount: .[0], spacetotal: .[1], spaceavail: .[2]}
else
empty
end
]'
Answered By - Charles Duffy Answer Checked By - Gilberto Lyons (WPSolving Admin)