How can I get drive capacity using lua

Is there a way using lua to retrieve the remaining space on a LAN drive.

Many thanks

Hello!.

You can use the following Lua code to print the remaining space information of your Vera controller on a .txt file on the /www/ directory.

local file = io.popen("df -h")
instr = file:read("*a")
file:close()
 
local file = io.open("/www/memory.txt", "w")
file:write(instr)
file:close()

You can access this file by typing the IP address of your Vera controller on the URL bar of your web browser followed by a forward slash and the name of the file.

Example:

HTTP://<YouVeraIPAddress>/memory.txt

It will look similar to this on your web browser.

Filesystem                Size      Used Available Use% Mounted on
rootfs                    8.9M      5.7M      3.1M  65% /
/dev/root                 9.8M      9.8M         0 100% /rom
tmpfs                    61.9M      4.0M     57.9M   6% /tmp
/dev/mtdblock6            8.9M      5.7M      3.1M  65% /overlay
overlayfs:/overlay        8.9M      5.7M      3.1M  65% /
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/mtdblock10          64.8M      9.3M     55.4M  14% /storage
/dev/mtdblock10          64.8M      9.3M     55.4M  14% /etc/cmh-firmware
/dev/mtdblock10          64.8M      9.3M     55.4M  14% /etc/cmh-backup
/dev/mtdblock9            7.8M      7.8M         0 100% /mios

I am thinking that you could also copy this file to your LAN device via SFTP or SCP.
I don’t know how you could add this to the LUA code to automate it, to be honest. I am not that well-versed in coding.

local function getDiskSpace(path)
    local function df()
        local format = "(%S+)%s+(%d+)%s+(%d+)%s+(%d+)[^/]+(/%S+)"
        local d = {}
        local p = io.popen "df"
        if p then
            local df = p:read "*a"
            p:close()
            local n = tonumber
            for f, b, u, a, m in df:gmatch(format) do
                d[#d + 1] = {file = f, blocks = n(b), used = n(u), available = n(a), mounted = m}
            end
        end
        return d
    end

    local dfinfo = {}
    for _, d in ipairs(df()) do dfinfo[d.mounted] = d end
    local x = dfinfo[path]
    return {
		total = x.available + x.used,
		available = x.available,
		percentage = (x.available /(x.available + x.used) * 100)
	}
end

local disk = getDiskSpace('/overlay')
luup.log("Total: " .. tostring(disk.total))
luup.log("Available: " .. tostring(disk.available))
luup.log("Percentage: " .. tostring(disk.percentage))

just pass your path and you’re good to go :wink: I’m using it to monitor the disk /overlay partition.

1 Like

Wow. Thanks so much.

Is it possible to get the disk space on a LAN drive on the same network as my Vera hub?

Yes. Just pass the path. Get your paths running df command via ssh.

My network drive is running on a rpi on my LAN. How do I determine the path to put into your code? Thanks again.

Thank you so much!.

1 Like

Is the drive mounted? I have mine setup this way, and I’m copying logs, data mine files and backups over there. It’s just under the output of a df command sent via ssh.

I just tried running your code on my Vera Edge and it does print to the log OK

luup_log:0: Total: 9600 <0x6e5b0520>
luup_log:0: Available: 3640 <0x6e5b0520>
luup_log:0: Percentage: 37.916666666667 <0x6e5b0520>

But then what can you do with it ?

Could you create a virtual device in Vera to display that information ?

Or enable Multi-System Reactor to see those values / variables ?

Thanks

Yes you can. I’m pushing it every day to my SysMon device, where I’m reading it.

SysMon the plugin ?

Yep. I’m running a modified version of it.

Best Home Automation shopping experience. Shop at getvera!

© 2021 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules