]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/interface.py
f0dcef50325bf22c00e0ec39426709227e4ad0f0
[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 import os
23
24 from . import base
25 from .. import util
26
27 from ..i18n import _
28
29 COLOUR_RX = "B22222"
30 COLOUR_RX_AREA = "%sAA" % COLOUR_RX
31 COLOUR_TX = "228B22"
32 COLOUR_TX_AREA = "%sAA" % COLOUR_TX
33
34 class GraphTemplateInterfaceBase(base.GraphTemplate):
35 @property
36 def interface(self):
37 return self.object.interface
38
39
40 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase):
41 name = "interface-bits"
42
43 @property
44 def rrd_graph(self):
45 _ = self.locale.translate
46
47 return [
48 "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
49 "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
50
51 # Convert everything into bits.
52 "CDEF:bits_rx=bytes_rx,8,*",
53 "CDEF:bits_tx=bytes_tx,8,*",
54
55 # Compute 95% lines.
56 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
57 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
58
59 # Draw the received area.
60 "AREA:bits_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
61 "VDEF:bits_rx_min=bits_rx,MINIMUM",
62 "VDEF:bits_rx_max=bits_rx,MAXIMUM",
63 "VDEF:bits_rx_avg=bits_rx,AVERAGE",
64 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
65 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
66 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
67
68 # Draw the transmitted area.
69 "AREA:bits_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
70 "VDEF:bits_tx_min=bits_tx,MINIMUM",
71 "VDEF:bits_tx_max=bits_tx,MAXIMUM",
72 "VDEF:bits_tx_avg=bits_tx,AVERAGE",
73 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
74 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
75 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
76
77 # Draw outlines.
78 "LINE1:bits_rx#%s" % COLOUR_RX,
79 "LINE1:bits_tx#%s" % COLOUR_TX,
80
81 # Draw the 95% lines.
82 "COMMENT:--- %s ---\\n" % _("95th percentile"),
83 "LINE2:bits_rx_95p#%s:%-15s" % (COLOUR_RX, _("Received")),
84 "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n",
85 "LINE2:bits_tx_95p#%s:%-15s" % (COLOUR_TX, _("Transmitted")),
86 "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n",
87 ]
88
89 @property
90 def graph_title(self):
91 _ = self.locale.translate
92 return _("Bandwidth usage on %s") % self.interface
93
94 @property
95 def graph_vertical_label(self):
96 _ = self.locale.translate
97 return _("Bit/s")
98
99
100 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
101 name = "interface-packets"
102
103 @property
104 def rrd_graph(self):
105 _ = self.locale.translate
106
107 return [
108 "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
109 "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
110
111 # Draw the received area.
112 "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
113 "VDEF:packets_rx_min=packets_rx,MINIMUM",
114 "VDEF:packets_rx_max=packets_rx,MAXIMUM",
115 "VDEF:packets_rx_avg=packets_rx,AVERAGE",
116 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
117 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
118 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
119
120 # Draw the transmitted area.
121 "AREA:packets_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
122 "VDEF:packets_tx_min=packets_tx,MINIMUM",
123 "VDEF:packets_tx_max=packets_tx,MAXIMUM",
124 "VDEF:packets_tx_avg=packets_tx,AVERAGE",
125 "GPRINT:packets_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
126 "GPRINT:packets_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
127 "GPRINT:packets_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
128
129 # Draw outlines of the areas on top.
130 "LINE1:packets_rx#%s" % COLOUR_RX,
131 "LINE1:packets_tx#%s" % COLOUR_TX,
132 ]
133
134 @property
135 def graph_title(self):
136 _ = self.locale.translate
137 return _("Transferred packets on %s") % self.interface
138
139 @property
140 def graph_vertical_label(self):
141 _ = self.locale.translate
142 return _("Packets/s")
143
144
145 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
146 name = "interface-errors"
147
148 @property
149 def rrd_graph(self):
150 _ = self.locale.translate
151
152 return [
153 "DEF:errors_rx=%(file)s:errors_rx:AVERAGE",
154 "DEF:errors_tx=%(file)s:errors_tx:AVERAGE",
155 "DEF:dropped_rx=%(file)s:dropped_rx:AVERAGE",
156 "DEF:dropped_tx=%(file)s:dropped_tx:AVERAGE",
157 "DEF:collisions=%(file)s:collisions:AVERAGE",
158
159 # Invert the transmitted packets to create upside down graph.
160 "CDEF:errors_tx_inv=errors_tx,-1,*",
161 "CDEF:dropped_tx_inv=dropped_tx,-1,*",
162
163 # Draw the receive errors.
164 "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"),
165 "VDEF:errors_rx_min=errors_rx,MINIMUM",
166 "VDEF:errors_rx_max=errors_rx,MAXIMUM",
167 "VDEF:errors_rx_avg=errors_rx,AVERAGE",
168 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
169 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
170 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
171 "LINE1:errors_rx#228B22",
172
173 # Draw the transmit errors.
174 "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"),
175 "VDEF:errors_tx_min=errors_tx,MINIMUM",
176 "VDEF:errors_tx_max=errors_tx,MAXIMUM",
177 "VDEF:errors_tx_avg=errors_tx,AVERAGE",
178 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
179 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
180 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
181 "LINE1:errors_tx_inv#B22222",
182
183 # Draw the receive drops.
184 "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"),
185 "VDEF:dropped_rx_min=dropped_rx,MINIMUM",
186 "VDEF:dropped_rx_max=dropped_rx,MAXIMUM",
187 "VDEF:dropped_rx_avg=dropped_rx,AVERAGE",
188 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
189 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
190 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
191 "LINE1:dropped_rx#228B22",
192
193 # Draw the transmit drops.
194 "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"),
195 "VDEF:dropped_tx_min=dropped_tx,MINIMUM",
196 "VDEF:dropped_tx_max=dropped_tx,MAXIMUM",
197 "VDEF:dropped_tx_avg=dropped_tx,AVERAGE",
198 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
199 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
200 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
201 "LINE1:dropped_tx#B22222",
202
203 # Draw the collisions as a line.
204 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
205 ]
206
207 @property
208 def graph_title(self):
209 _ = self.locale.translate
210 return _("Errors/dropped packets on %s") % self.interface
211
212 @property
213 def graph_vertical_label(self):
214 _ = self.locale.translate
215 return _("Packets/s")
216
217
218 class InterfaceObject(base.Object):
219 rrd_schema = [
220 "DS:bytes_rx:DERIVE:0:U",
221 "DS:bytes_tx:DERIVE:0:U",
222 "DS:collisions:DERIVE:0:U",
223 "DS:dropped_rx:DERIVE:0:U",
224 "DS:dropped_tx:DERIVE:0:U",
225 "DS:errors_rx:DERIVE:0:U",
226 "DS:errors_tx:DERIVE:0:U",
227 "DS:multicast:DERIVE:0:U",
228 "DS:packets_rx:DERIVE:0:U",
229 "DS:packets_tx:DERIVE:0:U",
230 ]
231
232 def __repr__(self):
233 return "<%s %s>" % (self.__class__.__name__, self.interface)
234
235 def init(self, interface):
236 self.interface = interface
237
238 @property
239 def id(self):
240 return self.interface
241
242 def collect(self):
243 interface_path = os.path.join("/sys/class/net", self.interface)
244
245 # Check if the interface exists.
246 if not os.path.exists(interface_path):
247 self.log.debug(_("Interface %s does not exists. Cannot collect.") \
248 % self.interface)
249 return
250
251 files = (
252 "rx_bytes", "tx_bytes",
253 "collisions",
254 "rx_dropped", "tx_dropped",
255 "rx_errors", "tx_errors",
256 "multicast",
257 "rx_packets", "tx_packets",
258 )
259 ret = []
260
261 for file in files:
262 path = os.path.join(interface_path, "statistics", file)
263
264 # Open file and read it's content.
265 f = None
266 try:
267 f = open(path)
268
269 line = f.readline()
270 line = line.strip()
271 ret.append(line)
272 except:
273 ret.append("0")
274 raise
275
276 finally:
277 if f:
278 f.close()
279
280 return ret
281
282
283 class InterfacePlugin(base.Plugin):
284 name = "interface"
285 description = "Interface Statistics Plugin"
286
287 templates = [
288 GraphTemplateInterfaceBits,
289 GraphTemplateInterfacePackets,
290 GraphTemplateInterfaceErrors,
291 ]
292
293 interval = 30
294
295 @property
296 def objects(self):
297 for interface in util.get_network_interfaces():
298 yield InterfaceObject(self, interface=interface)