ahcp.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.ahcp", package.seeall)
  4. function index()
  5. if not nixio.fs.access("/etc/config/ahcpd") then
  6. return
  7. end
  8. entry({"admin", "network", "ahcpd"}, cbi("ahcp"), _("AHCP Server"), 90)
  9. entry({"admin", "network", "ahcpd", "status"}, call("ahcp_status"))
  10. end
  11. function ahcp_status()
  12. local nfs = require "nixio.fs"
  13. local uci = require "luci.model.uci".cursor()
  14. local lsd = uci:get_first("ahcpd", "ahcpd", "lease_dir") or "/var/lib/leases"
  15. local idf = uci:get_first("ahcpd", "ahcpd", "id_file") or "/var/lib/ahcpd-unique-id"
  16. local rv = {
  17. uid = "00:00:00:00:00:00:00:00",
  18. leases = { }
  19. }
  20. idf = nfs.readfile(idf)
  21. if idf and #idf == 8 then
  22. rv.uid = "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" %{ idf:byte(1, 8) }
  23. end
  24. local itr = nfs.dir(lsd)
  25. if itr then
  26. local addr
  27. for addr in itr do
  28. if addr:match("^%d+%.%d+%.%d+%.%d+$") then
  29. local s = nfs.stat(lsd .. "/" .. addr)
  30. rv.leases[#rv.leases+1] = {
  31. addr = addr,
  32. age = s and (os.time() - s.mtime) or 0
  33. }
  34. end
  35. end
  36. end
  37. table.sort(rv.leases, function(a, b) return a.age < b.age end)
  38. luci.http.prepare_content("application/json")
  39. luci.http.write_json(rv)
  40. end