cshark.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright (C) 2014, QA Cafe, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. ]]--
  9. module("luci.controller.cshark", package.seeall)
  10. function index()
  11. page = node("admin", "network", "cloudshark")
  12. page.target = cbi("admin_network/cshark")
  13. page.title = _("CloudShark")
  14. page.order = 70
  15. page.acl_depends = { "luci-app-cshark" }
  16. page = entry({"admin", "network", "cshark_iface_dump_start"}, call("cshark_iface_dump_start"), nil)
  17. page.acl_depends = { "luci-app-cshark" }
  18. page.leaf = true
  19. page = entry({"admin", "network", "cshark_iface_dump_stop"}, call("cshark_iface_dump_stop"), nil)
  20. page.acl_depends = { "luci-app-cshark" }
  21. page.leaf = true
  22. page = entry({"admin", "network", "cshark_check_status"}, call("cshark_check_status"), nil)
  23. page.acl_depends = { "luci-app-cshark" }
  24. page.leaf = true
  25. page = entry({"admin", "network", "cshark_link_list_get"}, call("cshark_link_list_get"), nil)
  26. page.acl_depends = { "luci-app-cshark" }
  27. page.leaf = true
  28. page = entry({"admin", "network", "cshark_link_list_clear"}, call("cshark_link_list_clear"), nil)
  29. page.acl_depends = { "luci-app-cshark" }
  30. page.leaf = true
  31. end
  32. function cshark_iface_dump_start(ifname, value, flag, filter)
  33. if ifname == nil or ifname == '' then
  34. ifname = 'any'
  35. end
  36. if tonumber(value) == nil
  37. then
  38. value = '0'
  39. end
  40. if filter == nil or filter == '' then
  41. filter = ''
  42. end
  43. if flag == nil or flag == '' then
  44. filter = 'T'
  45. end
  46. luci.http.prepare_content("text/plain")
  47. local res = os.execute("(/sbin/cshark -i %s -%s %s -p /tmp/cshark-luci.pid %s > /tmp/cshark-luci.out 2>&1) &" %{
  48. luci.util.shellquote(ifname),
  49. luci.util.shellquote(flag),
  50. luci.util.shellquote(value),
  51. luci.util.shellquote(filter)
  52. })
  53. luci.http.write(tostring(res))
  54. end
  55. function cshark_iface_dump_stop()
  56. luci.http.prepare_content("text/plain")
  57. local f = io.open("/tmp/cshark-luci.pid", "rb")
  58. local pid = f:read("*all")
  59. io.close(f)
  60. local res = os.execute("kill -TERM " .. pid)
  61. luci.http.write(tostring(res))
  62. end
  63. function cshark_check_status()
  64. local msg = "";
  65. local status;
  66. local f = io.open("/tmp/cshark-luci.pid","r")
  67. if f ~= nil then
  68. status = 1;
  69. io.close(f)
  70. else
  71. status = 0;
  72. end
  73. f = io.open("/tmp/cshark-luci.out","r")
  74. if f ~= nil then
  75. msg = f:read("*all")
  76. io.close(f)
  77. if msg ~= '' then
  78. os.remove('/tmp/cshark-luci.out')
  79. end
  80. end
  81. luci.http.prepare_content("application/json")
  82. local res = {}
  83. res["status"] = status;
  84. res["msg"] = msg;
  85. luci.http.write_json(res)
  86. end
  87. function cshark_link_list_get()
  88. local uci = require("uci").cursor()
  89. luci.http.prepare_content("application/json")
  90. luci.http.write("[")
  91. local t = uci:get("cshark", "cshark", "entry")
  92. if (t ~= nil) then
  93. for i = #t, 1, -1 do
  94. luci.http.write("[\"" .. t[i] .. "\"],")
  95. end
  96. end
  97. luci.http.write("[]]")
  98. end
  99. function cshark_link_list_clear()
  100. local uci = require("uci").cursor()
  101. uci:delete("cshark", "cshark", "entry")
  102. uci:commit("cshark");
  103. luci.http.status(200, "OK")
  104. end