luci-app-statistics: add initial support for collect-mod-sensors
[project/luci.git] / applications / luci-app-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Copyright 2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
6
7 function index()
8
9         require("nixio.fs")
10         require("luci.util")
11         require("luci.statistics.datatree")
12
13         -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
14         function _entry( path, ... )
15                 local file = path[5] or path[4]
16                 if nixio.fs.access( "/usr/lib/collectd/" .. file .. ".so" ) then
17                         entry( path, ... )
18                 end
19         end
20
21         local labels = {
22                 s_output        = _("Output plugins"),
23                 s_general       = _("General plugins"),
24                 s_network       = _("Network plugins"),
25
26                 conntrack       = _("Conntrack"),
27                 cpu                     = _("Processor"),
28                 csv                     = _("CSV Output"),
29                 df                      = _("Disk Space Usage"),
30                 disk            = _("Disk Usage"),
31                 dns                     = _("DNS"),
32                 email           = _("Email"),
33                 entropy         = _("Entropy"),
34                 exec            = _("Exec"),
35                 interface       = _("Interfaces"),
36                 iptables        = _("Firewall"),
37                 irq                     = _("Interrupts"),
38                 iwinfo          = _("Wireless"),
39                 load            = _("System Load"),
40                 memory          = _("Memory"),
41                 netlink         = _("Netlink"),
42                 network         = _("Network"),
43                 nut                     = _("UPS"),
44                 olsrd           = _("OLSRd"),
45                 ping            = _("Ping"),
46                 processes       = _("Processes"),
47                 rrdtool         = _("RRDTool"),
48                 sensors     = _("Sensors"),
49                 splash_leases = _("Splash Leases"),
50                 tcpconns        = _("TCP Connections"),
51                 unixsock        = _("UnixSock"),
52                 uptime          = _("Uptime")
53         }
54
55         -- our collectd menu
56         local collectd_menu = {
57                 output  = { "csv", "network", "rrdtool", "unixsock" },
58                 general = { "cpu", "df", "disk", "email", "entropy", "exec", "irq", "load", "memory", "nut", "processes", "sensors", "uptime" },
59                 network = { "conntrack", "dns", "interface", "iptables", "netlink", "olsrd", "ping", "splash_leases", "tcpconns", "iwinfo" }
60         }
61
62         -- create toplevel menu nodes
63         local st = entry({"admin", "statistics"}, template("admin_statistics/index"), _("Statistics"), 80)
64         st.index = true
65
66         entry({"admin", "statistics", "collectd"}, cbi("luci_statistics/collectd"), _("Setup"), 20).subindex = true
67
68
69         -- populate collectd plugin menu
70         local index = 1
71         for section, plugins in luci.util.kspairs( collectd_menu ) do
72                 local e = entry(
73                         { "admin", "statistics", "collectd", section },
74                         firstchild(), labels["s_"..section], index * 10
75                 )
76
77                 e.index = true
78
79                 for j, plugin in luci.util.vspairs( plugins ) do
80                         _entry(
81                                 { "admin", "statistics", "collectd", section, plugin },
82                                 cbi("luci_statistics/" .. plugin ),
83                                 labels[plugin], j * 10
84                         )
85                 end
86
87                 index = index + 1
88         end
89
90         -- output views
91         local page = entry( { "admin", "statistics", "graph" }, template("admin_statistics/index"), _("Graphs"), 10)
92               page.setuser  = "nobody"
93               page.setgroup = "nogroup"
94
95         local vars = luci.http.formvalue(nil, true)
96         local span = vars.timespan or nil
97         local host = vars.host or nil
98
99         -- get rrd data tree
100         local tree = luci.statistics.datatree.Instance(host)
101
102         local _, plugin, idx
103         for _, plugin, idx in luci.util.vspairs( tree:plugins() ) do
104
105                 -- get plugin instances
106                 local instances = tree:plugin_instances( plugin )
107
108                 -- plugin menu entry
109                 entry(
110                         { "admin", "statistics", "graph", plugin },
111                         call("statistics_render"), labels[plugin], idx
112                 ).query = { timespan = span , host = host }
113
114                 -- if more then one instance is found then generate submenu
115                 if #instances > 1 then
116                         local _, inst, idx2
117                         for _, inst, idx2 in luci.util.vspairs(instances) do
118                                 -- instance menu entry
119                                 entry(
120                                         { "admin", "statistics", "graph", plugin, inst },
121                                         call("statistics_render"), inst, idx2
122                                 ).query = { timespan = span , host = host }
123                         end
124                 end
125         end
126 end
127
128 function statistics_render()
129
130         require("luci.statistics.rrdtool")
131         require("luci.template")
132         require("luci.model.uci")
133
134         local vars  = luci.http.formvalue()
135         local req   = luci.dispatcher.context.request
136         local path  = luci.dispatcher.context.path
137         local uci   = luci.model.uci.cursor()
138         local spans = luci.util.split( uci:get( "luci_statistics", "collectd_rrdtool", "RRATimespans" ), "%s+", nil, true )
139         local span  = vars.timespan or uci:get( "luci_statistics", "rrdtool", "default_timespan" ) or spans[1]
140         local host  = vars.host     or uci:get( "luci_statistics", "collectd", "Hostname" ) or luci.sys.hostname()
141         local opts = { host = vars.host }
142         local graph = luci.statistics.rrdtool.Graph( luci.util.parse_units( span ), opts )
143         local hosts = graph.tree:host_instances()
144
145         local is_index = false
146         local i, p, inst, idx
147
148         -- deliver image
149         if vars.img then
150                 local l12 = require "luci.ltn12"
151                 local png = io.open(graph.opts.imgpath .. "/" .. vars.img:gsub("%.+", "."), "r")
152                 if png then
153                         luci.http.prepare_content("image/png")
154                         l12.pump.all(l12.source.file(png), luci.http.write)
155                 end
156                 return
157         end
158
159         local plugin, instances
160         local images = { }
161
162         -- find requested plugin and instance
163     for i, p in ipairs( luci.dispatcher.context.path ) do
164         if luci.dispatcher.context.path[i] == "graph" then
165             plugin    = luci.dispatcher.context.path[i+1]
166             instances = { luci.dispatcher.context.path[i+2] }
167         end
168     end
169
170         -- no instance requested, find all instances
171         if #instances == 0 then
172                 --instances = { graph.tree:plugin_instances( plugin )[1] }
173                 instances = graph.tree:plugin_instances( plugin )
174                 is_index = (#instances > 1)
175
176         -- index instance requested
177         elseif instances[1] == "-" then
178                 instances[1] = ""
179                 is_index = true
180         end
181
182
183         -- render graphs
184         for i, inst in luci.util.vspairs( instances ) do
185                 for i, img in luci.util.vspairs( graph:render( plugin, inst, is_index ) ) do
186                         table.insert( images, graph:strippngpath( img ) )
187                         images[images[#images]] = inst
188                 end
189         end
190
191         luci.template.render( "public_statistics/graph", {
192                 images           = images,
193                 plugin           = plugin,
194                 timespans        = spans,
195                 current_timespan = span,
196                 hosts            = hosts,
197                 current_host     = host,
198                 is_index         = is_index
199         } )
200 end