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