]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/interface.py
interface: Localise plugin
[collecty.git] / src / collecty / plugins / interface.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2012 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22
23
24 import os
25
26 from . import base
27
28 from ..i18n import _
29
30 SYS_CLASS_NET = "/sys/class/net"
31
32 COLOUR_RX = "B22222"
33 COLOUR_RX_AREA = "%sAA" % COLOUR_RX
34 COLOUR_TX = "228B22"
35 COLOUR_TX_AREA = "%sAA" % COLOUR_TX
36
37 class GraphTemplateInterfaceBase(base.GraphTemplate):
38 @property
39 def interface(self):
40 return self.object.interface
41
42
43 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase):
44 name = "interface-bits"
45
46 @property
47 def rrd_graph(self):
48 _ = self.locale.translate
49
50 return [
51 "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
52 "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
53
54 # Convert everything into bits.
55 "CDEF:bits_rx=bytes_rx,8,*",
56 "CDEF:bits_tx=bytes_tx,8,*",
57
58 # Compute 95% lines.
59 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
60 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
61
62 # Draw the received area.
63 "AREA:bits_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
64 "VDEF:bits_rx_min=bits_rx,MINIMUM",
65 "VDEF:bits_rx_max=bits_rx,MAXIMUM",
66 "VDEF:bits_rx_avg=bits_rx,AVERAGE",
67 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
68 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
69 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
70
71 # Draw the transmitted area.
72 "AREA:bits_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
73 "VDEF:bits_tx_min=bits_tx,MINIMUM",
74 "VDEF:bits_tx_max=bits_tx,MAXIMUM",
75 "VDEF:bits_tx_avg=bits_tx,AVERAGE",
76 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
77 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
78 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
79
80 # Draw outlines.
81 "LINE1:bits_rx#%s" % COLOUR_RX,
82 "LINE1:bits_tx#%s" % COLOUR_TX,
83
84 # Draw the 95% lines.
85 "COMMENT:--- %s ---\\n" % _("95th percentile"),
86 "LINE2:bits_rx_95p#%s:%-15s" % (COLOUR_RX, _("Received")),
87 "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n",
88 "LINE2:bits_tx_95p#%s:%-15s" % (COLOUR_TX, _("Transmitted")),
89 "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n",
90 ]
91
92 @property
93 def graph_title(self):
94 _ = self.locale.translate
95 return _("Bandwidth usage on %s") % self.interface
96
97 @property
98 def graph_vertical_label(self):
99 _ = self.locale.translate
100 return _("Bit/s")
101
102
103 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
104 name = "interface-packets"
105
106 @property
107 def rrd_graph(self):
108 _ = self.locale.translate
109
110 return [
111 "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
112 "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
113
114 # Draw the received area.
115 "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
116 "VDEF:packets_rx_min=packets_rx,MINIMUM",
117 "VDEF:packets_rx_max=packets_rx,MAXIMUM",
118 "VDEF:packets_rx_avg=packets_rx,AVERAGE",
119 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
120 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
121 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
122
123 # Draw the transmitted area.
124 "AREA:packets_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
125 "VDEF:packets_tx_min=packets_tx,MINIMUM",
126 "VDEF:packets_tx_max=packets_tx,MAXIMUM",
127 "VDEF:packets_tx_avg=packets_tx,AVERAGE",
128 "GPRINT:packets_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
129 "GPRINT:packets_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
130 "GPRINT:packets_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
131
132 # Draw outlines of the areas on top.
133 "LINE1:packets_rx#%s" % COLOUR_RX,
134 "LINE1:packets_tx#%s" % COLOUR_TX,
135 ]
136
137 @property
138 def graph_title(self):
139 _ = self.locale.translate
140 return _("Transferred packets on %s") % self.interface
141
142 @property
143 def graph_vertical_label(self):
144 _ = self.locale.translate
145 return _("Packets/s")
146
147
148 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
149 name = "interface-errors"
150
151 @property
152 def rrd_graph(self):
153 _ = self.locale.translate
154
155 return [
156 "DEF:errors_rx=%(file)s:errors_rx:AVERAGE",
157 "DEF:errors_tx=%(file)s:errors_tx:AVERAGE",
158 "DEF:dropped_rx=%(file)s:dropped_rx:AVERAGE",
159 "DEF:dropped_tx=%(file)s:dropped_tx:AVERAGE",
160 "DEF:collisions=%(file)s:collisions:AVERAGE",
161
162 # Invert the transmitted packets to create upside down graph.
163 "CDEF:errors_tx_inv=errors_tx,-1,*",
164 "CDEF:dropped_tx_inv=dropped_tx,-1,*",
165
166 # Draw the receive errors.
167 "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"),
168 "VDEF:errors_rx_min=errors_rx,MINIMUM",
169 "VDEF:errors_rx_max=errors_rx,MAXIMUM",
170 "VDEF:errors_rx_avg=errors_rx,AVERAGE",
171 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
172 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
173 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
174 "LINE1:errors_rx#228B22",
175
176 # Draw the transmit errors.
177 "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"),
178 "VDEF:errors_tx_min=errors_tx,MINIMUM",
179 "VDEF:errors_tx_max=errors_tx,MAXIMUM",
180 "VDEF:errors_tx_avg=errors_tx,AVERAGE",
181 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
182 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
183 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
184 "LINE1:errors_tx_inv#B22222",
185
186 # Draw the receive drops.
187 "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"),
188 "VDEF:dropped_rx_min=dropped_rx,MINIMUM",
189 "VDEF:dropped_rx_max=dropped_rx,MAXIMUM",
190 "VDEF:dropped_rx_avg=dropped_rx,AVERAGE",
191 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
192 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
193 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
194 "LINE1:dropped_rx#228B22",
195
196 # Draw the transmit drops.
197 "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"),
198 "VDEF:dropped_tx_min=dropped_tx,MINIMUM",
199 "VDEF:dropped_tx_max=dropped_tx,MAXIMUM",
200 "VDEF:dropped_tx_avg=dropped_tx,AVERAGE",
201 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
202 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
203 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
204 "LINE1:dropped_tx#B22222",
205
206 # Draw the collisions as a line.
207 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
208 ]
209
210 @property
211 def graph_title(self):
212 _ = self.locale.translate
213 return _("Errors/dropped packets on %s") % self.interface
214
215 @property
216 def graph_vertical_label(self):
217 _ = self.locale.translate
218 return _("Packets/s")
219
220
221 class InterfaceObject(base.Object):
222 rrd_schema = [
223 "DS:bytes_rx:DERIVE:0:U",
224 "DS:bytes_tx:DERIVE:0:U",
225 "DS:collisions:DERIVE:0:U",
226 "DS:dropped_rx:DERIVE:0:U",
227 "DS:dropped_tx:DERIVE:0:U",
228 "DS:errors_rx:DERIVE:0:U",
229 "DS:errors_tx:DERIVE:0:U",
230 "DS:multicast:DERIVE:0:U",
231 "DS:packets_rx:DERIVE:0:U",
232 "DS:packets_tx:DERIVE:0:U",
233 ]
234
235 def __repr__(self):
236 return "<%s %s>" % (self.__class__.__name__, self.interface)
237
238 def init(self, interface):
239 self.interface = interface
240
241 @property
242 def id(self):
243 return self.interface
244
245 def collect(self):
246 interface_path = os.path.join(SYS_CLASS_NET, self.interface)
247
248 # Check if the interface exists.
249 if not os.path.exists(interface_path):
250 self.log.debug(_("Interface %s does not exists. Cannot collect.") \
251 % self.interface)
252 return
253
254 files = (
255 "rx_bytes", "tx_bytes",
256 "collisions",
257 "rx_dropped", "tx_dropped",
258 "rx_errors", "tx_errors",
259 "multicast",
260 "rx_packets", "tx_packets",
261 )
262 ret = []
263
264 for file in files:
265 path = os.path.join(interface_path, "statistics", file)
266
267 # Open file and read it's content.
268 f = None
269 try:
270 f = open(path)
271
272 line = f.readline()
273 line = line.strip()
274 ret.append(line)
275 except:
276 ret.append("0")
277 raise
278
279 finally:
280 if f:
281 f.close()
282
283 return ret
284
285
286 class InterfacePlugin(base.Plugin):
287 name = "interface"
288 description = "Interface Statistics Plugin"
289
290 templates = [
291 GraphTemplateInterfaceBits,
292 GraphTemplateInterfacePackets,
293 GraphTemplateInterfaceErrors,
294 ]
295
296 interval = 30
297
298 def get_interfaces(self):
299 for interface in os.listdir(SYS_CLASS_NET):
300 # Skip some unwanted interfaces.
301 if interface == "lo" or interface.startswith("mon."):
302 continue
303
304 path = os.path.join(SYS_CLASS_NET, interface)
305 if not os.path.isdir(path):
306 continue
307
308 yield interface
309
310 @property
311 def objects(self):
312 for interface in self.get_interfaces():
313 yield InterfaceObject(self, interface=interface)