applications/luci-olsr: Update all statuspages; use json reloading on overview, neigh...
[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"><label class="cbi-value-title"><%:Interfaces%></label>
166                         <div class="cbi-value-field">
167                                 <div style="width: 6em; float:left;">
168                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "interfaces")%>">
169                                                 <span id="nr_ifaces">
170                                                         <%=nr_ifaces%></a>
171                                                 <span>
172                                 </div>
173                                 <div style="padding-left: 6em;">
174                                         <span id="interfaces">
175                                                 <%=interfaces%>
176                                         </span>
177                                 </div>
178                         </div>
179                 </div>
180         
181                 <div class="cbi-value"><label class="cbi-value-title"><%:Neighbors%></label>
182                         <div class="cbi-value-field">
183                                 <div style="width: 6em; float:left;">
184                                         <a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "neighbors")%>">
185                                                 <span id="nr_neigh">
186                                                         <%=nr_neigh%></a>
187                                                 </span>
188                                 </div>
189                                 <div style="padding-left: 6em;">
190                                         <span id="neighbors">
191                                                 <%=neighbors%>
192                                         </span>
193                                 </div>
194                         </div>
195                 </div>
196
197                 <div class="cbi-value"><label class="cbi-value-title"><%:Nodes%></label>
198                         <div class="cbi-value-field"><a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "topology")%>">
199                                 <span id="nr_nodes">
200                                         <%=nr_nodes%></a>
201                                 </span>
202                         </div>
203                 </div>
204
205                 <div class="cbi-value"><label class="cbi-value-title"><%:HNA%></label>
206                         <div class="cbi-value-field"><a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "hna")%>">
207                                 <span id="nr_hna">
208                                         <%=nr_hna%></a>
209                                 </span>
210                         </div>
211                 </div>
212
213                 <div class="cbi-value"><label class="cbi-value-title"><%:Links total%></label>
214                         <div class="cbi-value-field"><a href="<%=luci.dispatcher.build_url("freifunk", "olsr", "topology")%>">
215                                 <span id="nr_topo">
216                                         <%=nr_topo%></a>
217                                 </span>
218                         </div>
219                 </div>
220
221                 <div class="cbi-value"><label class="cbi-value-title"><%:Links per node (average)%></label>
222                         <div class="cbi-value-field">
223                                 <span id="meshfactor">
224                                         <%=meshfactor%>
225                                 </span>
226                         </div>
227                 </div>
228         </div>
229 </div>
230
231 <h2><a id="content" name="content">OLSR <%:Configuration%></a></h2>
232
233 <div class="cbi-map">
234         <div class="cbi-section-node">
235                 <div class="cbi-value"><label class="cbi-value-title"><%:Version%></label>
236                         <div class="cbi-value-field"><%=ver.version%> (built <%=ver.date%> on <%=ver.host%>)
237                         </div>
238                 </div>
239
240                 <div class="cbi-value"><label class="cbi-value-title"><%:Download Config%></label>
241                         <div class="cbi-value-field">
242                                 <a href="<%=REQUEST_URI%>?openwrt">OpenWrt</a>,
243                                 <% if ipv == "6and4" then %>
244                                         <a href="<%=REQUEST_URI%>?conf_v4">OLSRD IPv4</a>,
245                                         <a href="<%=REQUEST_URI%>?conf_v6">OLSRD IPv6</a>
246                                 <% else %>
247                                         <a href="<%=REQUEST_URI%>?conf">OLSRD</a>
248                                 <% end %>
249                         </div>
250                 </div>
251         </div>
252 </div>
253
254 <%+footer%>