cadda546ef8017a5362ef4cbc17021736971b943
[project/luci.git] / modules / admin-core / luasrc / controller / admin / status_olsr.lua
1 module("luci.controller.admin.status_olsr", package.seeall)
2 require("luci.sys")
3
4 function index()
5 local page = node("admin", "status", "olsr")
6 page.target = call("action_index")
7 page.title = "OLSR"
8
9 local page = node("admin", "status", "olsr", "routes")
10 page.target = call("action_routes")
11 page.title = "Routen"
12 page.order = 10
13
14 local page = node("admin", "status", "olsr", "topology")
15 page.target = call("action_topology")
16 page.title = "Topologie"
17 page.order = 20
18
19 local page = node("admin", "status", "olsr", "hna")
20 page.target = call("action_hna")
21 page.title = "HNA"
22 page.order = 30
23
24 local page = node("admin", "status", "olsr", "mid")
25 page.target = call("action_mid")
26 page.title = "MID"
27 page.order = 50
28 end
29
30 function action_index()
31 local data = fetch_txtinfo("links")
32
33 if not data or not data.Links then
34 luci.template.render("status-olsr/error_olsr")
35 return nil
36 end
37
38 local function compare(a, b)
39 if tonumber(a.ETX) == 0 then
40 return false
41 end
42
43 if tonumber(b.ETX) == 0 then
44 return true
45 end
46
47 return tonumber(a.ETX) < tonumber(b.ETX)
48 end
49
50 table.sort(data.Links, compare)
51
52 luci.template.render("status-olsr/index", {links=data.Links})
53 end
54
55 function action_routes()
56 local data = fetch_txtinfo("routes")
57
58 if not data or not data.Routes then
59 luci.template.render("status-olsr/error_olsr")
60 return nil
61 end
62
63 local function compare(a, b)
64 if tonumber(a.ETX) == 0 then
65 return false
66 end
67
68 if tonumber(b.ETX) == 0 then
69 return true
70 end
71
72 return tonumber(a.ETX) < tonumber(b.ETX)
73 end
74
75 table.sort(data.Routes, compare)
76
77 luci.template.render("status-olsr/routes", {routes=data.Routes})
78 end
79
80 function action_topology()
81 local data = fetch_txtinfo("topology")
82
83 if not data or not data.Topology then
84 luci.template.render("status-olsr/error_olsr")
85 return nil
86 end
87
88 local function compare(a, b)
89 return a["Destination IP"] < b["Destination IP"]
90 end
91
92 table.sort(data.Topology, compare)
93
94 luci.template.render("status-olsr/topology", {routes=data.Topology})
95 end
96
97 function action_hna()
98 local data = fetch_txtinfo("hna")
99
100 if not data or not data.HNA then
101 luci.template.render("status-olsr/error_olsr")
102 return nil
103 end
104
105 local function compare(a, b)
106 return a.Network < b.Network
107 end
108
109 table.sort(data.HNA, compare)
110
111 luci.template.render("status-olsr/hna", {routes=data.HNA})
112 end
113
114 function action_mid()
115 local data = fetch_txtinfo("mid")
116
117 if not data or not data.MID then
118 luci.template.render("status-olsr/error_olsr")
119 return nil
120 end
121
122 local function compare(a, b)
123 return a.IP < b.IP
124 end
125
126 table.sort(data.MID, compare)
127
128 luci.template.render("status-olsr/mid", {mids=data.MID})
129 end
130
131
132 -- Internal
133 function fetch_txtinfo(otable)
134 otable = otable or ""
135 local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
136
137 if #rawdata == 0 then
138 return nil
139 end
140
141 local data = {}
142
143 local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
144
145
146 for i, tbl in ipairs(tables) do
147 local lines = luci.util.split(tbl, "\n")
148 local name = table.remove(lines, 1):sub(8)
149 local keys = luci.util.split(table.remove(lines, 1), "\t")
150
151 data[name] = {}
152
153 for j, line in ipairs(lines) do
154 local fields = luci.util.split(line, "\t")
155 data[name][j] = {}
156 for k, key in pairs(keys) do
157 data[name][j][key] = fields[k]
158 end
159 end
160 end
161
162 return data
163 end