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 COLOUR_RX_AREA
= "%sAA" % COLOUR_RX
32 COLOUR_TX_AREA
= "%sAA" % COLOUR_TX
34 class GraphTemplateInterfaceBase(base
.GraphTemplate
):
37 return self
.object.interface
40 class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase
):
41 name
= "interface-bits"
45 _
= self
.locale
.translate
48 # Convert everything into bits.
49 "CDEF:bits_rx=bytes_rx,8,*",
50 "CDEF:bits_tx=bytes_tx,8,*",
53 "VDEF:bits_rx_95p=bits_rx,95,PERCENT",
54 "VDEF:bits_tx_95p=bits_tx,95,PERCENT",
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",
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",
69 "LINE1:bits_rx#%s" % COLOUR_RX
,
70 "LINE1:bits_tx#%s" % COLOUR_TX
,
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",
81 def graph_title(self
):
82 _
= self
.locale
.translate
83 return _("Bandwidth usage on %s") % self
.interface
86 def graph_vertical_label(self
):
87 _
= self
.locale
.translate
91 class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase
):
92 name
= "interface-packets"
96 _
= self
.locale
.translate
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",
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",
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#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",
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",
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",
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",
167 # Draw the collisions as a line.
168 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
172 def graph_title(self
):
173 _
= self
.locale
.translate
174 return _("Errors/dropped packets on %s") % self
.interface
177 def graph_vertical_label(self
):
178 _
= self
.locale
.translate
179 return _("Packets/s")
182 class InterfaceObject(base
.Object
):
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",
197 return "<%s %s>" % (self
.__class
__.__name
__, self
.interface
)
199 def init(self
, interface
):
200 self
.interface
= interface
204 return self
.interface
207 interface_path
= os
.path
.join("/sys/class/net", self
.interface
)
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.") \
216 "rx_bytes", "tx_bytes",
218 "rx_dropped", "tx_dropped",
219 "rx_errors", "tx_errors",
221 "rx_packets", "tx_packets",
226 path
= os
.path
.join(interface_path
, "statistics", file)
228 # Open file and read it's content.
247 class InterfacePlugin(base
.Plugin
):
249 description
= "Interface Statistics Plugin"
252 GraphTemplateInterfaceBits
,
253 GraphTemplateInterfacePackets
,
254 GraphTemplateInterfaceErrors
,
261 for interface
in util
.get_network_interfaces():
262 yield InterfaceObject(self
, interface
=interface
)