2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 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 ###############################################################################
28 CONNTRACK_FILE
= "/proc/net/nf_conntrack"
30 class ConntrackTable(object):
48 _stateful_layer4_protocols
= {
85 def __init__(self
, filename
):
86 with
open(filename
) as f
:
87 self
.layer3_protocols
= {}
88 for proto
in self
._layer
3_protocols
:
89 self
.layer3_protocols
[proto
] = 0
91 self
.layer4_protocols
= {}
92 for proto
in self
._layer
4_protocols
:
93 self
.layer4_protocols
[proto
] = 0
95 self
.protocol_states
= {}
96 for proto
, states
in self
._stateful
_layer
4_protocols
.items():
97 self
.protocol_states
[proto
] = dict((state
, 0) for state
in states
)
99 for line
in f
.readlines():
103 layer3_protocol
= line
[0]
106 self
.layer3_protocols
[layer3_protocol
] += 1
108 self
.layer3_protocols
["other"] += 1
111 layer4_protocol
= line
[2]
114 self
.layer4_protocols
[layer4_protocol
] += 1
116 self
.layer4_protocols
["other"] += 1
117 layer4_protocol
= "other"
119 # Count connection states
120 if layer4_protocol
in self
.protocol_states
:
124 self
.protocol_states
[layer4_protocol
][state
] += 1
129 class ConntrackLayer3ProtocolsGraphTemplate(base
.GraphTemplate
):
130 name
= "conntrack-layer3-protocols"
132 _protocols
= ConntrackTable
._layer
3_protocols
139 def get_object(self
, *args
):
140 return self
.plugin
.get_object("layer3-protocols")
144 # Order the protocols by standard deviation which will give us cleaner graphs
145 # http://stackoverflow.com/questions/13958409/how-to-graph-rrd-stackable-data-by-standard-deviation-to-maximize-readability
146 stddev
= self
.object.get_stddev()
149 for p
in self
._protocols
:
150 protos
[p
] = stddev
.get(p
)
152 return sorted(protos
, key
=protos
.get
)
155 def protocol_descriptions(self
):
156 _
= self
.locale
.translate
161 "other" : _("Other"),
165 def graph_title(self
):
166 _
= self
.locale
.translate
167 return _("Connections by Layer 3 Protocols")
170 def graph_vertical_label(self
):
171 _
= self
.locale
.translate
172 return _("Number of open connections")
180 _
= self
.locale
.translate
183 for proto
in self
.protocols
:
185 "colour" : self
.protocol_colours
.get(proto
, "#000000"),
186 "description" : self
.protocol_descriptions
.get(proto
, proto
),
190 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
191 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
192 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
193 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
196 args
+= self
.object.make_rrd_defs(proto
) + [
197 "AREA:%(proto)s%(colour)s:%(description)-15s:STACK" % i
,
198 "GPRINT:%(proto)s_cur:%(legend_cur)s" % i
,
199 "GPRINT:%(proto)s_avg:%(legend_avg)s" % i
,
200 "GPRINT:%(proto)s_min:%(legend_min)s" % i
,
201 "GPRINT:%(proto)s_max:%(legend_max)s\\n" % i
,
207 def rrd_graph_args(self
):
209 "--legend-direction=bottomup",
213 class ConntrackLayer4ProtocolsGraphTemplate(ConntrackLayer3ProtocolsGraphTemplate
):
214 name
= "conntrack-layer4-protocols"
221 "udplite" : "#3366cc",
227 def protocol_descriptions(self
):
228 _
= self
.locale
.translate
235 "udplite" : _("UDP Lite"),
238 "other" : _("Other"),
241 protocol_sortorder
= {
251 def get_object(self
, *args
):
252 return self
.plugin
.get_object("layer4-protocols")
255 def graph_title(self
):
256 _
= self
.locale
.translate
257 return _("Connections by IP Protocols")
260 def _protocols(self
):
261 return sorted(ConntrackTable
._layer
4_protocols
,
262 key
=lambda x
: self
.protocol_sortorder
.get(x
, 99))
265 class ConntrackProtocolWithStatesGraphTemplate(base
.GraphTemplate
):
266 name
= "conntrack-protocol-states"
272 "CLOSEREQ" : "#000000",
273 "CLOSING" : "#111111",
274 "IGNORE" : "#222222",
275 "INVALID" : "#333333",
278 "PARTOPEN" : "#666666",
279 "REQUEST" : "#777777",
280 "RESPOND" : "#888888",
281 "TIME_WAIT" : "#999999",
284 "CLOSED" : "#000000",
285 "COOKIE_ECHOED" : "#111111",
286 "COOKIE_WAIT" : "#222222",
287 "ESTABLISHED" : "#333333",
289 "SHUTDOWN_ACK_SENT" : "#555555",
290 "SHUTDOWN_RECD" : "#666666",
291 "SHUTDOWN_SENT" : "#777777",
295 "CLOSE_WAIT" : "#999999",
296 "ESTABLISHED" : "#000000",
297 "FIN_WAIT" : "#888888",
298 "LAST_ACK" : "#777777",
300 "SYN_RECV" : "#111111",
301 "SYN_SENT" : "#222222",
302 "SYN_SENT2" : "#333333",
303 "TIME_WAIT" : "#444444",
307 states_descriptions
= {
332 "SHUTDOWN_ACK_SENT" : 0,
351 def graph_title(self
):
352 _
= self
.locale
.translate
353 return _("Protocol States of all %s connections") % self
.protocol
.upper()
356 def graph_vertical_label(self
):
357 _
= self
.locale
.translate
358 return _("Number of open connections")
362 return self
.object.protocol
366 return sorted(ConntrackTable
._stateful
_layer
4_protocols
[self
.protocol
],
367 key
=lambda x
: self
.states_sortorder
[self
.protocol
].get(x
, 99))
371 _
= self
.locale
.translate
374 for state
in reversed(self
.states
):
376 "colour" : self
.states_colours
[self
.protocol
].get(state
, "#000000"),
377 "description" : self
.states_descriptions
[self
.protocol
].get(state
, state
),
378 "proto" : self
.protocol
,
381 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
382 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
383 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
384 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
387 args
+= self
.object.make_rrd_defs(state
) + [
388 "AREA:%(state)s%(colour)s:%(description)-15s:STACK" % i
,
389 "GPRINT:%(state)s_cur:%(legend_cur)s" % i
,
390 "GPRINT:%(state)s_avg:%(legend_avg)s" % i
,
391 "GPRINT:%(state)s_min:%(legend_min)s" % i
,
392 "GPRINT:%(state)s_max:%(legend_max)s\\n" % i
,
398 def rrd_graph_args(self
):
400 "--legend-direction=bottomup",
404 class ConntrackObject(base
.Object
):
407 def init(self
, conntrack_table
):
408 self
.conntrack_table
= conntrack_table
415 class ConntrackLayer3ProtocolsObject(ConntrackObject
):
416 protocols
= ConntrackTable
._layer
3_protocols
419 "DS:%s:GAUGE:0:U" % p
for p
in protocols
424 return "layer3-protocols"
429 for proto
in self
.protocols
:
430 r
= self
.conntrack_table
.layer3_protocols
.get(proto
, 0)
431 results
.append("%s" % r
)
436 class ConntrackLayer4ProtocolsObject(ConntrackObject
):
437 protocols
= ConntrackTable
._layer
4_protocols
440 "DS:%s:GAUGE:0:U" % p
for p
in protocols
445 return "layer4-protocols"
450 for proto
in self
.protocols
:
451 r
= self
.conntrack_table
.layer4_protocols
.get(proto
, 0)
452 results
.append("%s" % r
)
457 class ConntrackProtocolWithStatesObject(ConntrackObject
):
458 def init(self
, conntrack_table
, protocol
):
459 ConntrackObject
.init(self
, conntrack_table
)
460 self
.protocol
= protocol
463 return "<%s %s>" % (self
.__class
__.__name
__, self
.protocol
)
467 return ConntrackTable
._stateful
_layer
4_protocols
.get(self
.protocol
)
470 def rrd_schema(self
):
471 return ["DS:%s:GAUGE:0:U" % state
for state
in self
.states
]
473 def get_states(self
):
476 for state
in self
.states
:
477 r
= self
.conntrack_table
.protocol_states
[self
.protocol
].get(state
, 0)
478 results
.append("%s" % r
)
483 return self
.get_states()
486 class ConntrackPlugin(base
.Plugin
):
488 description
= "Conntrack Plugin"
491 ConntrackLayer3ProtocolsGraphTemplate
,
492 ConntrackLayer4ProtocolsGraphTemplate
,
493 ConntrackProtocolWithStatesGraphTemplate
,
498 ct
= self
.get_conntrack_table()
501 yield ConntrackLayer3ProtocolsObject(self
, ct
)
502 yield ConntrackLayer4ProtocolsObject(self
, ct
)
504 for protocol
in ConntrackTable
._stateful
_layer
4_protocols
:
505 yield ConntrackProtocolWithStatesObject(self
, ct
, protocol
)
507 def get_conntrack_table(self
):
508 if not os
.path
.exists(CONNTRACK_FILE
):
511 return ConntrackTable(CONNTRACK_FILE
)