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