Store output diskspace df -h JSON

后端 未结 4 1332
不知归路
不知归路 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:13

    Xidel, together with some XQuery magic can do what you want (I used your df -h output).

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

提交回复
热议问题