]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/interface.py
psi: Add graph template
[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 util
25 from . import base
26
27 from ..colours import *
28 from ..constants import *
29 from ..i18n import _
30
31 class GraphTemplateInterfaceBase(base.GraphTemplate):
32 @property
33 def interface(self):
34 return self.object.interface
35
36
37 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase):
38 name = "interface-bits"
39
40 @property
41 def rrd_graph(self):
42 return [
43 # Headline
44 "COMMENT:%s" % EMPTY_LABEL,
45 "COMMENT:%s" % (COLUMN % _("Current")),
46 "COMMENT:%s" % (COLUMN % _("Average")),
47 "COMMENT:%s" % (COLUMN % _("Minimum")),
48 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
49
50 # Convert everything into bits.
51 "CDEF:bits_rx=bytes_rx,8,*",
52 "CDEF:bits_tx=bytes_tx,8,*",
53
54 # Compute 95% lines.
55 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
56 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
57
58 # Draw the received area.
59 "AREA:bits_rx%s:%s" % (
60 transparency(COLOUR_RX, AREA_OPACITY),
61 LABEL % _("Received"),
62 ),
63 "GPRINT:bits_rx_cur:%s" % BPS,
64 "GPRINT:bits_rx_avg:%s" % BPS,
65 "GPRINT:bits_rx_min:%s" % BPS,
66 "GPRINT:bits_rx_max:%s\\j" % BPS,
67
68 # Draw the transmitted area.
69 "AREA:bits_tx%s:%-15s" % (
70 transparency(COLOUR_TX, AREA_OPACITY),
71 LABEL % _("Transmitted"),
72 ),
73 "GPRINT:bits_tx_cur:%s" % BPS,
74 "GPRINT:bits_tx_avg:%s" % BPS,
75 "GPRINT:bits_tx_min:%s" % BPS,
76 "GPRINT:bits_tx_max:%s\\j" % BPS,
77
78 # Draw outlines.
79 "LINE1:bits_rx%s" % COLOUR_RX,
80 "LINE1:bits_tx%s" % COLOUR_TX,
81
82 EMPTY_LINE,
83
84 # Draw the 95% lines.
85 "COMMENT:%s" % _("95th Percentile"),
86 "LINE:bits_rx_95p%s:%s:dashes" % (COLOUR_RX, LABEL % _("Received")),
87 "GPRINT:bits_rx_95p:%s\\r" % BPS,
88 "LINE:bits_tx_95p%s:%s:dashes" % (COLOUR_TX, LABEL % _("Transmitted")),
89 "GPRINT:bits_tx_95p:%s\\r" % BPS,
90 ]
91
92 @property
93 def graph_title(self):
94 return _("Bandwidth Usage on %s") % self.interface
95
96 @property
97 def graph_vertical_label(self):
98 return _("Bit/s")
99
100
101 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase):
102 name = "interface-packets"
103
104 @property
105 def rrd_graph(self):
106 return [
107 # Headline
108 "COMMENT:%s" % EMPTY_LABEL,
109 "COMMENT:%s" % (COLUMN % _("Current")),
110 "COMMENT:%s" % (COLUMN % _("Average")),
111 "COMMENT:%s" % (COLUMN % _("Minimum")),
112 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
113
114 # Draw the received area.
115 "AREA:packets_rx%s:%s" % (
116 transparency(COLOUR_RX, AREA_OPACITY),
117 LABEL % _("Received"),
118 ),
119 "GPRINT:packets_rx_cur:%s" % PPS,
120 "GPRINT:packets_rx_avg:%s" % PPS,
121 "GPRINT:packets_rx_min:%s" % PPS,
122 "GPRINT:packets_rx_max:%s\\j" % PPS,
123
124 # Draw the transmitted area.
125 "AREA:packets_tx%s:%s" % (
126 transparency(COLOUR_TX, AREA_OPACITY),
127 LABEL % _("Transmitted"),
128 ),
129 "GPRINT:packets_tx_cur:%s" % PPS,
130 "GPRINT:packets_tx_avg:%s" % PPS,
131 "GPRINT:packets_tx_min:%s" % PPS,
132 "GPRINT:packets_tx_max:%s\\j" % PPS,
133
134 # Draw outlines of the areas on top.
135 "LINE1:packets_rx%s" % COLOUR_RX,
136 "LINE1:packets_tx%s" % COLOUR_TX,
137 ]
138
139 @property
140 def graph_title(self):
141 return _("Transferred Packets on %s") % self.interface
142
143 @property
144 def graph_vertical_label(self):
145 return _("Packets/s")
146
147
148 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase):
149 name = "interface-errors"
150
151 @property
152 def rrd_graph(self):
153 return [
154 # Headline
155 "COMMENT:%s" % EMPTY_LABEL,
156 "COMMENT:%s" % (COLUMN % _("Current")),
157 "COMMENT:%s" % (COLUMN % _("Average")),
158 "COMMENT:%s" % (COLUMN % _("Minimum")),
159 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
160
161 # Invert the transmitted packets to create upside down graph.
162 "CDEF:errors_tx_inv=errors_tx,-1,*",
163 "CDEF:dropped_tx_inv=dropped_tx,-1,*",
164
165 # Draw the receive errors.
166 "AREA:errors_rx%s:%-15s" % (
167 transparency(COLOUR_RX, AREA_OPACITY),
168 LABEL % _("Receive Errors"),
169 ),
170 "GPRINT:errors_rx_cur:%s" % PPS,
171 "GPRINT:errors_rx_avg:%s" % PPS,
172 "GPRINT:errors_rx_min:%s" % PPS,
173 "GPRINT:errors_rx_max:%s\\j" % PPS,
174 "LINE1:errors_rx%s" % COLOUR_RX,
175
176 # Draw the transmit errors.
177 "AREA:errors_tx_inv%s:%-15s" % (
178 transparency(COLOUR_TX, AREA_OPACITY),
179 LABEL % _("Transmit Errors"),
180 ),
181 "GPRINT:errors_tx_cur:%s" % PPS,
182 "GPRINT:errors_tx_avg:%s" % PPS,
183 "GPRINT:errors_tx_min:%s" % PPS,
184 "GPRINT:errors_tx_max:%s\\j" % PPS,
185 "LINE1:errors_tx_inv%s" % COLOUR_TX,
186
187 # Draw the receive drops.
188 "LINE2:dropped_rx%s:%-15s" % (
189 transparency(AMBER, AREA_OPACITY),
190 LABEL % _("Receive Drops"),
191 ),
192 "GPRINT:dropped_rx_cur:%s" % PPS,
193 "GPRINT:dropped_rx_avg:%s" % PPS,
194 "GPRINT:dropped_rx_min:%s" % PPS,
195 "GPRINT:dropped_rx_max:%s\\j" % PPS,
196 "LINE1:dropped_rx#228B22",
197
198 # Draw the transmit drops.
199 "LINE2:dropped_tx%s:%-15s" % (
200 transparency(TEAL, AREA_OPACITY),
201 LABEL % _("Transmit Drops"),
202 ),
203 "GPRINT:dropped_tx_cur:%s" % PPS,
204 "GPRINT:dropped_tx_avg:%s" % PPS,
205 "GPRINT:dropped_tx_min:%s" % PPS,
206 "GPRINT:dropped_tx_max:%s\\j" % PPS,
207 "LINE1:dropped_tx%s" % TEAL,
208
209 EMPTY_LINE,
210
211 # Draw the collisions as a line.
212 "LINE2:collisions%s:%s" % (
213 COLOUR_CRITICAL,
214 LABEL % _("Collisions"),
215 ),
216 "GPRINT:collisions_cur:%s" % PPS,
217 "GPRINT:collisions_avg:%s" % PPS,
218 "GPRINT:collisions_min:%s" % PPS,
219 "GPRINT:collisions_max:%s\\j" % PPS,
220 ]
221
222 @property
223 def graph_title(self):
224 return _("Errors/Dropped Packets on %s") % self.interface
225
226 @property
227 def graph_vertical_label(self):
228 return _("Packets/s")
229
230
231 class InterfaceObject(base.Object):
232 rrd_schema = [
233 "DS:bytes_rx:DERIVE:0:U",
234 "DS:bytes_tx:DERIVE:0:U",
235 "DS:collisions:DERIVE:0:U",
236 "DS:dropped_rx:DERIVE:0:U",
237 "DS:dropped_tx:DERIVE:0:U",
238 "DS:errors_rx:DERIVE:0:U",
239 "DS:errors_tx:DERIVE:0:U",
240 "DS:multicast:DERIVE:0:U",
241 "DS:packets_rx:DERIVE:0:U",
242 "DS:packets_tx:DERIVE:0:U",
243 ]
244
245 def __repr__(self):
246 return "<%s %s>" % (self.__class__.__name__, self.interface)
247
248 def init(self, interface):
249 self.interface = interface
250
251 @property
252 def id(self):
253 return self.interface
254
255 def collect(self):
256 interface_path = os.path.join("/sys/class/net", self.interface)
257
258 # Check if the interface exists.
259 if not os.path.exists(interface_path):
260 self.log.debug(_("Interface %s does not exists. Cannot collect.") \
261 % self.interface)
262 return
263
264 files = (
265 "rx_bytes", "tx_bytes",
266 "collisions",
267 "rx_dropped", "tx_dropped",
268 "rx_errors", "tx_errors",
269 "multicast",
270 "rx_packets", "tx_packets",
271 )
272 ret = []
273
274 for file in files:
275 path = os.path.join(interface_path, "statistics", file)
276
277 ret.append(
278 self.read_file_integer(path),
279 )
280
281 return ret
282
283
284 class InterfacePlugin(base.Plugin):
285 name = "interface"
286 description = "Interface Statistics Plugin"
287
288 templates = [
289 GraphTemplateInterfaceBits,
290 GraphTemplateInterfacePackets,
291 GraphTemplateInterfaceErrors,
292 ]
293
294 interval = 30
295
296 @property
297 def objects(self):
298 for interface in util.get_network_interfaces():
299 yield InterfaceObject(self, interface=interface)