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_objects(self
, *args
):
141 self
.plugin
.get_object("layer3-protocols"),
146 # Order the protocols by standard deviation which will give us cleaner graphs
147 # http://stackoverflow.com/questions/13958409/how-to-graph-rrd-stackable-data-by-standard-deviation-to-maximize-readability
148 stddev
= self
.object.get_stddev()
151 for p
in self
._protocols
:
152 protos
[p
] = stddev
.get(p
)
154 return sorted(protos
, key
=protos
.get
)
157 def protocol_descriptions(self
):
158 _
= self
.locale
.translate
163 "other" : _("Other"),
167 def graph_title(self
):
168 _
= self
.locale
.translate
169 return _("Connections by Layer 3 Protocols")
172 def graph_vertical_label(self
):
173 _
= self
.locale
.translate
174 return _("Number of open connections")
182 _
= self
.locale
.translate
185 for proto
in self
.protocols
:
187 "colour" : self
.protocol_colours
.get(proto
, "#000000"),
188 "description" : self
.protocol_descriptions
.get(proto
, proto
),
192 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
193 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
194 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
195 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
198 args
+= self
.object.make_rrd_defs(proto
) + [
199 "AREA:%(proto)s%(colour)s:%(description)-15s:STACK" % i
,
200 "GPRINT:%(proto)s_cur:%(legend_cur)s" % i
,
201 "GPRINT:%(proto)s_avg:%(legend_avg)s" % i
,
202 "GPRINT:%(proto)s_min:%(legend_min)s" % i
,
203 "GPRINT:%(proto)s_max:%(legend_max)s\\n" % i
,
209 def rrd_graph_args(self
):
211 "--legend-direction=bottomup",
215 class ConntrackLayer4ProtocolsGraphTemplate(ConntrackLayer3ProtocolsGraphTemplate
):
216 name
= "conntrack-layer4-protocols"
223 "udplite" : "#3366cc",
229 def protocol_descriptions(self
):
230 _
= self
.locale
.translate
237 "udplite" : _("UDP Lite"),
240 "other" : _("Other"),
243 protocol_sortorder
= {
253 def get_objects(self
, *args
):
255 self
.plugin
.get_object("layer4-protocols"),
259 def graph_title(self
):
260 _
= self
.locale
.translate
261 return _("Connections by IP Protocols")
264 def _protocols(self
):
265 return sorted(ConntrackTable
._layer
4_protocols
,
266 key
=lambda x
: self
.protocol_sortorder
.get(x
, 99))
269 class ConntrackProtocolWithStatesGraphTemplate(base
.GraphTemplate
):
270 name
= "conntrack-protocol-states"
276 "CLOSEREQ" : "#000000",
277 "CLOSING" : "#111111",
278 "IGNORE" : "#222222",
279 "INVALID" : "#333333",
282 "PARTOPEN" : "#666666",
283 "REQUEST" : "#777777",
284 "RESPOND" : "#888888",
285 "TIME_WAIT" : "#999999",
288 "CLOSED" : "#000000",
289 "COOKIE_ECHOED" : "#111111",
290 "COOKIE_WAIT" : "#222222",
291 "ESTABLISHED" : "#333333",
293 "SHUTDOWN_ACK_SENT" : "#555555",
294 "SHUTDOWN_RECD" : "#666666",
295 "SHUTDOWN_SENT" : "#777777",
299 "CLOSE_WAIT" : "#999999",
300 "ESTABLISHED" : "#000000",
301 "FIN_WAIT" : "#888888",
302 "LAST_ACK" : "#777777",
304 "SYN_RECV" : "#111111",
305 "SYN_SENT" : "#222222",
306 "SYN_SENT2" : "#333333",
307 "TIME_WAIT" : "#444444",
311 states_descriptions
= {
336 "SHUTDOWN_ACK_SENT" : 0,
355 def graph_title(self
):
356 _
= self
.locale
.translate
357 return _("Protocol States of all %s connections") % self
.protocol
.upper()
360 def graph_vertical_label(self
):
361 _
= self
.locale
.translate
362 return _("Number of open connections")
366 return self
.object.protocol
370 return sorted(ConntrackTable
._stateful
_layer
4_protocols
[self
.protocol
],
371 key
=lambda x
: self
.states_sortorder
[self
.protocol
].get(x
, 99))
375 _
= self
.locale
.translate
378 for state
in reversed(self
.states
):
380 "colour" : self
.states_colours
[self
.protocol
].get(state
, "#000000"),
381 "description" : self
.states_descriptions
[self
.protocol
].get(state
, state
),
382 "proto" : self
.protocol
,
385 "legend_min" : "%10s\: %%8.0lf" % _("Minimum"),
386 "legend_max" : "%10s\: %%8.0lf" % _("Maximum"),
387 "legend_avg" : "%10s\: %%8.0lf" % _("Average"),
388 "legend_cur" : "%10s\: %%8.0lf" % _("Current"),
391 args
+= self
.object.make_rrd_defs(state
) + [
392 "AREA:%(state)s%(colour)s:%(description)-15s:STACK" % i
,
393 "GPRINT:%(state)s_cur:%(legend_cur)s" % i
,
394 "GPRINT:%(state)s_avg:%(legend_avg)s" % i
,
395 "GPRINT:%(state)s_min:%(legend_min)s" % i
,
396 "GPRINT:%(state)s_max:%(legend_max)s\\n" % i
,
402 def rrd_graph_args(self
):
404 "--legend-direction=bottomup",
408 class ConntrackObject(base
.Object
):
411 def init(self
, conntrack_table
):
412 self
.conntrack_table
= conntrack_table
419 class ConntrackLayer3ProtocolsObject(ConntrackObject
):
420 protocols
= ConntrackTable
._layer
3_protocols
423 "DS:%s:GAUGE:0:U" % p
for p
in protocols
428 return "layer3-protocols"
433 for proto
in self
.protocols
:
434 r
= self
.conntrack_table
.layer3_protocols
.get(proto
, 0)
435 results
.append("%s" % r
)
440 class ConntrackLayer4ProtocolsObject(ConntrackObject
):
441 protocols
= ConntrackTable
._layer
4_protocols
444 "DS:%s:GAUGE:0:U" % p
for p
in protocols
449 return "layer4-protocols"
454 for proto
in self
.protocols
:
455 r
= self
.conntrack_table
.layer4_protocols
.get(proto
, 0)
456 results
.append("%s" % r
)
461 class ConntrackProtocolWithStatesObject(ConntrackObject
):
462 def init(self
, conntrack_table
, protocol
):
463 ConntrackObject
.init(self
, conntrack_table
)
464 self
.protocol
= protocol
467 return "<%s %s>" % (self
.__class
__.__name
__, self
.protocol
)
471 return ConntrackTable
._stateful
_layer
4_protocols
.get(self
.protocol
)
474 def rrd_schema(self
):
475 return ["DS:%s:GAUGE:0:U" % state
for state
in self
.states
]
477 def get_states(self
):
480 for state
in self
.states
:
481 r
= self
.conntrack_table
.protocol_states
[self
.protocol
].get(state
, 0)
482 results
.append("%s" % r
)
487 return self
.get_states()
490 class ConntrackPlugin(base
.Plugin
):
492 description
= "Conntrack Plugin"
495 ConntrackLayer3ProtocolsGraphTemplate
,
496 ConntrackLayer4ProtocolsGraphTemplate
,
497 ConntrackProtocolWithStatesGraphTemplate
,
502 ct
= self
.get_conntrack_table()
505 yield ConntrackLayer3ProtocolsObject(self
, ct
)
506 yield ConntrackLayer4ProtocolsObject(self
, ct
)
508 for protocol
in ConntrackTable
._stateful
_layer
4_protocols
:
509 yield ConntrackProtocolWithStatesObject(self
, ct
, protocol
)
511 def get_conntrack_table(self
):
512 if not os
.path
.exists(CONNTRACK_FILE
):
515 return ConntrackTable(CONNTRACK_FILE
)