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 ###############################################################################
26 from ..colours
import *
30 class GraphTemplateInterfaceBase(base
.GraphTemplate
):
33 return self
.object.interface
36 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase
):
37 name
= "interface-bits"
41 _
= self
.locale
.translate
44 # Convert everything into bits.
45 "CDEF:bits_rx=bytes_rx,8,*",
46 "CDEF:bits_tx=bytes_tx,8,*",
49 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
50 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
52 # Draw the received area.
53 "AREA:bits_rx%s:%-15s" % (util
.lighten(COLOUR_RX
, AREA_OPACITY
), _("Received")),
54 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
55 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
56 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
58 # Draw the transmitted area.
59 "AREA:bits_tx%s:%-15s" % (util
.lighten(COLOUR_TX
, AREA_OPACITY
), _("Transmitted")),
60 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
61 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
62 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
65 "LINE1:bits_rx%s" % COLOUR_RX
,
66 "LINE1:bits_tx%s" % COLOUR_TX
,
69 "COMMENT:--- %s ---\\n" % _("95th percentile"),
70 "LINE2:bits_rx_95p%s:%-15s" % (COLOUR_RX
, _("Received")),
71 "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n",
72 "LINE2:bits_tx_95p%s:%-15s" % (COLOUR_TX
, _("Transmitted")),
73 "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n",
77 def graph_title(self
):
78 _
= self
.locale
.translate
79 return _("Bandwidth usage on %s") % self
.interface
82 def graph_vertical_label(self
):
83 _
= self
.locale
.translate
87 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase
):
88 name
= "interface-packets"
92 _
= self
.locale
.translate
95 # Draw the received area.
96 "AREA:packets_rx%s:%-15s" % (
97 util
.lighten(COLOUR_RX
, AREA_OPACITY
), _("Received"),
99 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
100 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
101 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
103 # Draw the transmitted area.
104 "AREA:packets_tx%s:%-15s" % (
105 util
.lighten(COLOUR_TX
, AREA_OPACITY
), _("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",
111 # Draw outlines of the areas on top.
112 "LINE1:packets_rx%s" % COLOUR_RX
,
113 "LINE1:packets_tx%s" % COLOUR_TX
,
117 def graph_title(self
):
118 _
= self
.locale
.translate
119 return _("Transferred packets on %s") % self
.interface
122 def graph_vertical_label(self
):
123 _
= self
.locale
.translate
124 return _("Packets/s")
127 class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase
):
128 name
= "interface-errors"
132 _
= self
.locale
.translate
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,*",
139 # Draw the receive errors.
140 "AREA:errors_rx%s:%-15s" % (
141 util
.lighten(COLOUR_RX
, AREA_OPACITY
), _("Receive errors"),
143 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
144 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
145 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
146 "LINE1:errors_rx%s" % COLOUR_RX
,
148 # Draw the transmit errors.
149 "AREA:errors_tx_inv%s:%-15s" % (
150 util
.lighten(COLOUR_TX
, AREA_OPACITY
), _("Transmit errors"),
152 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
153 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
154 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
155 "LINE1:errors_tx_inv%s" % COLOUR_TX
,
157 # Draw the receive drops.
158 "LINE2:dropped_rx%s:%-15s" % (
159 util
.lighten(AMBER
, AREA_OPACITY
), _("Receive drops"),
161 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
162 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
163 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
164 "LINE1:dropped_rx#228B22",
166 # Draw the transmit drops.
167 "LINE2:dropped_tx%s:%-15s" % (
168 util
.lighten(TEAL
, AREA_OPACITY
), _("Transmit drops"),
170 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
171 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
172 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
173 "LINE1:dropped_tx%s" % TEAL
,
175 # Draw the collisions as a line.
176 "LINE2:collisions%s:%-15s\l" % (COLOUR_CRITICAL
, _("Collisions")),
180 def graph_title(self
):
181 _
= self
.locale
.translate
182 return _("Errors/dropped packets on %s") % self
.interface
185 def graph_vertical_label(self
):
186 _
= self
.locale
.translate
187 return _("Packets/s")
190 class InterfaceObject(base
.Object
):
192 "DS:bytes_rx:DERIVE:0:U",
193 "DS:bytes_tx:DERIVE:0:U",
194 "DS:collisions:DERIVE:0:U",
195 "DS:dropped_rx:DERIVE:0:U",
196 "DS:dropped_tx:DERIVE:0:U",
197 "DS:errors_rx:DERIVE:0:U",
198 "DS:errors_tx:DERIVE:0:U",
199 "DS:multicast:DERIVE:0:U",
200 "DS:packets_rx:DERIVE:0:U",
201 "DS:packets_tx:DERIVE:0:U",
205 return "<%s %s>" % (self
.__class
__.__name
__, self
.interface
)
207 def init(self
, interface
):
208 self
.interface
= interface
212 return self
.interface
215 interface_path
= os
.path
.join("/sys/class/net", self
.interface
)
217 # Check if the interface exists.
218 if not os
.path
.exists(interface_path
):
219 self
.log
.debug(_("Interface %s does not exists. Cannot collect.") \
224 "rx_bytes", "tx_bytes",
226 "rx_dropped", "tx_dropped",
227 "rx_errors", "tx_errors",
229 "rx_packets", "tx_packets",
234 path
= os
.path
.join(interface_path
, "statistics", file)
236 # Open file and read it's content.
255 class InterfacePlugin(base
.Plugin
):
257 description
= "Interface Statistics Plugin"
260 GraphTemplateInterfaceBits
,
261 GraphTemplateInterfacePackets
,
262 GraphTemplateInterfaceErrors
,
269 for interface
in util
.get_network_interfaces():
270 yield InterfaceObject(self
, interface
=interface
)