status.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.controller.admin.status", package.seeall)
  5. function index()
  6. entry({"admin", "status", "overview"}, template("admin_status/index"), _("Overview"), 1)
  7. entry({"admin", "status", "iptables"}, template("admin_status/iptables"), _("Firewall"), 2).leaf = true
  8. entry({"admin", "status", "iptables_dump"}, call("dump_iptables")).leaf = true
  9. entry({"admin", "status", "iptables_action"}, post("action_iptables")).leaf = true
  10. entry({"admin", "status", "routes"}, template("admin_status/routes"), _("Routes"), 3)
  11. entry({"admin", "status", "syslog"}, call("action_syslog"), _("System Log"), 4)
  12. entry({"admin", "status", "dmesg"}, call("action_dmesg"), _("Kernel Log"), 5)
  13. entry({"admin", "status", "processes"}, form("admin_status/processes"), _("Processes"), 6)
  14. entry({"admin", "status", "realtime"}, alias("admin", "status", "realtime", "load"), _("Realtime Graphs"), 7)
  15. entry({"admin", "status", "realtime", "load"}, template("admin_status/load"), _("Load"), 1).leaf = true
  16. entry({"admin", "status", "realtime", "load_status"}, call("action_load")).leaf = true
  17. entry({"admin", "status", "realtime", "bandwidth"}, template("admin_status/bandwidth"), _("Traffic"), 2).leaf = true
  18. entry({"admin", "status", "realtime", "bandwidth_status"}, call("action_bandwidth")).leaf = true
  19. if nixio.fs.access("/etc/config/wireless") then
  20. entry({"admin", "status", "realtime", "wireless"}, template("admin_status/wireless"), _("Wireless"), 3).leaf = true
  21. entry({"admin", "status", "realtime", "wireless_status"}, call("action_wireless")).leaf = true
  22. end
  23. entry({"admin", "status", "realtime", "connections"}, template("admin_status/connections"), _("Connections"), 4).leaf = true
  24. entry({"admin", "status", "realtime", "connections_status"}, call("action_connections")).leaf = true
  25. entry({"admin", "status", "nameinfo"}, call("action_nameinfo")).leaf = true
  26. end
  27. function action_syslog()
  28. local syslog = luci.sys.syslog()
  29. luci.template.render("admin_status/syslog", {syslog=syslog})
  30. end
  31. function action_dmesg()
  32. local dmesg = luci.sys.dmesg()
  33. luci.template.render("admin_status/dmesg", {dmesg=dmesg})
  34. end
  35. function dump_iptables(family, table)
  36. local prefix = (family == "6") and "ip6" or "ip"
  37. local ok, lines = pcall(io.lines, "/proc/net/%s_tables_names" % prefix)
  38. if ok and lines then
  39. local s
  40. for s in lines do
  41. if s == table then
  42. luci.http.prepare_content("text/plain")
  43. luci.sys.process.exec({
  44. "/usr/sbin/%stables" % prefix, "-w", "-t", table,
  45. "--line-numbers", "-nxvL"
  46. }, luci.http.write)
  47. return
  48. end
  49. end
  50. end
  51. luci.http.status(404, "No such table")
  52. luci.http.prepare_content("text/plain")
  53. end
  54. function action_iptables()
  55. if luci.http.formvalue("zero") then
  56. if luci.http.formvalue("family") == "6" then
  57. luci.util.exec("/usr/sbin/ip6tables -Z")
  58. else
  59. luci.util.exec("/usr/sbin/iptables -Z")
  60. end
  61. elseif luci.http.formvalue("restart") then
  62. luci.util.exec("/etc/init.d/firewall restart")
  63. end
  64. luci.http.redirect(luci.dispatcher.build_url("admin/status/iptables"))
  65. end
  66. function action_bandwidth(iface)
  67. luci.http.prepare_content("application/json")
  68. local bwc = io.popen("luci-bwc -i %s 2>/dev/null"
  69. % luci.util.shellquote(iface))
  70. if bwc then
  71. luci.http.write("[")
  72. while true do
  73. local ln = bwc:read("*l")
  74. if not ln then break end
  75. luci.http.write(ln)
  76. end
  77. luci.http.write("]")
  78. bwc:close()
  79. end
  80. end
  81. function action_wireless(iface)
  82. luci.http.prepare_content("application/json")
  83. local bwc = io.popen("luci-bwc -r %s 2>/dev/null"
  84. % luci.util.shellquote(iface))
  85. if bwc then
  86. luci.http.write("[")
  87. while true do
  88. local ln = bwc:read("*l")
  89. if not ln then break end
  90. luci.http.write(ln)
  91. end
  92. luci.http.write("]")
  93. bwc:close()
  94. end
  95. end
  96. function action_load()
  97. luci.http.prepare_content("application/json")
  98. local bwc = io.popen("luci-bwc -l 2>/dev/null")
  99. if bwc then
  100. luci.http.write("[")
  101. while true do
  102. local ln = bwc:read("*l")
  103. if not ln then break end
  104. luci.http.write(ln)
  105. end
  106. luci.http.write("]")
  107. bwc:close()
  108. end
  109. end
  110. function action_connections()
  111. local sys = require "luci.sys"
  112. luci.http.prepare_content("application/json")
  113. luci.http.write('{ "connections": ')
  114. luci.http.write_json(sys.net.conntrack())
  115. local bwc = io.popen("luci-bwc -c 2>/dev/null")
  116. if bwc then
  117. luci.http.write(', "statistics": [')
  118. while true do
  119. local ln = bwc:read("*l")
  120. if not ln then break end
  121. luci.http.write(ln)
  122. end
  123. luci.http.write("]")
  124. bwc:close()
  125. end
  126. luci.http.write(" }")
  127. end
  128. function action_nameinfo(...)
  129. local util = require "luci.util"
  130. luci.http.prepare_content("application/json")
  131. luci.http.write_json(util.ubus("network.rrdns", "lookup", {
  132. addrs = { ... },
  133. timeout = 5000,
  134. limit = 1000
  135. }) or { })
  136. end