]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/interface.py
Add some magic to collecty that makes the graph templates smaller
[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 # 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")),
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")),
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
81 def graph_title(self):
82 _ = self.locale.translate
83 return _("Bandwidth usage on %s") % self.interface
84
85 @property
86 def graph_vertical_label(self):
87 _ = self.locale.translate
88 return _("Bit/s")
89
90
91 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
92 name = "interface-packets"
93
94 @property
95 def rrd_graph(self):
96 _ = self.locale.translate
97
98 return [
99 # Draw the received area.
100 "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")),
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")),
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 ]
115
116 @property
117 def graph_title(self):
118 _ = self.locale.translate
119 return _("Transferred packets on %s") % self.interface
120
121 @property
122 def graph_vertical_label(self):
123 _ = self.locale.translate
124 return _("Packets/s")
125
126
127 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
128 name = "interface-errors"
129
130 @property
131 def rrd_graph(self):
132 _ = self.locale.translate
133
134 return [
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"),
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"),
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"),
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"),
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 ]
170
171 @property
172 def graph_title(self):
173 _ = self.locale.translate
174 return _("Errors/dropped packets on %s") % self.interface
175
176 @property
177 def graph_vertical_label(self):
178 _ = self.locale.translate
179 return _("Packets/s")
180
181
182 class InterfaceObject(base.Object):
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
196 def __repr__(self):
197 return "<%s %s>" % (self.__class__.__name__, self.interface)
198
199 def init(self, interface):
200 self.interface = interface
201
202 @property
203 def id(self):
204 return self.interface
205
206 def collect(self):
207 interface_path = os.path.join("/sys/class/net", self.interface)
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
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 )
223 ret = []
224
225 for file in files:
226 path = os.path.join(interface_path, "statistics", file)
227
228 # Open file and read it's content.
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
244 return ret
245
246
247 class InterfacePlugin(base.Plugin):
248 name = "interface"
249 description = "Interface Statistics Plugin"
250
251 templates = [
252 GraphTemplateInterfaceBits,
253 GraphTemplateInterfacePackets,
254 GraphTemplateInterfaceErrors,
255 ]
256
257 interval = 30
258
259 @property
260 def objects(self):
261 for interface in util.get_network_interfaces():
262 yield InterfaceObject(self, interface=interface)