]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/conntrack.py
731d5bf2514789a0d214de10ab403265d5b8d929
[collecty.git] / src / collecty / plugins / conntrack.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 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 import os
23
24 from . import base
25
26 from ..i18n import _
27
28 CONNTRACK_FILE = "/proc/net/nf_conntrack"
29
30 class ConntrackTable(object):
31 _layer3_protocols = (
32 "ipv6",
33 "ipv4",
34 "other",
35 )
36
37 _layer4_protocols = (
38 "dccp",
39 "icmp",
40 "igmp",
41 "sctp",
42 "tcp",
43 "udp",
44 "udplite",
45 "other",
46 )
47
48 _stateful_layer4_protocols = {
49 "dccp" : (
50 "CLOSEREQ",
51 "CLOSING",
52 "IGNORE",
53 "INVALID",
54 "NONE",
55 "OPEN",
56 "PARTOPEN",
57 "REQUEST",
58 "RESPOND",
59 "TIME_WAIT",
60 ),
61 "sctp" : (
62 "CLOSED",
63 "COOKIE_ECHOED",
64 "COOKIE_WAIT",
65 "ESTABLISHED",
66 "NONE",
67 "SHUTDOWN_ACK_SENT",
68 "SHUTDOWN_RECD",
69 "SHUTDOWN_SENT",
70 ),
71 "tcp" : (
72 "CLOSE",
73 "CLOSE_WAIT",
74 "ESTABLISHED",
75 "FIN_WAIT",
76 "LAST_ACK",
77 "NONE",
78 "SYN_RECV",
79 "SYN_SENT",
80 "SYN_SENT2",
81 "TIME_WAIT",
82 ),
83 }
84
85 def __init__(self, filename):
86 with open(filename) as f:
87 self.layer3_protocols = {}
88 for proto in self._layer3_protocols:
89 self.layer3_protocols[proto] = 0
90
91 self.layer4_protocols = {}
92 for proto in self._layer4_protocols:
93 self.layer4_protocols[proto] = 0
94
95 self.protocol_states = {}
96 for proto, states in self._stateful_layer4_protocols.items():
97 self.protocol_states[proto] = dict((state, 0) for state in states)
98
99 for line in f.readlines():
100 line = line.split()
101
102 # Layer 3 protocol
103 layer3_protocol = line[0]
104
105 try:
106 self.layer3_protocols[layer3_protocol] += 1
107 except KeyError:
108 self.layer3_protocols["other"] += 1
109
110 # Layer 4 protocol
111 layer4_protocol = line[2]
112
113 try:
114 self.layer4_protocols[layer4_protocol] += 1
115 except KeyError:
116 self.layer4_protocols["other"] += 1
117 layer4_protocol = "other"
118
119 # Count connection states
120 if layer4_protocol in self.protocol_states:
121 state = line[5]
122
123 try:
124 self.protocol_states[layer4_protocol][state] += 1
125 except KeyError:
126 pass
127
128
129 class ConntrackLayer3ProtocolsGraphTemplate(base.GraphTemplate):
130 name = "conntrack-layer3-protocols"
131
132 protocols = ConntrackTable._layer3_protocols
133
134 protocol_colours = {
135 "ipv6" : "#cc0033",
136 "ipv4" : "#cccc33",
137 }
138
139 def get_object(self, *args):
140 return self.plugin.get_object("layer3-protocols")
141
142 @property
143 def protocol_descriptions(self):
144 _ = self.locale.translate
145
146 return {
147 "ipv6" : _("IPv6"),
148 "ipv4" : _("IPv4"),
149 "other" : _("Other"),
150 }
151
152 @property
153 def graph_title(self):
154 _ = self.locale.translate
155 return _("Connections by Layer 3 Protocols")
156
157 @property
158 def graph_vertical_label(self):
159 _ = self.locale.translate
160 return _("Number of open connections")
161
162 @property
163 def rrd_defs(self):
164 return []
165
166 @property
167 def rrd_graph(self):
168 _ = self.locale.translate
169 args = []
170
171 for proto in reversed(self.protocols):
172 i = {
173 "colour" : self.protocol_colours.get(proto, "#000000"),
174 "description" : self.protocol_descriptions.get(proto, proto),
175 "proto" : proto,
176 "type" : type,
177
178 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
179 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
180 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
181 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
182 }
183
184 args += self.object.make_rrd_defs(proto) + [
185 "AREA:%(proto)s%(colour)s:%(description)-15s:STACK" % i,
186 "GPRINT:%(proto)s_cur:%(legend_cur)s" % i,
187 "GPRINT:%(proto)s_avg:%(legend_avg)s" % i,
188 "GPRINT:%(proto)s_min:%(legend_min)s" % i,
189 "GPRINT:%(proto)s_max:%(legend_max)s\\n" % i,
190 ]
191
192 return args
193
194 @property
195 def rrd_graph_args(self):
196 return [
197 "--legend-direction=bottomup",
198 ]
199
200
201 class ConntrackLayer4ProtocolsGraphTemplate(ConntrackLayer3ProtocolsGraphTemplate):
202 name = "conntrack-layer4-protocols"
203
204 protocol_colours = {
205 "tcp" : "#336600",
206 "udp" : "#666633",
207 "icmp" : "#336666",
208 "igmp" : "#666699",
209 "udplite" : "#3366cc",
210 "sctp" : "#6666ff",
211 "dccp" : "#33cc00",
212 }
213
214 @property
215 def protocol_descriptions(self):
216 _ = self.locale.translate
217
218 return {
219 "tcp" : _("TCP"),
220 "udp" : _("UDP"),
221 "icmp" : _("ICMP"),
222 "igmp" : _("IGMP"),
223 "udplite" : _("UDP Lite"),
224 "sctp" : _("SCTP"),
225 "dccp" : _("DCCP"),
226 "other" : _("Other"),
227 }
228
229 protocol_sortorder = {
230 "tcp" : 1,
231 "udp" : 2,
232 "icmp" : 3,
233 "igmp" : 4,
234 "udplite" : 5,
235 "sctp" : 6,
236 "dccp" : 7,
237 }
238
239 def get_object(self, *args):
240 return self.plugin.get_object("layer4-protocols")
241
242 @property
243 def graph_title(self):
244 _ = self.locale.translate
245 return _("Connections by IP Protocols")
246
247 @property
248 def protocols(self):
249 return sorted(ConntrackTable._layer4_protocols,
250 key=lambda x: self.protocol_sortorder.get(x, 99))
251
252
253 class ConntrackProtocolWithStatesGraphTemplate(base.GraphTemplate):
254 name = "conntrack-protocol-states"
255
256 lower_limit = 0
257
258 states_colours = {
259 "dccp" : {
260 "CLOSEREQ" : "#000000",
261 "CLOSING" : "#111111",
262 "IGNORE" : "#222222",
263 "INVALID" : "#333333",
264 "NONE" : "#444444",
265 "OPEN" : "#555555",
266 "PARTOPEN" : "#666666",
267 "REQUEST" : "#777777",
268 "RESPOND" : "#888888",
269 "TIME_WAIT" : "#999999",
270 },
271 "sctp" : {
272 "CLOSED" : "#000000",
273 "COOKIE_ECHOED" : "#111111",
274 "COOKIE_WAIT" : "#222222",
275 "ESTABLISHED" : "#333333",
276 "NONE" : "#444444",
277 "SHUTDOWN_ACK_SENT" : "#555555",
278 "SHUTDOWN_RECD" : "#666666",
279 "SHUTDOWN_SENT" : "#777777",
280 },
281 "tcp" : {
282 "CLOSE" : "#ffffff",
283 "CLOSE_WAIT" : "#999999",
284 "ESTABLISHED" : "#000000",
285 "FIN_WAIT" : "#888888",
286 "LAST_ACK" : "#777777",
287 "NONE" : "#000000",
288 "SYN_RECV" : "#111111",
289 "SYN_SENT" : "#222222",
290 "SYN_SENT2" : "#333333",
291 "TIME_WAIT" : "#444444",
292 },
293 }
294
295 states_descriptions = {
296 "dccp" : {},
297 "sctp" : {},
298 "tcp" : {},
299 }
300
301 states_sortorder = {
302 "dccp" : {
303 "CLOSEREQ" : 0,
304 "CLOSING" : 0,
305 "IGNORE" : 0,
306 "INVALID" : 0,
307 "NONE" : 0,
308 "OPEN" : 0,
309 "PARTOPEN" : 0,
310 "REQUEST" : 0,
311 "RESPOND" : 0,
312 "TIME_WAIT" : 0,
313 },
314 "sctp" : {
315 "CLOSED" : 0,
316 "COOKIE_ECHOED" : 0,
317 "COOKIE_WAIT" : 0,
318 "ESTABLISHED" : 0,
319 "NONE" : 0,
320 "SHUTDOWN_ACK_SENT" : 0,
321 "SHUTDOWN_RECD" : 0,
322 "SHUTDOWN_SENT" : 0,
323 },
324 "tcp" : {
325 "CLOSE" : 9,
326 "CLOSE_WAIT" : 8,
327 "ESTABLISHED" : 1,
328 "FIN_WAIT" : 6,
329 "LAST_ACK" : 7,
330 "NONE" : 10,
331 "SYN_RECV" : 2,
332 "SYN_SENT" : 3,
333 "SYN_SENT2" : 4,
334 "TIME_WAIT" : 5,
335 },
336 }
337
338 @property
339 def graph_title(self):
340 _ = self.locale.translate
341 return _("Protocol States of all %s connections") % self.protocol.upper()
342
343 @property
344 def graph_vertical_label(self):
345 _ = self.locale.translate
346 return _("Number of open connections")
347
348 @property
349 def protocol(self):
350 return self.object.protocol
351
352 @property
353 def states(self):
354 return sorted(ConntrackTable._stateful_layer4_protocols[self.protocol],
355 key=lambda x: self.states_sortorder[self.protocol].get(x, 99))
356
357 @property
358 def rrd_graph(self):
359 _ = self.locale.translate
360 args = []
361
362 for state in reversed(self.states):
363 i = {
364 "colour" : self.states_colours[self.protocol].get(state, "#000000"),
365 "description" : self.states_descriptions[self.protocol].get(state, state),
366 "proto" : self.protocol,
367 "state" : state,
368
369 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
370 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
371 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
372 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
373 }
374
375 args += self.object.make_rrd_defs(state) + [
376 "AREA:%(state)s%(colour)s:%(description)-15s:STACK" % i,
377 "GPRINT:%(state)s_cur:%(legend_cur)s" % i,
378 "GPRINT:%(state)s_avg:%(legend_avg)s" % i,
379 "GPRINT:%(state)s_min:%(legend_min)s" % i,
380 "GPRINT:%(state)s_max:%(legend_max)s\\n" % i,
381 ]
382
383 return args
384
385 @property
386 def rrd_graph_args(self):
387 return [
388 "--legend-direction=bottomup",
389 ]
390
391
392 class ConntrackObject(base.Object):
393 protocol = None
394
395 def init(self, conntrack_table):
396 self.conntrack_table = conntrack_table
397
398 @property
399 def id(self):
400 return self.protocol
401
402
403 class ConntrackLayer3ProtocolsObject(ConntrackObject):
404 protocols = ConntrackTable._layer3_protocols
405
406 rrd_schema = [
407 "DS:%s:GAUGE:0:U" % p for p in protocols
408 ]
409
410 @property
411 def id(self):
412 return "layer3-protocols"
413
414 def collect(self):
415 results = []
416
417 for proto in self.protocols:
418 r = self.conntrack_table.layer3_protocols.get(proto, 0)
419 results.append("%s" % r)
420
421 return results
422
423
424 class ConntrackLayer4ProtocolsObject(ConntrackObject):
425 protocols = ConntrackTable._layer4_protocols
426
427 rrd_schema = [
428 "DS:%s:GAUGE:0:U" % p for p in protocols
429 ]
430
431 @property
432 def id(self):
433 return "layer4-protocols"
434
435 def collect(self):
436 results = []
437
438 for proto in self.protocols:
439 r = self.conntrack_table.layer4_protocols.get(proto, 0)
440 results.append("%s" % r)
441
442 return results
443
444
445 class ConntrackProtocolWithStatesObject(ConntrackObject):
446 def init(self, conntrack_table, protocol):
447 ConntrackObject.init(self, conntrack_table)
448 self.protocol = protocol
449
450 def __repr__(self):
451 return "<%s %s>" % (self.__class__.__name__, self.protocol)
452
453 @property
454 def states(self):
455 return ConntrackTable._stateful_layer4_protocols.get(self.protocol)
456
457 @property
458 def rrd_schema(self):
459 return ["DS:%s:GAUGE:0:U" % state for state in self.states]
460
461 def get_states(self):
462 results = []
463
464 for state in self.states:
465 r = self.conntrack_table.protocol_states[self.protocol].get(state, 0)
466 results.append("%s" % r)
467
468 return results
469
470 def collect(self):
471 return self.get_states()
472
473
474 class ConntrackPlugin(base.Plugin):
475 name = "conntrack"
476 description = "Conntrack Plugin"
477
478 templates = [
479 ConntrackLayer3ProtocolsGraphTemplate,
480 ConntrackLayer4ProtocolsGraphTemplate,
481 ConntrackProtocolWithStatesGraphTemplate,
482 ]
483
484 @property
485 def objects(self):
486 ct = self.get_conntrack_table()
487
488 if ct:
489 yield ConntrackLayer3ProtocolsObject(self, ct)
490 yield ConntrackLayer4ProtocolsObject(self, ct)
491
492 for protocol in ConntrackTable._stateful_layer4_protocols:
493 yield ConntrackProtocolWithStatesObject(self, ct, protocol)
494
495 def get_conntrack_table(self):
496 if not os.path.exists(CONNTRACK_FILE):
497 return
498
499 return ConntrackTable(CONNTRACK_FILE)