luci-olsr: fix html olsr status page
[project/luci.git] / applications / luci-olsr / luasrc / view / status-olsr / overview.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008 Steven Barth <steven@midlink.org>
4 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12 -%>
13
14 <%
15 local sys = require "luci.sys"
16 local utl = require "luci.util"
17 local fs = require "luci.fs"
18  
19 function get_version()
20         local data = utl.split((utl.trim(sys.exec("/usr/sbin/olsrd -v"))))
21         local buildfull = utl.trim(utl.split(data[2],": ")[2])
22         local ver = {
23                 version = utl.trim(utl.split(data[1]," - ")[2]),
24                 date = utl.trim(utl.split(buildfull, " ")[1]),
25                 time = utl.trim(utl.split(buildfull, " ")[2]),
26                 host = utl.trim(utl.split(buildfull, " ")[4])
27         }
28         return ver
29 end
30 local ver = get_version()
31
32 local ifaces = fetch_txtinfo("int")
33 if not ifaces or not ifaces.Interfaces then
34         luci.template.render("status-olsr/error_olsr")
35         return nil
36 end
37 local interfaces = ""
38 for k,v in pairs(ifaces.Interfaces) do
39         interfaces = utl.trim(interfaces.." "..v.Name)
40 end
41 interfaces = string.gsub(interfaces, " ", ", ")
42 local nr_ifaces = #ifaces.Interfaces
43
44 local links = fetch_txtinfo("links")
45 local nr_neigh = #links.Links
46 local neighbors = ""
47 for k,v in pairs(links.Links) do
48         local link
49         if v.Hostname then
50                 link = v.Hostname
51         else
52                 link = v["Remote IP"]
53         end
54         neighbors = utl.trim("<a href=http://"..link.."/cgi-bin-status.html>"..link.."</a> "..neighbors)
55 end
56
57 local data = fetch_txtinfo("topology")
58 local nr_topo = #data.Topology
59 local utable = {}
60 for k,v in pairs(data.Topology) do
61         if utl.contains(utable, v["Dest. IP"]) == false then
62                 table.insert(utable, v["Dest. IP"])
63         end
64 end
65 local nr_nodes = #utable
66
67 local data = fetch_txtinfo("hna")
68 local nr_hna = #data.HNA
69
70 local meshfactor = string.format("%.2f", nr_topo / nr_nodes)
71
72 if luci.http.formvalue("status") == "1" then
73         rv = {
74                 nr_neighbors = nr_neigbors,
75                 neighbors = neighbors,
76                 interfaces = interfaces,
77                 nr_ifaces = nr_ifaces,
78                 nr_links = nr_links,
79                 nr_topo = nr_topo,
80                 nr_nodes = nr_nodes,
81                 meshfactor = meshfactor
82         }
83         luci.http.prepare_content("application/json")
84         luci.http.write_json(rv)
85         return
86 end
87         
88
89 local ipv = luci.model.uci.cursor():get_first("olsrd", "olsrd", "IpVersion", "4")
90
91 function write_conf(conf, file)
92         if fs.access(conf) then
93                 luci.http.header("Content-Disposition", "attachment; filename="..file)
94                 luci.http.prepare_content("text/plain")
95                 luci.http.write(fs.readfile(conf))
96         end
97 end
98
99 conf = luci.http.formvalue()
100
101 if conf.openwrt then
102         write_conf("/etc/config/olsrd", "olsrd")
103         return false
104 end
105
106 if conf.conf_v4 then   
107         write_conf("/var/etc/olsrd.conf.ipv4", "olsrd.conf.ipv4")
108         return false
109 end
110
111 if conf.conf_v6 then
112         write_conf("/var/etc/olsrd.conf.ipv6", "olsrd.conf.ipv6")
113         return false
114 end
115
116 if conf.conf then
117         write_conf("/var/etc/olsrd.conf", "olsrd.conf")
118         return false
119 end
120
121 %>
122
123 <%+header%>
124
125 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
126 <script type="text/javascript">//<![CDATA[
127
128 XHR.poll(30, '<%=REQUEST_URI%>', { status: 1 },
129                 function(x, info)
130                 {
131                 var e;
132
133                 if (e = document.getElementById('nr_neighbors'))
134                         e.innerHTML = info.nr_neighbors;
135
136                 if (e = document.getElementById('neighbors'))
137                         e.innerHTML = info.neighbors;
138
139                 if (e = document.getElementById('nr_ifaces'))
140                         e.innerHTML = info.nr_ifaces;
141
142                 if (e = document.getElementById('interfaces'))
143                         e.innerHTML = info.interfaces;
144
145                 if (e = document.getElementById('nr_links'))
146                         e.innerHTML = info.nr_links;
147
148                 if (e = document.getElementById('nr_topo'))
149                         e.innerHTML = info.nr_topo;
150
151                 if (e = document.getElementById('nr_nodes'))
152                         e.innerHTML = info.nr_nodes;
153
154                 if (e = document.getElementById('meshfactor'))
155                         e.innerHTML = info.meshfactor;
156                 }
157         );
158 //]]></script>
159
160
161 <h2><a id="content" name="content">OLSR <%:Overview%></a></h2>
162
163 <div class="cbi-map">
164         <div class="cbi-section-node">
165                 <div class="cbi-value">
166                         <div style="width: 15em; float:left;">
167                                 <label class="cbi-value-title"><%:Interfaces%></label>
168                         </div>
169                         <div class="cbi-value-field">
170                                 <div style="width: 6em; float:left;">
171                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "interfaces")%>">
172                                                 <span id="nr_ifaces">
173                                                         <%=nr_ifaces%>
174                                                 <span>
175                                         </a>
176                                 </div>
177                                 <div style="padding-left: 21em;">
178                                         <span id="interfaces">
179                                                 <%=interfaces%>
180                                         </span>
181                                 </div>
182                         </div>
183                         <br/>
184                 </div>
185
186                 <div class="cbi-value">
187                         <div style="width: 15em; float:left;">
188                                 <%:Neighbors%>
189                         </div>
190                         <div class="cbi-value-field">
191                                 <div style="width: 6em; float:left;">
192                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "neighbors")%>">
193                                                 <span id="nr_neigh">
194                                                         <%=nr_neigh%>
195                                                 </span>
196                                         </a>
197                                 </div>
198                                 <div style="padding-left: 21em;">
199                                         <span id="neighbors">
200                                                 <%=neighbors%>
201                                         </span>
202                                 </div>
203                         </div>
204                         <br/>
205                 </div>
206
207                 <div class="cbi-value">
208                         <div style="width: 15em; float:left;">
209                                 <%:Nodes%>
210                         </div>
211                         <div class="cbi-value-field">
212                                 <div style="width: 6em; float:left;">
213                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "topology")%>">
214                                                 <span id="nr_nodes">
215                                                         <%=nr_nodes%>
216                                                 </span>
217                                         </a>
218                                 </div>
219                         </div>
220                         <br/>
221                 </div>
222
223                 <div class="cbi-value">
224                         <div style="width: 15em; float:left;">
225                                 <%:HNA%>
226                         </div>
227                         <div class="cbi-value-field">
228                                 <div style="width: 6em; float:left;">
229                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "hna")%>">
230                                                 <span id="nr_hna">
231                                                         <%=nr_hna%>
232                                                 </span>
233                                         </a>
234                                 </div>
235                         </div>
236                         <br/>
237                 </div>
238
239                 <div class="cbi-value">
240                         <div style="width: 15em; float:left;">
241                                 <%:Links total%>
242                         </div>
243                         <div class="cbi-value-field">
244                                 <div style="width: 6em; float:left;">
245                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "topology")%>">
246                                                 <span id="nr_topo">
247                                                         <%=nr_topo%>
248                                                 </span>
249                                         </a>
250                                 </div>
251                         </div>
252                         <br/>
253                 </div>
254
255                 <div class="cbi-value">
256                         <div style="width: 15em; float:left;">
257                                 <%:Links per node (average)%>
258                         </div>
259                         <div class="cbi-value-field">
260                                 <div style="width: 6em; float:left;">
261                                         <span id="meshfactor">
262                                                 <%=meshfactor%>
263                                         </span>
264                                 </div>
265                         </div>
266                         <br/>
267                 </div>
268         </div>
269 </div>
270 <br/>
271
272 <h2><a id="content" name="content">OLSR <%:Configuration%></a></h2>
273
274 <div class="cbi-map">
275         <div class="cbi-section-node">
276                 <div class="cbi-value">
277                         <div style="width: 15em; float:left;">
278                                 <%:Version%>
279                         </div>
280                         <div class="cbi-value-field">
281                                 <%=ver.version%> (built <%=ver.date%> on <%=ver.host%>)
282                         </div>
283                         <br/>
284                 </div>
285
286                 <div class="cbi-value">
287                         <div style="width: 15em; float:left;">
288                                 <%:Download Config%>
289                         </div>
290                         <div class="cbi-value-field">
291                                 <a href="<%=REQUEST_URI%>?openwrt">OpenWrt</a>,
292                                 <% if ipv == "6and4" then %>
293                                         <a href="<%=REQUEST_URI%>?conf_v4">OLSRD IPv4</a>,
294                                         <a href="<%=REQUEST_URI%>?conf_v6">OLSRD IPv6</a>
295                                 <% else %>
296                                         <a href="<%=REQUEST_URI%>?conf">OLSRD</a>
297                                 <% end %>
298                         </div>
299                         <br/>
300                 </div>
301         </div>
302 </div>
303
304 <%+footer%>