* luci/statistics: template and controller translation
[project/luci.git] / applications / luci-statistics / luasrc / controller / luci_statistics / luci_statistics.lua
1 module("luci.controller.luci_statistics.luci_statistics", package.seeall)
2
3 require("luci.fs")
4 require("luci.i18n")
5 require("luci.template")
6
7
8 function index()
9
10         require("luci.i18n")
11         require("luci.statistics.datatree")
12
13         -- load language file
14         luci.i18n.load("statistics.en") -- XXX: temporary / replace with loadc()
15
16         -- get rrd data tree
17         local tree = luci.statistics.datatree.Instance()
18
19         -- override entry(): check for existance <plugin>.so where <plugin> is derived from the called path
20         function _entry( path, ... )
21                 local file = path[4] or path[3]
22                 if luci.fs.isfile( "/usr/lib/collectd/" .. file .. ".so" ) then
23                         entry( path, ... )
24                 end
25         end
26
27         -- override call(): call requested action function with supplied parameters
28         function _call( func, tree, plugin )
29                 return function() getfenv()[func]( tree, plugin ) end
30         end
31
32         -- override i18n(): try to translate stat_<str> or fall back to <str>
33         function _i18n( str )
34                 return luci.i18n.translate( "stat_" .. str, str )
35         end
36
37
38         entry({"admin", "statistics"},                          call("statistics_index"),               _i18n("statistics"),    80).i18n = "statistics"
39         entry({"admin", "statistics", "collectd"},              cbi("luci_statistics/collectd"),        _i18n("collectd"),      10)
40
41         entry({"admin", "statistics", "output"},                call("statistics_outputplugins"),       _i18n("outputplugins"), 20)
42         _entry({"admin", "statistics", "output", "rrdtool"},    cbi("luci_statistics/rrdtool"),         _i18n("rrdtool"),       10)
43         _entry({"admin", "statistics", "output", "network"},    cbi("luci_statistics/network"),         _i18n("network"),       20)
44         _entry({"admin", "statistics", "output", "unixsock"},   cbi("luci_statistics/unixsock"),        _i18n("unixsock"),      30)
45         _entry({"admin", "statistics", "output", "csv"},        cbi("luci_statistics/csv"),             _i18n("csv"),           40)
46
47         entry({"admin", "statistics", "system"},                call("statistics_systemplugins"),       _i18n("systemplugins"), 30)
48         _entry({"admin", "statistics", "system", "exec"},       cbi("luci_statistics/exec"),            _i18n("exec"),          10)
49         _entry({"admin", "statistics", "system", "email"},      cbi("luci_statistics/email"),           _i18n("email"),         20)
50         _entry({"admin", "statistics", "system", "cpu"},        cbi("luci_statistics/cpu"),             _i18n("cpu"),           30)
51         _entry({"admin", "statistics", "system", "df"},         cbi("luci_statistics/df"),              _i18n("df"),            40)
52         _entry({"admin", "statistics", "system", "disk"},       cbi("luci_statistics/disk"),            _i18n("disk"),          50)
53         _entry({"admin", "statistics", "system", "irq"},        cbi("luci_statistics/irq"),             _i18n("irq"),           60)
54         _entry({"admin", "statistics", "system", "processes"},  cbi("luci_statistics/processes"),       _i18n("processes"),     70)
55
56         entry({"admin", "statistics", "network"},               call("statistics_networkplugins"),      _i18n("networkplugins"),40)
57         _entry({"admin", "statistics", "network", "interface"}, cbi("luci_statistics/interface"),       _i18n("interface"),     10)
58         _entry({"admin", "statistics", "network", "netlink"},   cbi("luci_statistics/netlink"),         _i18n("netlink"),       20)
59         _entry({"admin", "statistics", "network", "iptables"},  cbi("luci_statistics/iptables"),        _i18n("iptables"),      30)
60         _entry({"admin", "statistics", "network", "tcpconns"},  cbi("luci_statistics/tcpconns"),        _i18n("tcpconns"),      40)
61         _entry({"admin", "statistics", "network", "ping"},      cbi("luci_statistics/ping"),            _i18n("ping"),          50)
62         _entry({"admin", "statistics", "network", "dns"},       cbi("luci_statistics/dns"),             _i18n("dns"),           60)
63         _entry({"admin", "statistics", "network", "wireless"},  cbi("luci_statistics/wireless"),        _i18n("wireless"),      70)
64
65         
66         -- public views
67         entry({"freifunk", "statistics"}, call("statistics_index"), "Statistiken", 80).i18n = "statistics"
68
69         for i, plugin in ipairs( tree:plugins() ) do
70
71                 -- get plugin instances
72                 local instances = tree:plugin_instances( plugin )
73
74                 -- plugin menu entry
75                 _entry( { "freifunk", "statistics", plugin }, call("statistics_render"), _i18n( plugin ), i )
76
77                 -- if more then one instance is found then generate submenu
78                 if #instances > 1 then
79                         for j, inst in ipairs(instances) do
80                                 -- instance menu entry
81                                 entry(
82                                         { "freifunk", "statistics", plugin, inst },
83                                         call("statistics_render"), inst, j
84                                 )
85                         end
86                 end                     
87         end
88 end
89
90
91 function statistics_index()
92         luci.template.render("admin_statistics/index")
93 end
94
95 function statistics_outputplugins()
96         local plugins = { }
97
98         for i, p in ipairs({ "rrdtool", "network", "unixsock", "csv" }) do
99                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
100         end
101
102         luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
103 end
104
105 function statistics_systemplugins()
106         local plugins = { }
107
108         for i, p in ipairs({ "exec", "email", "df", "disk", "irq", "processes", "cpu" }) do
109                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
110         end
111
112         luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
113 end
114
115 function statistics_networkplugins()
116         local plugins = { }
117
118         for i, p in ipairs({ "interface", "netlink", "iptables", "tcpconns", "ping", "dns", "wireless" }) do
119                 plugins[p] = luci.i18n.translate( "stat_" .. p, p )
120         end
121
122         luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
123 end
124
125
126 function statistics_render( tree )
127
128         require("luci.statistics.rrdtool")
129         require("luci.template")
130
131         local req   = luci.dispatcher.request 
132         local graph = luci.statistics.rrdtool.Graph()
133
134         local plugin    = req[3]
135         local instances = { req[4] }
136         local images    = { }
137
138         -- no instance requested, find all instances
139         if #instances == 0 then
140
141                 instances = graph.tree:plugin_instances( plugin )
142
143                 -- more than one available instance
144                 if #instances > 1 then
145
146                         -- redirect to first instance and return
147                         local r = luci.dispatcher.request
148                         local i = instances[1]
149                         if i:len() == 0 then i = "-" end
150
151                         luci.http.redirect( luci.dispatcher.build_url(
152                                 req[1], req[2], req[3], i
153                         ) )
154
155                         return
156                 end
157
158         -- index instance requested
159         elseif instances[1] == "-" then
160                 instances[1] = ""
161         end
162
163
164         -- render graphs
165         for i, inst in ipairs( instances ) do
166                 for i, img in ipairs( graph:render( plugin, inst ) ) do
167                         table.insert( images, graph:strippngpath( img ) )
168                 end
169         end
170
171         luci.template.render("public_statistics/graph", { images=images, plugin=plugin } )
172 end