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