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