7bb47612b6bf8bcb5e611996aa7269dddcb8b302
[project/luci.git] / applications / luci-app-freifunk-diagnostics / luasrc / controller / freifunk / diag.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2013 Manuel Munz <freifunk@somakoma.de>
4 -- Licensed to the public under the Apache License 2.0.
5
6 module("luci.controller.freifunk.diag", package.seeall)
7
8 function index()
9         local uci = require("luci.model.uci").cursor()
10         local page
11         page = node("freifunk", "status", "diagnostics")
12         page.target = template("freifunk/diagnostics")
13         page.title  = _("Diagnostics")
14         page.order  = 60
15
16         page = entry({"freifunk", "status", "diag_ping"}, call("diag_ping"), nil)
17         page.leaf = true
18
19         page = entry({"freifunk", "status", "diag_nslookup"}, call("diag_nslookup"), nil)
20         page.leaf = true
21
22         page = entry({"freifunk", "status", "diag_traceroute"}, call("diag_traceroute"), nil)
23         page.leaf = true
24
25         page = entry({"freifunk", "status", "diag_ping6"}, call("diag_ping6"), nil)
26         page.leaf = true
27
28         page = entry({"freifunk", "status", "diag_traceroute6"}, call("diag_traceroute6"), nil)
29         page.leaf = true
30 end
31
32 function diag_command(cmd, addr)
33         if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
34                 luci.http.prepare_content("text/plain")
35
36                 local util = io.popen(cmd % addr)
37                 if util then
38                         while true do
39                                 local ln = util:read("*l")
40                                 if not ln then break end
41                                 luci.http.write(ln)
42                                 luci.http.write("\n")
43                         end
44
45                         util:close()
46                 end
47
48                 return
49         end
50
51         luci.http.status(500, "Bad address")
52 end
53
54 function diag_ping(addr)
55         diag_command("ping -c 5 -W 1 %q 2>&1", addr)
56 end
57
58 function diag_traceroute(addr)
59         diag_command("traceroute -q 1 -w 1 -n %q 2>&1", addr)
60 end
61
62 function diag_nslookup(addr)
63         diag_command("nslookup %q 2>&1", addr)
64 end
65
66 function diag_ping6(addr)
67         diag_command("ping6 -c 5 %q 2>&1", addr)
68 end
69
70 function diag_traceroute6(addr)
71         diag_command("traceroute6 -q 1 -w 2 -n %q 2>&1", addr)
72 end