event-forwarder 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/lua
  2. -- event-forwarder ubus RPC plugin
  3. -- Provides remote control via ubus
  4. local json = require "cbi.json"
  5. local ubus = require "ubus"
  6. local uci = require "luci.model.uci".cursor()
  7. module("luci.rpcd.event-forwarder", package.seeall)
  8. function getStatus()
  9. local conn = ubus.connect()
  10. local status = {
  11. running = (tonumber(luci.sys.exec("pgrep -f event-forwarder")) > 0),
  12. router_id = uci:get("event-forwarder", "router", "id") or "unknown",
  13. server_url = uci:get("event-forwarder", "server", "url") or "",
  14. events_sent = 0, -- Could track this in a file
  15. }
  16. conn:close()
  17. return status
  18. end
  19. function getEvents(count)
  20. count = tonumber(count) or 10
  21. local events = {}
  22. local f = io.popen("tail -n " .. count .. " /var/log/event-forwarder.log 2>/dev/null")
  23. if f then
  24. for line in f:lines() do
  25. table.insert(events, line)
  26. end
  27. f:close()
  28. end
  29. return events
  30. end
  31. function testConnection()
  32. local conn = ubus.connect()
  33. local res = conn:call("network", "getStatus", {})
  34. conn:close()
  35. return { success = true, network = res }
  36. end
  37. function _init()
  38. local lp = require "luci.http"
  39. local cb = require "luci.rpcd".callback
  40. local rv = {
  41. ["getStatus"] = { cb=getStatus, desc="Get event forwarder status" },
  42. ["getEvents"] = { cb=getEvents, desc="Get recent events" },
  43. ["test"] = { cb=testConnection, desc="Test connection" },
  44. }
  45. return rv
  46. end
  47. return _init()