]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/interface.py
Add graphs for IP fragmentation
[collecty.git] / src / collecty / plugins / interface.py
CommitLineData
f37913e8 1#!/usr/bin/python3
89b70961
MT
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
89b70961
MT
22import os
23
f37913e8 24from . import base
29455c5f 25from .. import util
89b70961
MT
26
27from ..i18n import _
28
bba5cf66
MT
29COLOUR_RX = "B22222"
30COLOUR_RX_AREA = "%sAA" % COLOUR_RX
31COLOUR_TX = "228B22"
32COLOUR_TX_AREA = "%sAA" % COLOUR_TX
e9bb328f 33
2a3d5c1b
MT
34class GraphTemplateInterfaceBase(base.GraphTemplate):
35 @property
36 def interface(self):
37 return self.object.interface
38
39
40class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase):
bba5cf66
MT
41 name = "interface-bits"
42
43 @property
44 def rrd_graph(self):
c0ebfd25
MT
45 _ = self.locale.translate
46
bba5cf66
MT
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
f181246a 90 def graph_title(self):
c0ebfd25 91 _ = self.locale.translate
2a3d5c1b 92 return _("Bandwidth usage on %s") % self.interface
f181246a
MT
93
94 @property
95 def graph_vertical_label(self):
c0ebfd25 96 _ = self.locale.translate
f181246a 97 return _("Bit/s")
e9bb328f
MT
98
99
2a3d5c1b 100class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
e9bb328f
MT
101 name = "interface-packets"
102
bba5cf66
MT
103 @property
104 def rrd_graph(self):
c0ebfd25
MT
105 _ = self.locale.translate
106
bba5cf66
MT
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 ]
e9bb328f 133
bba5cf66 134 @property
f181246a 135 def graph_title(self):
c0ebfd25 136 _ = self.locale.translate
2a3d5c1b 137 return _("Transferred packets on %s") % self.interface
f181246a
MT
138
139 @property
140 def graph_vertical_label(self):
c0ebfd25 141 _ = self.locale.translate
f181246a 142 return _("Packets/s")
e9bb328f
MT
143
144
2a3d5c1b 145class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
e9bb328f
MT
146 name = "interface-errors"
147
bba5cf66
MT
148 @property
149 def rrd_graph(self):
c0ebfd25
MT
150 _ = self.locale.translate
151
bba5cf66
MT
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 ]
e9bb328f 206
bba5cf66 207 @property
f181246a 208 def graph_title(self):
c0ebfd25 209 _ = self.locale.translate
2a3d5c1b 210 return _("Errors/dropped packets on %s") % self.interface
f181246a
MT
211
212 @property
213 def graph_vertical_label(self):
c0ebfd25 214 _ = self.locale.translate
f181246a 215 return _("Packets/s")
e9bb328f
MT
216
217
72364063 218class InterfaceObject(base.Object):
89b70961
MT
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
72364063
MT
232 def __repr__(self):
233 return "<%s %s>" % (self.__class__.__name__, self.interface)
bba5cf66 234
72364063
MT
235 def init(self, interface):
236 self.interface = interface
89b70961
MT
237
238 @property
239 def id(self):
72364063 240 return self.interface
89b70961 241
72364063 242 def collect(self):
29455c5f 243 interface_path = os.path.join("/sys/class/net", self.interface)
bba5cf66
MT
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
89b70961
MT
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 )
bba5cf66 259 ret = []
89b70961
MT
260
261 for file in files:
bba5cf66 262 path = os.path.join(interface_path, "statistics", file)
89b70961
MT
263
264 # Open file and read it's content.
bba5cf66
MT
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
f648421a 280 return ret
72364063
MT
281
282
283class InterfacePlugin(base.Plugin):
284 name = "interface"
285 description = "Interface Statistics Plugin"
286
287 templates = [
288 GraphTemplateInterfaceBits,
289 GraphTemplateInterfacePackets,
290 GraphTemplateInterfaceErrors,
291 ]
292
b064255e 293 interval = 30
72364063 294
72364063
MT
295 @property
296 def objects(self):
29455c5f 297 for interface in util.get_network_interfaces():
72364063 298 yield InterfaceObject(self, interface=interface)