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 ###############################################################################
26 CONNTRACK_FILE
= "/proc/net/nf_conntrack"
28 class ConntrackTable(object):
46 _stateful_layer4_protocols
= {
83 def __init__(self
, filename
):
84 with
open(filename
) as f
:
85 self
.layer3_protocols
= {}
86 for proto
in self
._layer
3_protocols
:
87 self
.layer3_protocols
[proto
] = 0
89 self
.layer4_protocols
= {}
90 for proto
in self
._layer
4_protocols
:
91 self
.layer4_protocols
[proto
] = 0
93 self
.protocol_states
= {}
94 for proto
, states
in self
._stateful
_layer
4_protocols
.items():
95 self
.protocol_states
[proto
] = dict((state
, 0) for state
in states
)
97 for line
in f
.readlines():
101 layer3_protocol
= line
[0]
104 self
.layer3_protocols
[layer3_protocol
] += 1
106 self
.layer3_protocols
["other"] += 1
109 layer4_protocol
= line
[2]
112 self
.layer4_protocols
[layer4_protocol
] += 1
114 self
.layer4_protocols
["other"] += 1
115 layer4_protocol
= "other"
117 # Count connection states
118 if self
.protocol_states
.has_key(layer4_protocol
):
122 self
.protocol_states
[layer4_protocol
][state
] += 1
127 class ConntrackObject(base
.Object
):
130 def init(self
, conntrack_table
):
131 self
.conntrack_table
= conntrack_table
138 class ConntrackLayer3ProtocolsObject(ConntrackObject
):
139 protocols
= ConntrackTable
._layer
3_protocols
142 "DS:%s:GAUGE:0:U" % p
for p
in protocols
147 return "layer3-protocols"
152 for proto
in self
.protocols
:
153 r
= self
.conntrack_table
.layer3_protocols
.get(proto
, 0)
154 results
.append("%s" % r
)
156 return ":".join(results
)
159 class ConntrackLayer4ProtocolsObject(ConntrackObject
):
160 protocols
= ConntrackTable
._layer
4_protocols
163 "DS:%s:GAUGE:0:U" % p
for p
in protocols
168 return "layer4-protocols"
173 for proto
in self
.protocols
:
174 r
= self
.conntrack_table
.layer4_protocols
.get(proto
, 0)
175 results
.append("%s" % r
)
177 return ":".join(results
)
180 class ConntrackProtocolWithStatesObject(ConntrackObject
):
181 def init(self
, conntrack_table
, protocol
):
182 ConntrackObject
.init(self
, conntrack_table
)
183 self
.protocol
= protocol
187 return ConntrackTable
._stateful
_layer
4_protocols
.get(self
.protocol
)
190 def rrd_schema(self
):
191 return ["DS:%s:GAUGE:0:U" % state
for state
in self
.states
]
193 def get_states(self
):
196 for state
in self
.states
:
197 r
= self
.conntrack_table
.protocol_states
[self
.protocol
].get(state
, 0)
198 results
.append("%s" % r
)
203 return ":".join(self
.get_states())
206 class ConntrackPlugin(base
.Plugin
):
208 description
= "Conntrack Plugin"
214 ct
= self
.get_conntrack_table()
217 yield ConntrackLayer3ProtocolsObject(self
, ct
)
218 yield ConntrackLayer4ProtocolsObject(self
, ct
)
220 for protocol
in ConntrackTable
._stateful
_layer
4_protocols
:
221 yield ConntrackProtocolWithStatesObject(self
, ct
, protocol
)
223 def get_conntrack_table(self
):
224 if not os
.path
.exists(CONNTRACK_FILE
):
227 return ConntrackTable(CONNTRACK_FILE
)