]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/interface.py
Add some magic to collecty that makes the graph templates smaller
[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 47 return [
bba5cf66
MT
48 # Convert everything into bits.
49 "CDEF:bits_rx=bytes_rx,8,*",
50 "CDEF:bits_tx=bytes_tx,8,*",
51
52 # Compute 95% lines.
53 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
54 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
55
56 # Draw the received area.
57 "AREA:bits_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
bba5cf66
MT
58 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
59 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
60 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
61
62 # Draw the transmitted area.
63 "AREA:bits_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
bba5cf66
MT
64 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
65 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
66 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
67
68 # Draw outlines.
69 "LINE1:bits_rx#%s" % COLOUR_RX,
70 "LINE1:bits_tx#%s" % COLOUR_TX,
71
72 # Draw the 95% lines.
73 "COMMENT:--- %s ---\\n" % _("95th percentile"),
74 "LINE2:bits_rx_95p#%s:%-15s" % (COLOUR_RX, _("Received")),
75 "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n",
76 "LINE2:bits_tx_95p#%s:%-15s" % (COLOUR_TX, _("Transmitted")),
77 "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n",
78 ]
79
80 @property
f181246a 81 def graph_title(self):
c0ebfd25 82 _ = self.locale.translate
2a3d5c1b 83 return _("Bandwidth usage on %s") % self.interface
f181246a
MT
84
85 @property
86 def graph_vertical_label(self):
c0ebfd25 87 _ = self.locale.translate
f181246a 88 return _("Bit/s")
e9bb328f
MT
89
90
2a3d5c1b 91class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
e9bb328f
MT
92 name = "interface-packets"
93
bba5cf66
MT
94 @property
95 def rrd_graph(self):
c0ebfd25
MT
96 _ = self.locale.translate
97
bba5cf66 98 return [
bba5cf66
MT
99 # Draw the received area.
100 "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
bba5cf66
MT
101 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
102 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
103 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
104
105 # Draw the transmitted area.
106 "AREA:packets_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")),
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.
112 "LINE1:packets_rx#%s" % COLOUR_RX,
113 "LINE1:packets_tx#%s" % COLOUR_TX,
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.
140 "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"),
bba5cf66
MT
141 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
142 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
143 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
144 "LINE1:errors_rx#228B22",
145
146 # Draw the transmit errors.
147 "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"),
bba5cf66
MT
148 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
149 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
150 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
151 "LINE1:errors_tx_inv#B22222",
152
153 # Draw the receive drops.
154 "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"),
bba5cf66
MT
155 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
156 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
157 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
158 "LINE1:dropped_rx#228B22",
159
160 # Draw the transmit drops.
161 "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"),
bba5cf66
MT
162 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
163 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
164 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
165 "LINE1:dropped_tx#B22222",
166
167 # Draw the collisions as a line.
168 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
169 ]
e9bb328f 170
bba5cf66 171 @property
f181246a 172 def graph_title(self):
c0ebfd25 173 _ = self.locale.translate
2a3d5c1b 174 return _("Errors/dropped packets on %s") % self.interface
f181246a
MT
175
176 @property
177 def graph_vertical_label(self):
c0ebfd25 178 _ = self.locale.translate
f181246a 179 return _("Packets/s")
e9bb328f
MT
180
181
72364063 182class InterfaceObject(base.Object):
89b70961
MT
183 rrd_schema = [
184 "DS:bytes_rx:DERIVE:0:U",
185 "DS:bytes_tx:DERIVE:0:U",
186 "DS:collisions:DERIVE:0:U",
187 "DS:dropped_rx:DERIVE:0:U",
188 "DS:dropped_tx:DERIVE:0:U",
189 "DS:errors_rx:DERIVE:0:U",
190 "DS:errors_tx:DERIVE:0:U",
191 "DS:multicast:DERIVE:0:U",
192 "DS:packets_rx:DERIVE:0:U",
193 "DS:packets_tx:DERIVE:0:U",
194 ]
195
72364063
MT
196 def __repr__(self):
197 return "<%s %s>" % (self.__class__.__name__, self.interface)
bba5cf66 198
72364063
MT
199 def init(self, interface):
200 self.interface = interface
89b70961
MT
201
202 @property
203 def id(self):
72364063 204 return self.interface
89b70961 205
72364063 206 def collect(self):
29455c5f 207 interface_path = os.path.join("/sys/class/net", self.interface)
bba5cf66
MT
208
209 # Check if the interface exists.
210 if not os.path.exists(interface_path):
211 self.log.debug(_("Interface %s does not exists. Cannot collect.") \
212 % self.interface)
213 return
214
89b70961
MT
215 files = (
216 "rx_bytes", "tx_bytes",
217 "collisions",
218 "rx_dropped", "tx_dropped",
219 "rx_errors", "tx_errors",
220 "multicast",
221 "rx_packets", "tx_packets",
222 )
bba5cf66 223 ret = []
89b70961
MT
224
225 for file in files:
bba5cf66 226 path = os.path.join(interface_path, "statistics", file)
89b70961
MT
227
228 # Open file and read it's content.
bba5cf66
MT
229 f = None
230 try:
231 f = open(path)
232
233 line = f.readline()
234 line = line.strip()
235 ret.append(line)
236 except:
237 ret.append("0")
238 raise
239
240 finally:
241 if f:
242 f.close()
243
f648421a 244 return ret
72364063
MT
245
246
247class InterfacePlugin(base.Plugin):
248 name = "interface"
249 description = "Interface Statistics Plugin"
250
251 templates = [
252 GraphTemplateInterfaceBits,
253 GraphTemplateInterfacePackets,
254 GraphTemplateInterfaceErrors,
255 ]
256
b064255e 257 interval = 30
72364063 258
72364063
MT
259 @property
260 def objects(self):
29455c5f 261 for interface in util.get_network_interfaces():
72364063 262 yield InterfaceObject(self, interface=interface)