566e137575a16a6c1e137542d99c1086ea35b9dd
[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")
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"),               "Statistiken",          80)
39         entry({"admin", "statistics", "collectd"},              cbi("luci_statistics/collectd"),        "Collectd",             10)
40
41         entry({"admin", "statistics", "output"},                call("statistics_outputplugins"),       "Ausgabeplugins",       20)
42         _entry({"admin", "statistics", "output", "rrdtool"},    cbi("luci_statistics/rrdtool"),         "RRDTool",              10)
43         _entry({"admin", "statistics", "output", "network"},    cbi("luci_statistics/network"),         "Netzwerk",             20)
44         _entry({"admin", "statistics", "output", "unixsock"},   cbi("luci_statistics/unixsock"),        "Unix Socket",          30)
45         _entry({"admin", "statistics", "output", "csv"},        cbi("luci_statistics/csv"),             "CSV",                  40)
46
47         entry({"admin", "statistics", "system"},                call("statistics_systemplugins"),       "Systemplugins",        30)
48         _entry({"admin", "statistics", "system", "exec"},       cbi("luci_statistics/exec"),            "Exec",                 10)
49         _entry({"admin", "statistics", "system", "email"},      cbi("luci_statistics/email"),           "E-Mail",               20)
50         _entry({"admin", "statistics", "system", "cpu"},        cbi("luci_statistics/cpu"),             "Prozessor",            30)
51         _entry({"admin", "statistics", "system", "df"},         cbi("luci_statistics/df"),              "Speicherplatz",        40)
52         _entry({"admin", "statistics", "system", "disk"},       cbi("luci_statistics/disk"),            "Datenträger",         50)
53         _entry({"admin", "statistics", "system", "irq"},        cbi("luci_statistics/irq"),             "Interrupts",           60)
54         _entry({"admin", "statistics", "system", "processes"},  cbi("luci_statistics/processes"),       "Prozesse",             70)
55
56         entry({"admin", "statistics", "network"},               call("statistics_networkplugins"),      "Netzwerkplugins",      40)
57         _entry({"admin", "statistics", "network", "interface"}, cbi("luci_statistics/interface"),       "Schnittstellen",       10)
58         _entry({"admin", "statistics", "network", "netlink"},   cbi("luci_statistics/netlink"),         "Netlink",              20)
59         _entry({"admin", "statistics", "network", "iptables"},  cbi("luci_statistics/iptables"),        "Firewall",             30)
60         _entry({"admin", "statistics", "network", "tcpconns"},  cbi("luci_statistics/tcpconns"),        "Verbindungen",         40)
61         _entry({"admin", "statistics", "network", "ping"},      cbi("luci_statistics/ping"),            "Ping",                 50)
62         _entry({"admin", "statistics", "network", "dns"},       cbi("luci_statistics/dns"),             "DNS",                  60)
63
64         
65         -- public views
66         entry({"freifunk", "statistics"}, call("statistics_index"), "Statistiken", 80).i18n = "statistics"
67
68         for i, plugin in ipairs( tree:plugins() ) do
69
70                 -- get plugin instances
71                 local instances = tree:plugin_instances( plugin )
72
73                 -- plugin menu entry
74                 _entry( { "freifunk", "statistics", plugin }, call("statistics_render"), _i18n( plugin ), i )
75
76                 -- if more then one instance is found then generate submenu
77                 if #instances > 1 then
78                         for j, inst in ipairs(instances) do
79                                 -- instance menu entry
80                                 entry(
81                                         { "freifunk", "statistics", plugin, inst },
82                                         call("statistics_render"), inst, j
83                                 )
84                         end
85                 end                     
86         end
87 end
88
89
90 function statistics_index()
91         luci.template.render("admin_statistics/index")
92 end
93
94 function statistics_outputplugins()
95         plugins = {
96                 rrdtool="RRDTool",
97                 network="Netzwerk",
98                 unixsock="Unix Socket",
99                 csv="CSV"
100         }
101
102         luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
103 end
104
105 function statistics_systemplugins()
106         plugins = {
107                 exec="Exec",
108                 email="E-Mail",
109                 disk="Datenträger",
110                 irq="Interrupts",
111                 processes="Prozesse"
112         }
113
114         luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
115 end
116
117 function statistics_networkplugins()
118         plugins = {
119                 interface="Schnittstellen",
120                 netlink="Netlink",
121                 iptables="Firewall",
122                 tcpconns="Verbindungen",
123                 ping="Ping",
124                 dns="DNS"
125         }
126
127         luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
128 end
129
130
131 function statistics_render( tree )
132
133         require("luci.statistics.rrdtool")
134         require("luci.template")
135
136         local req   = luci.dispatcher.request 
137         local graph = luci.statistics.rrdtool.Graph()
138
139         local plugin    = req[3]
140         local instances = { req[4] }
141         local images    = { }
142
143         -- no instance requested, find all instances
144         if #instances == 0 then
145
146                 instances = graph.tree:plugin_instances( plugin )
147
148                 -- more than one available instance
149                 if #instances > 1 then
150
151                         -- redirect to first instance and return
152                         local r = luci.dispatcher.request
153                         local i = instances[1]
154                         if i:len() == 0 then i = "-" end
155
156                         luci.http.redirect( luci.dispatcher.build_url(
157                                 req[1], req[2], req[3], i
158                         ) )
159
160                         return
161                 end
162
163         -- index instance requested
164         elseif instances[1] == "-" then
165                 instances[1] = ""
166         end
167
168
169         -- render graphs
170         for i, inst in ipairs( instances ) do
171                 for i, img in ipairs( graph:render( plugin, inst ) ) do
172                         table.insert( images, img )
173                 end
174         end
175
176         luci.template.render("public_statistics/graph", { images=images, plugin=plugin } )
177 end