MtnMCG

ndisk_space.lua

Dec 20th, 2025 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. -- ndisk_space.lua
  2. -- Simple standalone tool to check NDisk storage space
  3. -- No config files needed - just run it
  4.  
  5. local modem = peripheral.find("modem")
  6. if not modem then error("No modem found") end
  7.  
  8. modem.open(5) -- NDisk channel
  9.  
  10. -- Load NDisk secret for authentication
  11. local NDISK_SECRET = nil
  12. if fs.exists(".ndisk_secret") then
  13. local f = fs.open(".ndisk_secret", "r")
  14. NDISK_SECRET = f.readAll()
  15. f.close()
  16. end
  17.  
  18. print("NDisk Space Monitor")
  19. print("===================")
  20. print("Discovering storage nodes...")
  21.  
  22. -- Broadcast discovery
  23. modem.transmit(5, 5, {type = "discovery", secret = NDISK_SECRET})
  24.  
  25. local nodes = {}
  26. local timer = os.startTimer(3)
  27.  
  28. while true do
  29. local event, p1, p2, p3, p4 = os.pullEvent()
  30. if event == "modem_message" and type(p4) == "table" and p4.type == "discovery_response" then
  31. nodes[p4.id] = true
  32. elseif event == "timer" and p1 == timer then
  33. break
  34. end
  35. end
  36.  
  37. local nodeList = {}
  38. for id, _ in pairs(nodes) do
  39. table.insert(nodeList, id)
  40. end
  41.  
  42. if #nodeList == 0 then
  43. print("No storage nodes found!")
  44. return
  45. end
  46.  
  47. print("Found " .. #nodeList .. " nodes")
  48. print("")
  49.  
  50. -- Query each node for space
  51. local totalFree = 0
  52. local totalUsed = 0
  53.  
  54. for _, nodeId in ipairs(nodeList) do
  55. local requestId = os.epoch("utc")
  56. modem.transmit(5, 5, {
  57. targetNode = nodeId,
  58. action = "getSpace",
  59. id = requestId,
  60. secret = NDISK_SECRET
  61. })
  62.  
  63. local spaceTimer = os.startTimer(2)
  64. while true do
  65. local event, p1, p2, p3, p4 = os.pullEvent()
  66. if event == "modem_message" and type(p4) == "table" and p4.id == requestId then
  67. if p4.response and p4.response.success then
  68. local free = p4.response.space or 0
  69. totalFree = totalFree + free
  70. print("Node " .. nodeId .. ": " .. math.floor(free/1024) .. " KB free")
  71. else
  72. print("Node " .. nodeId .. ": Error")
  73. end
  74. break
  75. elseif event == "timer" and p1 == spaceTimer then
  76. print("Node " .. nodeId .. ": Timeout")
  77. break
  78. end
  79. end
  80. end
  81.  
  82. print("")
  83. print("===================")
  84. print("Total Free: " .. math.floor(totalFree/1024) .. " KB")
  85. print(" (" .. #nodeList .. " nodes)")
  86.  
Advertisement
Add Comment
Please, Sign In to add comment