Store output diskspace df -h JSON

后端 未结 4 1335
不知归路
不知归路 2021-01-07 02:34

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 & use

4条回答
  •  我在风中等你
    2021-01-07 03:00

    You can do:

    $ 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"
        }
      ]
    }
    

提交回复
热议问题