Store output diskspace df -h JSON

后端 未结 4 1322
不知归路
不知归路 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"
        }
      ]
    }
    
    0 讨论(0)
  • 2021-01-07 03:06

    Alternative 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%"
          }
       ]
    }
    
    0 讨论(0)
  • 2021-01-07 03:07

    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
        ]'
    
    0 讨论(0)
  • 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"
        }
      ]
    }
    
    0 讨论(0)
提交回复
热议问题