]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/interface.py
Simplify RRD descriptions.
[collecty.git] / collecty / plugins / interface.py
CommitLineData
89b70961
MT
1#!/usr/bin/python
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
22from __future__ import division
23
24import os
25
26import base
27
28from ..i18n import _
29
30SYS_CLASS_NET = "/sys/class/net"
31
e9bb328f
MT
32class GraphTemplateInterfaceBytes(base.GraphTemplate):
33 name = "interface-bytes"
34
35 rrd_graph = [
36 "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE",
37 "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE",
38
39 # Convert everything into bits.
40 "CDEF:bits_rx=bytes_rx,8,*",
41 "CDEF:bits_tx=bytes_tx,8,*",
42
43 # Invert the transmitted bytes to create upside down graph.
44 "CDEF:bits_tx_inv=bits_tx,-1,*",
45
46 # Compute the total bandwidth.
47 "CDEF:bits=bits_rx,bits_tx,+",
48
49 # Compute 95% line.
50 "VDEF:bits_95p=bits,95,PERCENT",
51
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",
61
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",
71
72 # Draw the 95% line.
73 "LINE1:bits_95p#000000:%-15s" % _("95th percentile"),
74 "GPRINT:bits_95p:%s" % _("%8.2lf %sbps") + "\\n",
75 ]
76
77 rrd_graph_args = [
78 "--title", _("Bandwidth usage on %(interface)s"),
79 "--vertical-label", _("Bit/s"),
80 ]
81
82
83class GraphTemplateInterfacePackets(base.GraphTemplate):
84 name = "interface-packets"
85
86 rrd_graph = [
87 "DEF:packets_rx=%(file)s:packets_rx:AVERAGE",
88 "DEF:packets_tx=%(file)s:packets_tx:AVERAGE",
89
90 # Invert the transmitted packets to create upside down graph.
91 "CDEF:packets_tx_inv=packets_tx,-1,*",
92
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",
102
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",
112 ]
113
114 rrd_graph_args = [
115 "--title", _("Transferred packets on %(interface)s"),
116 "--vertical-label", _("Packets/s"),
117 ]
118
119
120class GraphTemplateInterfaceErrors(base.GraphTemplate):
121 name = "interface-errors"
122
123 rrd_graph = [
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",
129
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,*",
133
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",
143
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",
153
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",
163
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",
173
174 # Draw the collisions as a line.
175 "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n",
176 ]
177
178 rrd_graph_args = [
179 "--title", _("Errors/dropped packets on %(interface)s"),
180 "--vertical-label", _("Packets/s"),
181 ]
182
183
b1ea4956 184class DataSourceInterface(base.DataSource):
89b70961 185 name = "interface"
b1ea4956
MT
186 description = "Interface Statistics Data Source"
187
e9bb328f
MT
188 templates = [
189 GraphTemplateInterfaceBytes,
190 GraphTemplateInterfacePackets,
191 GraphTemplateInterfaceErrors,
192 ]
89b70961
MT
193
194 rrd_schema = [
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",
205 ]
206
207 @classmethod
208 def autocreate(cls, collecty, **kwargs):
209 if not os.path.exists(SYS_CLASS_NET):
210 return
211
212 instances = []
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):
216 continue
217
218 instance = cls(collecty, interface=interface)
219 instances.append(instance)
220
221 return instances
222
223 def init(self, **kwargs):
224 self.interface = kwargs.get("interface")
225
226 @property
227 def id(self):
228 return "-".join((self.name, self.interface))
229
230 def read(self):
231 files = (
232 "rx_bytes", "tx_bytes",
233 "collisions",
234 "rx_dropped", "tx_dropped",
235 "rx_errors", "tx_errors",
236 "multicast",
237 "rx_packets", "tx_packets",
238 )
239 ret = ["%s" % self.now,]
240
241 for file in files:
242 path = os.path.join(SYS_CLASS_NET, self.interface, "statistics", file)
243
244 # Open file and read it's content.
245 f = open(path)
246
247 line = f.readline()
248 line = line.strip()
249 ret.append(line)
250
251 f.close()
252
253 self.data.append(":".join(ret))