cshark.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = entry({"admin", "network", "cshark_iface_dump_start"}, call("cshark_iface_dump_start"), nil)
  16. page.leaf = true
  17. page = entry({"admin", "network", "cshark_iface_dump_stop"}, call("cshark_iface_dump_stop"), nil)
  18. page.leaf = true
  19. page = entry({"admin", "network", "cshark_check_status"}, call("cshark_check_status"), nil)
  20. page.leaf = true
  21. page = entry({"admin", "network", "cshark_link_list_get"}, call("cshark_link_list_get"), nil)
  22. page.leaf = true
  23. page = entry({"admin", "network", "cshark_link_list_clear"}, call("cshark_link_list_clear"), nil)
  24. page.leaf = true
  25. end
  26. function cshark_iface_dump_start(ifname, value, flag, filter)
  27. if ifname == nil or ifname == '' then
  28. ifname = 'any'
  29. end
  30. if tonumber(value) == nil
  31. then
  32. value = '0'
  33. end
  34. if filter == nil or filter == '' then
  35. filter = ''
  36. end
  37. if flag == nil or flag == '' then
  38. filter = 'T'
  39. end
  40. luci.http.prepare_content("text/plain")
  41. local res = os.execute("(/sbin/cshark -i " .. ifname .. " -" .. flag .. " " .. value .. " -p /tmp/cshark-luci.pid " .. filter .. " > /tmp/cshark-luci.out 2>&1) &")
  42. luci.http.write(tostring(res))
  43. end
  44. function cshark_iface_dump_stop()
  45. luci.http.prepare_content("text/plain")
  46. local f = io.open("/tmp/cshark-luci.pid", "rb")
  47. local pid = f:read("*all")
  48. io.close(f)
  49. local res = os.execute("kill -TERM " .. pid)
  50. luci.http.write(tostring(res))
  51. end
  52. function cshark_check_status()
  53. local msg = "";
  54. local status;
  55. local f = io.open("/tmp/cshark-luci.pid","r")
  56. if f ~= nil then
  57. status = 1;
  58. io.close(f)
  59. else
  60. status = 0;
  61. end
  62. f = io.open("/tmp/cshark-luci.out","r")
  63. if f ~= nil then
  64. msg = f:read("*all")
  65. io.close(f)
  66. if msg ~= '' then
  67. os.remove('/tmp/cshark-luci.out')
  68. end
  69. end
  70. luci.http.prepare_content("application/json")
  71. local res = {}
  72. res["status"] = status;
  73. res["msg"] = msg;
  74. luci.http.write_json(res)
  75. end
  76. function cshark_link_list_get()
  77. local uci = require("uci").cursor()
  78. luci.http.prepare_content("application/json")
  79. luci.http.write("[")
  80. local t = uci:get("cshark", "cshark", "entry")
  81. if (t ~= nil) then
  82. for i = #t, 1, -1 do
  83. luci.http.write("[\"" .. t[i] .. "\"],")
  84. end
  85. end
  86. luci.http.write("[]]")
  87. end
  88. function cshark_link_list_clear()
  89. local uci = require("uci").cursor()
  90. uci:delete("cshark", "cshark", "entry")
  91. uci:commit("cshark");
  92. luci.http.status(200, "OK")
  93. end