Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ndisk_space.lua
- -- Simple standalone tool to check NDisk storage space
- -- No config files needed - just run it
- local modem = peripheral.find("modem")
- if not modem then error("No modem found") end
- modem.open(5) -- NDisk channel
- -- Load NDisk secret for authentication
- local NDISK_SECRET = nil
- if fs.exists(".ndisk_secret") then
- local f = fs.open(".ndisk_secret", "r")
- NDISK_SECRET = f.readAll()
- f.close()
- end
- print("NDisk Space Monitor")
- print("===================")
- print("Discovering storage nodes...")
- -- Broadcast discovery
- modem.transmit(5, 5, {type = "discovery", secret = NDISK_SECRET})
- local nodes = {}
- local timer = os.startTimer(3)
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "modem_message" and type(p4) == "table" and p4.type == "discovery_response" then
- nodes[p4.id] = true
- elseif event == "timer" and p1 == timer then
- break
- end
- end
- local nodeList = {}
- for id, _ in pairs(nodes) do
- table.insert(nodeList, id)
- end
- if #nodeList == 0 then
- print("No storage nodes found!")
- return
- end
- print("Found " .. #nodeList .. " nodes")
- print("")
- -- Query each node for space
- local totalFree = 0
- local totalUsed = 0
- for _, nodeId in ipairs(nodeList) do
- local requestId = os.epoch("utc")
- modem.transmit(5, 5, {
- targetNode = nodeId,
- action = "getSpace",
- id = requestId,
- secret = NDISK_SECRET
- })
- local spaceTimer = os.startTimer(2)
- while true do
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "modem_message" and type(p4) == "table" and p4.id == requestId then
- if p4.response and p4.response.success then
- local free = p4.response.space or 0
- totalFree = totalFree + free
- print("Node " .. nodeId .. ": " .. math.floor(free/1024) .. " KB free")
- else
- print("Node " .. nodeId .. ": Error")
- end
- break
- elseif event == "timer" and p1 == spaceTimer then
- print("Node " .. nodeId .. ": Timeout")
- break
- end
- end
- end
- print("")
- print("===================")
- print("Total Free: " .. math.floor(totalFree/1024) .. " KB")
- print(" (" .. #nodeList .. " nodes)")
Advertisement
Add Comment
Please, Sign In to add comment