f19d2cdb7fe6af7c0829f421a9c76f85785bc1c1
[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 _entry({"admin", "statistics", "network", "wireless"}, cbi("luci_statistics/wireless"), "Drahtlos", 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 plugins = {
97 rrdtool="RRDTool",
98 network="Netzwerk",
99 unixsock="Unix Socket",
100 csv="CSV"
101 }
102
103 luci.template.render("admin_statistics/outputplugins", {plugins=plugins})
104 end
105
106 function statistics_systemplugins()
107 plugins = {
108 exec="Exec",
109 email="E-Mail",
110 disk="Datenträger",
111 irq="Interrupts",
112 processes="Prozesse"
113 }
114
115 luci.template.render("admin_statistics/systemplugins", {plugins=plugins})
116 end
117
118 function statistics_networkplugins()
119 plugins = {
120 interface="Schnittstellen",
121 netlink="Netlink",
122 iptables="Firewall",
123 tcpconns="Verbindungen",
124 ping="Ping",
125 dns="DNS"
126 }
127
128 luci.template.render("admin_statistics/networkplugins", {plugins=plugins})
129 end
130
131
132 function statistics_render( tree )
133
134 require("luci.statistics.rrdtool")
135 require("luci.template")
136
137 local req = luci.dispatcher.request
138 local graph = luci.statistics.rrdtool.Graph()
139
140 local plugin = req[3]
141 local instances = { req[4] }
142 local images = { }
143
144 -- no instance requested, find all instances
145 if #instances == 0 then
146
147 instances = graph.tree:plugin_instances( plugin )
148
149 -- more than one available instance
150 if #instances > 1 then
151
152 -- redirect to first instance and return
153 local r = luci.dispatcher.request
154 local i = instances[1]
155 if i:len() == 0 then i = "-" end
156
157 luci.http.redirect( luci.dispatcher.build_url(
158 req[1], req[2], req[3], i
159 ) )
160
161 return
162 end
163
164 -- index instance requested
165 elseif instances[1] == "-" then
166 instances[1] = ""
167 end
168
169
170 -- render graphs
171 for i, inst in ipairs( instances ) do
172 for i, img in ipairs( graph:render( plugin, inst ) ) do
173 table.insert( images, graph:strippngpath( img ) )
174 end
175 end
176
177 luci.template.render("public_statistics/graph", { images=images, plugin=plugin } )
178 end