]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | from __future__ import division | |
23 | ||
24 | import os | |
25 | ||
26 | import base | |
27 | ||
28 | from ..i18n import _ | |
29 | ||
30 | SYS_CLASS_NET = "/sys/class/net" | |
31 | ||
bba5cf66 MT |
32 | COLOUR_RX = "B22222" |
33 | COLOUR_RX_AREA = "%sAA" % COLOUR_RX | |
34 | COLOUR_TX = "228B22" | |
35 | COLOUR_TX_AREA = "%sAA" % COLOUR_TX | |
e9bb328f | 36 | |
2a3d5c1b MT |
37 | class GraphTemplateInterfaceBase(base.GraphTemplate): |
38 | @property | |
39 | def interface(self): | |
40 | return self.object.interface | |
41 | ||
42 | ||
43 | class GraphTemplateInterfaceBits(GraphTemplateInterfaceBase): | |
bba5cf66 MT |
44 | name = "interface-bits" |
45 | ||
46 | @property | |
47 | def rrd_graph(self): | |
48 | return [ | |
49 | "DEF:bytes_rx=%(file)s:bytes_rx:AVERAGE", | |
50 | "DEF:bytes_tx=%(file)s:bytes_tx:AVERAGE", | |
51 | ||
52 | # Convert everything into bits. | |
53 | "CDEF:bits_rx=bytes_rx,8,*", | |
54 | "CDEF:bits_tx=bytes_tx,8,*", | |
55 | ||
56 | # Compute 95% lines. | |
57 | "VDEF:bits_rx_95p=bits_rx,95,PERCENT", | |
58 | "VDEF:bits_tx_95p=bits_tx,95,PERCENT", | |
59 | ||
60 | # Draw the received area. | |
61 | "AREA:bits_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")), | |
62 | "VDEF:bits_rx_min=bits_rx,MINIMUM", | |
63 | "VDEF:bits_rx_max=bits_rx,MAXIMUM", | |
64 | "VDEF:bits_rx_avg=bits_rx,AVERAGE", | |
65 | "GPRINT:bits_rx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"), | |
66 | "GPRINT:bits_rx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"), | |
67 | "GPRINT:bits_rx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n", | |
68 | ||
69 | # Draw the transmitted area. | |
70 | "AREA:bits_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")), | |
71 | "VDEF:bits_tx_min=bits_tx,MINIMUM", | |
72 | "VDEF:bits_tx_max=bits_tx,MAXIMUM", | |
73 | "VDEF:bits_tx_avg=bits_tx,AVERAGE", | |
74 | "GPRINT:bits_tx_max:%12s\: " % _("Maximum") + _("%8.2lf %sbps"), | |
75 | "GPRINT:bits_tx_min:%12s\: " % _("Minimum") + _("%8.2lf %sbps"), | |
76 | "GPRINT:bits_tx_avg:%12s\: " % _("Average") + _("%8.2lf %sbps") + "\\n", | |
77 | ||
78 | # Draw outlines. | |
79 | "LINE1:bits_rx#%s" % COLOUR_RX, | |
80 | "LINE1:bits_tx#%s" % COLOUR_TX, | |
81 | ||
82 | # Draw the 95% lines. | |
83 | "COMMENT:--- %s ---\\n" % _("95th percentile"), | |
84 | "LINE2:bits_rx_95p#%s:%-15s" % (COLOUR_RX, _("Received")), | |
85 | "GPRINT:bits_rx_95p:%s" % _("%8.2lf %sbps") + "\\n", | |
86 | "LINE2:bits_tx_95p#%s:%-15s" % (COLOUR_TX, _("Transmitted")), | |
87 | "GPRINT:bits_tx_95p:%s" % _("%8.2lf %sbps") + "\\n", | |
88 | ] | |
89 | ||
90 | @property | |
f181246a | 91 | def graph_title(self): |
2a3d5c1b | 92 | return _("Bandwidth usage on %s") % self.interface |
f181246a MT |
93 | |
94 | @property | |
95 | def graph_vertical_label(self): | |
96 | return _("Bit/s") | |
e9bb328f MT |
97 | |
98 | ||
2a3d5c1b | 99 | class GraphTemplateInterfacePackets(GraphTemplateInterfaceBase): |
e9bb328f MT |
100 | name = "interface-packets" |
101 | ||
bba5cf66 MT |
102 | @property |
103 | def rrd_graph(self): | |
104 | return [ | |
105 | "DEF:packets_rx=%(file)s:packets_rx:AVERAGE", | |
106 | "DEF:packets_tx=%(file)s:packets_tx:AVERAGE", | |
107 | ||
108 | # Draw the received area. | |
109 | "AREA:packets_rx#%s:%-15s" % (COLOUR_RX_AREA, _("Received")), | |
110 | "VDEF:packets_rx_min=packets_rx,MINIMUM", | |
111 | "VDEF:packets_rx_max=packets_rx,MAXIMUM", | |
112 | "VDEF:packets_rx_avg=packets_rx,AVERAGE", | |
113 | "GPRINT:packets_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
114 | "GPRINT:packets_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
115 | "GPRINT:packets_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
116 | ||
117 | # Draw the transmitted area. | |
118 | "AREA:packets_tx#%s:%-15s" % (COLOUR_TX_AREA, _("Transmitted")), | |
119 | "VDEF:packets_tx_min=packets_tx,MINIMUM", | |
120 | "VDEF:packets_tx_max=packets_tx,MAXIMUM", | |
121 | "VDEF:packets_tx_avg=packets_tx,AVERAGE", | |
122 | "GPRINT:packets_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
123 | "GPRINT:packets_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
124 | "GPRINT:packets_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
125 | ||
126 | # Draw outlines of the areas on top. | |
127 | "LINE1:packets_rx#%s" % COLOUR_RX, | |
128 | "LINE1:packets_tx#%s" % COLOUR_TX, | |
129 | ] | |
e9bb328f | 130 | |
bba5cf66 | 131 | @property |
f181246a | 132 | def graph_title(self): |
2a3d5c1b | 133 | return _("Transferred packets on %s") % self.interface |
f181246a MT |
134 | |
135 | @property | |
136 | def graph_vertical_label(self): | |
137 | return _("Packets/s") | |
e9bb328f MT |
138 | |
139 | ||
2a3d5c1b | 140 | class GraphTemplateInterfaceErrors(GraphTemplateInterfaceBase): |
e9bb328f MT |
141 | name = "interface-errors" |
142 | ||
bba5cf66 MT |
143 | @property |
144 | def rrd_graph(self): | |
145 | return [ | |
146 | "DEF:errors_rx=%(file)s:errors_rx:AVERAGE", | |
147 | "DEF:errors_tx=%(file)s:errors_tx:AVERAGE", | |
148 | "DEF:dropped_rx=%(file)s:dropped_rx:AVERAGE", | |
149 | "DEF:dropped_tx=%(file)s:dropped_tx:AVERAGE", | |
150 | "DEF:collisions=%(file)s:collisions:AVERAGE", | |
151 | ||
152 | # Invert the transmitted packets to create upside down graph. | |
153 | "CDEF:errors_tx_inv=errors_tx,-1,*", | |
154 | "CDEF:dropped_tx_inv=dropped_tx,-1,*", | |
155 | ||
156 | # Draw the receive errors. | |
157 | "AREA:errors_rx#228B2277:%-15s" % _("Receive errors"), | |
158 | "VDEF:errors_rx_min=errors_rx,MINIMUM", | |
159 | "VDEF:errors_rx_max=errors_rx,MAXIMUM", | |
160 | "VDEF:errors_rx_avg=errors_rx,AVERAGE", | |
161 | "GPRINT:errors_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
162 | "GPRINT:errors_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
163 | "GPRINT:errors_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
164 | "LINE1:errors_rx#228B22", | |
165 | ||
166 | # Draw the transmit errors. | |
167 | "AREA:errors_tx_inv#B2222277:%-15s" % _("Transmit errors"), | |
168 | "VDEF:errors_tx_min=errors_tx,MINIMUM", | |
169 | "VDEF:errors_tx_max=errors_tx,MAXIMUM", | |
170 | "VDEF:errors_tx_avg=errors_tx,AVERAGE", | |
171 | "GPRINT:errors_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
172 | "GPRINT:errors_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
173 | "GPRINT:errors_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
174 | "LINE1:errors_tx_inv#B22222", | |
175 | ||
176 | # Draw the receive drops. | |
177 | "LINE2:dropped_rx#228B22:%-15s" % _("Receive drops"), | |
178 | "VDEF:dropped_rx_min=dropped_rx,MINIMUM", | |
179 | "VDEF:dropped_rx_max=dropped_rx,MAXIMUM", | |
180 | "VDEF:dropped_rx_avg=dropped_rx,AVERAGE", | |
181 | "GPRINT:dropped_rx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
182 | "GPRINT:dropped_rx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
183 | "GPRINT:dropped_rx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
184 | "LINE1:dropped_rx#228B22", | |
185 | ||
186 | # Draw the transmit drops. | |
187 | "LINE2:dropped_tx#B22222:%-15s" % _("Transmit drops"), | |
188 | "VDEF:dropped_tx_min=dropped_tx,MINIMUM", | |
189 | "VDEF:dropped_tx_max=dropped_tx,MAXIMUM", | |
190 | "VDEF:dropped_tx_avg=dropped_tx,AVERAGE", | |
191 | "GPRINT:dropped_tx_max:%12s\: " % _("Maximum") + _("%8.0lf %spps"), | |
192 | "GPRINT:dropped_tx_min:%12s\: " % _("Minimum") + _("%8.0lf %spps"), | |
193 | "GPRINT:dropped_tx_avg:%12s\: " % _("Average") + _("%8.2lf %spps") + "\\n", | |
194 | "LINE1:dropped_tx#B22222", | |
195 | ||
196 | # Draw the collisions as a line. | |
197 | "LINE3:collisions#8B0000:%-15s" % _("Collisions") + "\\n", | |
198 | ] | |
e9bb328f | 199 | |
bba5cf66 | 200 | @property |
f181246a | 201 | def graph_title(self): |
2a3d5c1b | 202 | return _("Errors/dropped packets on %s") % self.interface |
f181246a MT |
203 | |
204 | @property | |
205 | def graph_vertical_label(self): | |
206 | return _("Packets/s") | |
e9bb328f MT |
207 | |
208 | ||
72364063 | 209 | class InterfaceObject(base.Object): |
89b70961 MT |
210 | rrd_schema = [ |
211 | "DS:bytes_rx:DERIVE:0:U", | |
212 | "DS:bytes_tx:DERIVE:0:U", | |
213 | "DS:collisions:DERIVE:0:U", | |
214 | "DS:dropped_rx:DERIVE:0:U", | |
215 | "DS:dropped_tx:DERIVE:0:U", | |
216 | "DS:errors_rx:DERIVE:0:U", | |
217 | "DS:errors_tx:DERIVE:0:U", | |
218 | "DS:multicast:DERIVE:0:U", | |
219 | "DS:packets_rx:DERIVE:0:U", | |
220 | "DS:packets_tx:DERIVE:0:U", | |
221 | ] | |
222 | ||
72364063 MT |
223 | def __repr__(self): |
224 | return "<%s %s>" % (self.__class__.__name__, self.interface) | |
bba5cf66 | 225 | |
72364063 MT |
226 | def init(self, interface): |
227 | self.interface = interface | |
89b70961 MT |
228 | |
229 | @property | |
230 | def id(self): | |
72364063 | 231 | return self.interface |
89b70961 | 232 | |
72364063 | 233 | def collect(self): |
bba5cf66 MT |
234 | interface_path = os.path.join(SYS_CLASS_NET, self.interface) |
235 | ||
236 | # Check if the interface exists. | |
237 | if not os.path.exists(interface_path): | |
238 | self.log.debug(_("Interface %s does not exists. Cannot collect.") \ | |
239 | % self.interface) | |
240 | return | |
241 | ||
89b70961 MT |
242 | files = ( |
243 | "rx_bytes", "tx_bytes", | |
244 | "collisions", | |
245 | "rx_dropped", "tx_dropped", | |
246 | "rx_errors", "tx_errors", | |
247 | "multicast", | |
248 | "rx_packets", "tx_packets", | |
249 | ) | |
bba5cf66 | 250 | ret = [] |
89b70961 MT |
251 | |
252 | for file in files: | |
bba5cf66 | 253 | path = os.path.join(interface_path, "statistics", file) |
89b70961 MT |
254 | |
255 | # Open file and read it's content. | |
bba5cf66 MT |
256 | f = None |
257 | try: | |
258 | f = open(path) | |
259 | ||
260 | line = f.readline() | |
261 | line = line.strip() | |
262 | ret.append(line) | |
263 | except: | |
264 | ret.append("0") | |
265 | raise | |
266 | ||
267 | finally: | |
268 | if f: | |
269 | f.close() | |
270 | ||
f648421a | 271 | return ret |
72364063 MT |
272 | |
273 | ||
274 | class InterfacePlugin(base.Plugin): | |
275 | name = "interface" | |
276 | description = "Interface Statistics Plugin" | |
277 | ||
278 | templates = [ | |
279 | GraphTemplateInterfaceBits, | |
280 | GraphTemplateInterfacePackets, | |
281 | GraphTemplateInterfaceErrors, | |
282 | ] | |
283 | ||
284 | interval = 20 | |
285 | ||
286 | def get_interfaces(self): | |
287 | for interface in os.listdir(SYS_CLASS_NET): | |
288 | # Skip some unwanted interfaces. | |
289 | if interface == "lo" or interface.startswith("mon."): | |
290 | continue | |
291 | ||
292 | path = os.path.join(SYS_CLASS_NET, interface) | |
293 | if not os.path.isdir(path): | |
294 | continue | |
295 | ||
296 | yield interface | |
297 | ||
298 | @property | |
299 | def objects(self): | |
300 | for interface in self.get_interfaces(): | |
301 | yield InterfaceObject(self, interface=interface) |