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 ###############################################################################
22 from __future__
import division
30 SYS_CLASS_NET
= "/sys/class/net"
32 class GraphTemplateInterfaceBytes(base
.GraphTemplate
):
33 name
= "interface-bytes"
36 "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
37 "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
39 # Convert everything into bits.
40 "CDEF:bits_rx=bytes_rx,8,*",
41 "CDEF:bits_tx=bytes_tx,8,*",
43 # Invert the transmitted bytes to create upside down graph.
44 "CDEF:bits_tx_inv=bits_tx,-1,*",
46 # Compute the total bandwidth.
47 "CDEF:bits=bits_rx,bits_tx,+",
50 "VDEF:bits_95p=bits,95,PERCENT",
52 # Draw the received area.
53 "AREA:bits_rx#228B2277:%-15s" % _("Received"),
54 "VDEF:bits_rx_min=bits_rx,MINIMUM",
55 "VDEF:bits_rx_max=bits_rx,MAXIMUM",
56 "VDEF:bits_rx_avg=bits_rx,AVERAGE",
57 "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
58 "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
59 "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
60 "LINE1:bits_rx#228B22",
62 # Draw the transmitted area.
63 "AREA:bits_tx_inv#B2222277:%-15s" % _("Transmitted"),
64 "VDEF:bits_tx_min=bits_tx,MINIMUM",
65 "VDEF:bits_tx_max=bits_tx,MAXIMUM",
66 "VDEF:bits_tx_avg=bits_tx,AVERAGE",
67 "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"),
68 "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"),
69 "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n",
70 "LINE1:bits_tx_inv#B22222",
73 "LINE1:bits_95p#000000:%-15s" % _("95th percentile"),
74 "GPRINT:bits_95p:%s" % _("%8.2lf %sbps") + "\\n",
78 "--title", _("Bandwidth usage on %(interface)s"),
79 "--vertical-label", _("Bit/s"),
83 class GraphTemplateInterfacePackets(base
.GraphTemplate
):
84 name
= "interface-packets"
87 "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
88 "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
90 # Invert the transmitted packets to create upside down graph.
91 "CDEF:packets_tx_inv=packets_tx,-1,*",
93 # Draw the received area.
94 "AREA:packets_rx#228B2277:%-15s" % _("Received"),
95 "VDEF:packets_rx_min=packets_rx,MINIMUM",
96 "VDEF:packets_rx_max=packets_rx,MAXIMUM",
97 "VDEF:packets_rx_avg=packets_rx,AVERAGE",
98 "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
99 "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
100 "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
101 "LINE1:packets_rx#228B22",
103 # Draw the transmitted area.
104 "AREA:packets_tx_inv#B2222277:%-15s" % _("Transmitted"),
105 "VDEF:packets_tx_min=packets_tx,MINIMUM",
106 "VDEF:packets_tx_max=packets_tx,MAXIMUM",
107 "VDEF:packets_tx_avg=packets_tx,AVERAGE",
108 "GPRINT:packets_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
109 "GPRINT:packets_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
110 "GPRINT:packets_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
111 "LINE1:packets_tx_inv#B22222",
115 "--title", _("Transferred packets on %(interface)s"),
116 "--vertical-label", _("Packets/s"),
120 class GraphTemplateInterfaceErrors(base
.GraphTemplate
):
121 name
= "interface-errors"
124 "DEF:errors_rx=%(file)s:errors_rx:AVERAGE",
125 "DEF:errors_tx=%(file)s:errors_tx:AVERAGE",
126 "DEF:dropped_rx=%(file)s:dropped_rx:AVERAGE",
127 "DEF:dropped_tx=%(file)s:dropped_tx:AVERAGE",
128 "DEF:collisions=%(file)s:collisions:AVERAGE",
130 # Invert the transmitted packets to create upside down graph.
131 "CDEF:errors_tx_inv=errors_tx,-1,*",
132 "CDEF:dropped_tx_inv=dropped_tx,-1,*",
134 # Draw the receive errors.
135 "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"),
136 "VDEF:errors_rx_min=errors_rx,MINIMUM",
137 "VDEF:errors_rx_max=errors_rx,MAXIMUM",
138 "VDEF:errors_rx_avg=errors_rx,AVERAGE",
139 "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
140 "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
141 "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
142 "LINE1:errors_rx#228B22",
144 # Draw the transmit errors.
145 "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"),
146 "VDEF:errors_tx_min=errors_tx,MINIMUM",
147 "VDEF:errors_tx_max=errors_tx,MAXIMUM",
148 "VDEF:errors_tx_avg=errors_tx,AVERAGE",
149 "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
150 "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
151 "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
152 "LINE1:errors_tx_inv#B22222",
154 # Draw the receive drops.
155 "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"),
156 "VDEF:dropped_rx_min=dropped_rx,MINIMUM",
157 "VDEF:dropped_rx_max=dropped_rx,MAXIMUM",
158 "VDEF:dropped_rx_avg=dropped_rx,AVERAGE",
159 "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
160 "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
161 "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
162 "LINE1:dropped_rx#228B22",
164 # Draw the transmit drops.
165 "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"),
166 "VDEF:dropped_tx_min=dropped_tx,MINIMUM",
167 "VDEF:dropped_tx_max=dropped_tx,MAXIMUM",
168 "VDEF:dropped_tx_avg=dropped_tx,AVERAGE",
169 "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"),
170 "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"),
171 "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n",
172 "LINE1:dropped_tx#B22222",
174 # Draw the collisions as a line.
175 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
179 "--title", _("Errors/dropped packets on %(interface)s"),
180 "--vertical-label", _("Packets/s"),
184 class DataSourceInterface(base
.DataSource
):
186 description
= "Interface Statistics Data Source"
189 GraphTemplateInterfaceBytes
,
190 GraphTemplateInterfacePackets
,
191 GraphTemplateInterfaceErrors
,
195 "DS:bytes_rx:DERIVE:0:U",
196 "DS:bytes_tx:DERIVE:0:U",
197 "DS:collisions:DERIVE:0:U",
198 "DS:dropped_rx:DERIVE:0:U",
199 "DS:dropped_tx:DERIVE:0:U",
200 "DS:errors_rx:DERIVE:0:U",
201 "DS:errors_tx:DERIVE:0:U",
202 "DS:multicast:DERIVE:0:U",
203 "DS:packets_rx:DERIVE:0:U",
204 "DS:packets_tx:DERIVE:0:U",
208 def autocreate(cls
, collecty
, **kwargs
):
209 if not os
.path
.exists(SYS_CLASS_NET
):
213 for interface
in os
.listdir(SYS_CLASS_NET
):
214 path
= os
.path
.join(SYS_CLASS_NET
, interface
)
215 if not os
.path
.isdir(path
):
218 instance
= cls(collecty
, interface
=interface
)
219 instances
.append(instance
)
223 def init(self
, **kwargs
):
224 self
.interface
= kwargs
.get("interface")
228 return "-".join((self
.name
, self
.interface
))
232 "rx_bytes", "tx_bytes",
234 "rx_dropped", "tx_dropped",
235 "rx_errors", "tx_errors",
237 "rx_packets", "tx_packets",
239 ret
= ["%s" % self
.now
,]
242 path
= os
.path
.join(SYS_CLASS_NET
, self
.interface
, "statistics", file)
244 # Open file and read it's content.
253 self
.data
.append(":".join(ret
))