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")
143 def protocol_descriptions(self
):
144 _
= self
.locale
.translate
149 "other" : _("Other"),
153 def graph_title(self
):
154 _
= self
.locale
.translate
155 return _("Connections by Layer 3 Protocols")
158 def graph_vertical_label(self
):
159 _
= self
.locale
.translate
160 return _("Number of open connections")
168 _
= self
.locale
.translate
171 for proto
in reversed(self
.protocols
):
173 "colour" : self
.protocol_colours
.get(proto
, "#000000"),
174 "description" : self
.protocol_descriptions
.get(proto
, proto
),
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"),
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
,
195 def rrd_graph_args(self
):
197 "--legend-direction=bottomup",
201 class ConntrackLayer4ProtocolsGraphTemplate(ConntrackLayer3ProtocolsGraphTemplate
):
202 name
= "conntrack-layer4-protocols"
209 "udplite" : "#3366cc",
215 def protocol_descriptions(self
):
216 _
= self
.locale
.translate
223 "udplite" : _("UDP Lite"),
226 "other" : _("Other"),
229 protocol_sortorder
= {
239 def get_object(self
, *args
):
240 return self
.plugin
.get_object("layer4-protocols")
243 def graph_title(self
):
244 _
= self
.locale
.translate
245 return _("Connections by IP Protocols")
249 return sorted(ConntrackTable
._layer
4_protocols
,
250 key
=lambda x
: self
.protocol_sortorder
.get(x
, 99))
253 class ConntrackProtocolWithStatesGraphTemplate(base
.GraphTemplate
):
254 name
= "conntrack-protocol-states"
260 "CLOSEREQ" : "#000000",
261 "CLOSING" : "#111111",
262 "IGNORE" : "#222222",
263 "INVALID" : "#333333",
266 "PARTOPEN" : "#666666",
267 "REQUEST" : "#777777",
268 "RESPOND" : "#888888",
269 "TIME_WAIT" : "#999999",
272 "CLOSED" : "#000000",
273 "COOKIE_ECHOED" : "#111111",
274 "COOKIE_WAIT" : "#222222",
275 "ESTABLISHED" : "#333333",
277 "SHUTDOWN_ACK_SENT" : "#555555",
278 "SHUTDOWN_RECD" : "#666666",
279 "SHUTDOWN_SENT" : "#777777",
283 "CLOSE_WAIT" : "#999999",
284 "ESTABLISHED" : "#000000",
285 "FIN_WAIT" : "#888888",
286 "LAST_ACK" : "#777777",
288 "SYN_RECV" : "#111111",
289 "SYN_SENT" : "#222222",
290 "SYN_SENT2" : "#333333",
291 "TIME_WAIT" : "#444444",
295 states_descriptions
= {
320 "SHUTDOWN_ACK_SENT" : 0,
339 def graph_title(self
):
340 _
= self
.locale
.translate
341 return _("Protocol States of all %s connections") % self
.protocol
.upper()
344 def graph_vertical_label(self
):
345 _
= self
.locale
.translate
346 return _("Number of open connections")
350 return self
.object.protocol
354 return sorted(ConntrackTable
._stateful
_layer
4_protocols
[self
.protocol
],
355 key
=lambda x
: self
.states_sortorder
[self
.protocol
].get(x
, 99))
359 _
= self
.locale
.translate
362 for state
in reversed(self
.states
):
364 "colour" : self
.states_colours
[self
.protocol
].get(state
, "#000000"),
365 "description" : self
.states_descriptions
[self
.protocol
].get(state
, state
),
366 "proto" : self
.protocol
,
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"),
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
,
386 def rrd_graph_args(self
):
388 "--legend-direction=bottomup",
392 class ConntrackObject(base
.Object
):
395 def init(self
, conntrack_table
):
396 self
.conntrack_table
= conntrack_table
403 class ConntrackLayer3ProtocolsObject(ConntrackObject
):
404 protocols
= ConntrackTable
._layer
3_protocols
407 "DS:%s:GAUGE:0:U" % p
for p
in protocols
412 return "layer3-protocols"
417 for proto
in self
.protocols
:
418 r
= self
.conntrack_table
.layer3_protocols
.get(proto
, 0)
419 results
.append("%s" % r
)
424 class ConntrackLayer4ProtocolsObject(ConntrackObject
):
425 protocols
= ConntrackTable
._layer
4_protocols
428 "DS:%s:GAUGE:0:U" % p
for p
in protocols
433 return "layer4-protocols"
438 for proto
in self
.protocols
:
439 r
= self
.conntrack_table
.layer4_protocols
.get(proto
, 0)
440 results
.append("%s" % r
)
445 class ConntrackProtocolWithStatesObject(ConntrackObject
):
446 def init(self
, conntrack_table
, protocol
):
447 ConntrackObject
.init(self
, conntrack_table
)
448 self
.protocol
= protocol
451 return "<%s %s>" % (self
.__class
__.__name
__, self
.protocol
)
455 return ConntrackTable
._stateful
_layer
4_protocols
.get(self
.protocol
)
458 def rrd_schema(self
):
459 return ["DS:%s:GAUGE:0:U" % state
for state
in self
.states
]
461 def get_states(self
):
464 for state
in self
.states
:
465 r
= self
.conntrack_table
.protocol_states
[self
.protocol
].get(state
, 0)
466 results
.append("%s" % r
)
471 return self
.get_states()
474 class ConntrackPlugin(base
.Plugin
):
476 description
= "Conntrack Plugin"
479 ConntrackLayer3ProtocolsGraphTemplate
,
480 ConntrackLayer4ProtocolsGraphTemplate
,
481 ConntrackProtocolWithStatesGraphTemplate
,
486 ct
= self
.get_conntrack_table()
489 yield ConntrackLayer3ProtocolsObject(self
, ct
)
490 yield ConntrackLayer4ProtocolsObject(self
, ct
)
492 for protocol
in ConntrackTable
._stateful
_layer
4_protocols
:
493 yield ConntrackProtocolWithStatesObject(self
, ct
, protocol
)
495 def get_conntrack_table(self
):
496 if not os
.path
.exists(CONNTRACK_FILE
):
499 return ConntrackTable(CONNTRACK_FILE
)