2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
30 SYS_CLASS_NET
= "/sys/class/net"
33 COLOUR_RX_AREA
= "%sAA" % COLOUR_RX
35 COLOUR_TX_AREA
= "%sAA" % COLOUR_TX
37 class GraphTemplateInterfaceBase(base
.GraphTemplate
):
40 return self
.object.interface
43 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase
):
44 name
= "interface-bits"
48 _
= self
.locale
.translate
51 "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
52 "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
54 # Convert everything into bits.
55 "CDEF:bits_rx=bytes_rx,8,*",
56 "CDEF:bits_tx=bytes_tx,8,*",
59 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
60 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
62 # Draw the received area.
63 "AREA:bits_rx#%s:%-15s" % (COLOUR_RX_AREA
, _("Received")),
64 "VDEF:bits_rx_min=bits_rx,MINIMUM",
65 "VDEF:bits_rx_max=bits_rx,MAXIMUM",
66 "VDEF:bits_rx_avg=bits_rx,AVERAGE",
67 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
68 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
69 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
71 # Draw the transmitted area.
72 "AREA:bits_tx#%s:%-15s" % (COLOUR_TX_AREA
, _("Transmitted")),
73 "VDEF:bits_tx_min=bits_tx,MINIMUM",
74 "VDEF:bits_tx_max=bits_tx,MAXIMUM",
75 "VDEF:bits_tx_avg=bits_tx,AVERAGE",
76 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
77 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
78 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
81 "LINE1:bits_rx#%s" % COLOUR_RX
,
82 "LINE1:bits_tx#%s" % COLOUR_TX
,
85 "COMMENT:--- %s ---\\n" % _("95th percentile"),
86 "LINE2:bits_rx_95p#%s:%-15s" % (COLOUR_RX
, _("Received")),
87 "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n",
88 "LINE2:bits_tx_95p#%s:%-15s" % (COLOUR_TX
, _("Transmitted")),
89 "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n",
93 def graph_title(self
):
94 _
= self
.locale
.translate
95 return _("Bandwidth usage on %s") % self
.interface
98 def graph_vertical_label(self
):
99 _
= self
.locale
.translate
103 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase
):
104 name
= "interface-packets"
108 _
= self
.locale
.translate
111 "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
112 "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
114 # Draw the received area.
115 "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA
, _("Received")),
116 "VDEF:packets_rx_min=packets_rx,MINIMUM",
117 "VDEF:packets_rx_max=packets_rx,MAXIMUM",
118 "VDEF:packets_rx_avg=packets_rx,AVERAGE",
119 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
120 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
121 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
123 # Draw the transmitted area.
124 "AREA:packets_tx#%s:%-15s" % (COLOUR_TX_AREA
, _("Transmitted")),
125 "VDEF:packets_tx_min=packets_tx,MINIMUM",
126 "VDEF:packets_tx_max=packets_tx,MAXIMUM",
127 "VDEF:packets_tx_avg=packets_tx,AVERAGE",
128 "GPRINT:packets_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
129 "GPRINT:packets_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
130 "GPRINT:packets_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
132 # Draw outlines of the areas on top.
133 "LINE1:packets_rx#%s" % COLOUR_RX
,
134 "LINE1:packets_tx#%s" % COLOUR_TX
,
138 def graph_title(self
):
139 _
= self
.locale
.translate
140 return _("Transferred packets on %s") % self
.interface
143 def graph_vertical_label(self
):
144 _
= self
.locale
.translate
145 return _("Packets/s")
148 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase
):
149 name
= "interface-errors"
153 _
= self
.locale
.translate
156 "DEF:errors_rx=%(file)s:errors_rx:AVERAGE",
157 "DEF:errors_tx=%(file)s:errors_tx:AVERAGE",
158 "DEF:dropped_rx=%(file)s:dropped_rx:AVERAGE",
159 "DEF:dropped_tx=%(file)s:dropped_tx:AVERAGE",
160 "DEF:collisions=%(file)s:collisions:AVERAGE",
162 # Invert the transmitted packets to create upside down graph.
163 "CDEF:errors_tx_inv=errors_tx,-1,*",
164 "CDEF:dropped_tx_inv=dropped_tx,-1,*",
166 # Draw the receive errors.
167 "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"),
168 "VDEF:errors_rx_min=errors_rx,MINIMUM",
169 "VDEF:errors_rx_max=errors_rx,MAXIMUM",
170 "VDEF:errors_rx_avg=errors_rx,AVERAGE",
171 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
172 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
173 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
174 "LINE1:errors_rx#228B22",
176 # Draw the transmit errors.
177 "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"),
178 "VDEF:errors_tx_min=errors_tx,MINIMUM",
179 "VDEF:errors_tx_max=errors_tx,MAXIMUM",
180 "VDEF:errors_tx_avg=errors_tx,AVERAGE",
181 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
182 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
183 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
184 "LINE1:errors_tx_inv#B22222",
186 # Draw the receive drops.
187 "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"),
188 "VDEF:dropped_rx_min=dropped_rx,MINIMUM",
189 "VDEF:dropped_rx_max=dropped_rx,MAXIMUM",
190 "VDEF:dropped_rx_avg=dropped_rx,AVERAGE",
191 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
192 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
193 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
194 "LINE1:dropped_rx#228B22",
196 # Draw the transmit drops.
197 "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"),
198 "VDEF:dropped_tx_min=dropped_tx,MINIMUM",
199 "VDEF:dropped_tx_max=dropped_tx,MAXIMUM",
200 "VDEF:dropped_tx_avg=dropped_tx,AVERAGE",
201 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
202 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
203 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
204 "LINE1:dropped_tx#B22222",
206 # Draw the collisions as a line.
207 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
211 def graph_title(self
):
212 _
= self
.locale
.translate
213 return _("Errors/dropped packets on %s") % self
.interface
216 def graph_vertical_label(self
):
217 _
= self
.locale
.translate
218 return _("Packets/s")
221 class InterfaceObject(base
.Object
):
223 "DS:bytes_rx:DERIVE:0:U",
224 "DS:bytes_tx:DERIVE:0:U",
225 "DS:collisions:DERIVE:0:U",
226 "DS:dropped_rx:DERIVE:0:U",
227 "DS:dropped_tx:DERIVE:0:U",
228 "DS:errors_rx:DERIVE:0:U",
229 "DS:errors_tx:DERIVE:0:U",
230 "DS:multicast:DERIVE:0:U",
231 "DS:packets_rx:DERIVE:0:U",
232 "DS:packets_tx:DERIVE:0:U",
236 return "<%s %s>" % (self
.__class
__.__name
__, self
.interface
)
238 def init(self
, interface
):
239 self
.interface
= interface
243 return self
.interface
246 interface_path
= os
.path
.join(SYS_CLASS_NET
, self
.interface
)
248 # Check if the interface exists.
249 if not os
.path
.exists(interface_path
):
250 self
.log
.debug(_("Interface %s does not exists. Cannot collect.") \
255 "rx_bytes", "tx_bytes",
257 "rx_dropped", "tx_dropped",
258 "rx_errors", "tx_errors",
260 "rx_packets", "tx_packets",
265 path
= os
.path
.join(interface_path
, "statistics", file)
267 # Open file and read it's content.
286 class InterfacePlugin(base
.Plugin
):
288 description
= "Interface Statistics Plugin"
291 GraphTemplateInterfaceBits
,
292 GraphTemplateInterfacePackets
,
293 GraphTemplateInterfaceErrors
,
298 def get_interfaces(self
):
299 for interface
in os
.listdir(SYS_CLASS_NET
):
300 # Skip some unwanted interfaces.
301 if interface
== "lo" or interface
.startswith("mon."):
304 path
= os
.path
.join(SYS_CLASS_NET
, interface
)
305 if not os
.path
.isdir(path
):
312 for interface
in self
.get_interfaces():
313 yield InterfaceObject(self
, interface
=interface
)