#!/usr/bin/luaweb
json = require("json")
omg = require("omg")

local names={}
names[1]="filessystem"
names[2]="size"
names[3]="used"
names[4]="available"
names[5]="percentused"
names[6]="mountpoint"

function execute(cmd)
    local f = assert(io.popen(cmd, 'r'))
    local s = assert(f:read('*a'))
    f:close()
    return s
end

function output2TreeTable(output,delimiter)
    local returnTable={}
    local mySplit={}
    local line
    local first=true
    local nameindex=1
    local index=1

    local t = returnTable

    for line in output:gmatch("[^\n\r]+")do
       if(line ~= nil and first==false) then
          returnTable[index]={}
          nameindex=1
          for v in string.gmatch(line, "[^" .. delimiter .. "]+") do
             returnTable[index][names[nameindex]]=v
             nameindex=nameindex+1
          end
          index=index+1
       end
       first=false
    end
    return returnTable
end

local output = execute("df -h -P")
local ret = output2TreeTable(output," ")

omg.runScript(ret,arg,"collects file system information\n")


