Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
905c56f1
Commit
905c56f1
authored
Mar 31, 2016
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(netmap/flowd) map interface indexes to names
parent
c752b771
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
0 deletions
+32
-0
parse.py
src/opnsense/scripts/netflow/lib/parse.py
+32
-0
No files found.
src/opnsense/scripts/netflow/lib/parse.py
View file @
905c56f1
...
@@ -28,6 +28,9 @@
...
@@ -28,6 +28,9 @@
"""
"""
import
flowd
import
flowd
import
glob
import
glob
import
tempfile
import
subprocess
import
os
# define field
# define field
PARSE_FLOW_FIELDS
=
[
PARSE_FLOW_FIELDS
=
[
...
@@ -48,12 +51,38 @@ PARSE_FLOW_FIELDS = [
...
@@ -48,12 +51,38 @@ PARSE_FLOW_FIELDS = [
# location of flowd logfiles to use
# location of flowd logfiles to use
FLOWD_LOG_FILES
=
'/var/log/flowd.log*'
FLOWD_LOG_FILES
=
'/var/log/flowd.log*'
class
Interfaces
(
object
):
""" mapper for local interface index to interface name (1 -> em0 for example)
"""
def
__init__
(
self
):
""" construct local interface mapping
"""
self
.
_ifIndex
=
dict
()
with
tempfile
.
NamedTemporaryFile
()
as
output_stream
:
subprocess
.
call
([
'/usr/bin/netstat'
,
'-i'
,
'-n'
],
stdout
=
output_stream
,
stderr
=
open
(
os
.
devnull
,
'wb'
))
output_stream
.
seek
(
0
)
for
line
in
output_stream
.
read
()
.
split
(
'
\n
'
):
parts
=
line
.
split
()
if
len
(
parts
)
>
2
and
parts
[
2
]
.
find
(
'<Link#'
)
>
-
1
:
self
.
_ifIndex
[
parts
[
2
]
.
split
(
'#'
)[
1
]
.
split
(
'>'
)[
0
]]
=
parts
[
0
]
def
if_device
(
self
,
ifIndex
):
""" convert index to device (if found)
"""
if
str
(
ifIndex
)
in
self
.
_ifIndex
:
# found, return interface name
return
self
.
_ifIndex
[
str
(
ifIndex
)]
else
:
# not found, return index
return
str
(
ifIndex
)
def
parse_flow
(
recv_stamp
):
def
parse_flow
(
recv_stamp
):
""" parse flowd logs and yield records (dict type)
""" parse flowd logs and yield records (dict type)
:param recv_stamp: last receive timestamp (recv)
:param recv_stamp: last receive timestamp (recv)
:return: iterator flow details
:return: iterator flow details
"""
"""
interfaces
=
Interfaces
()
parse_done
=
False
parse_done
=
False
for
filename
in
sorted
(
glob
.
glob
(
FLOWD_LOG_FILES
)):
for
filename
in
sorted
(
glob
.
glob
(
FLOWD_LOG_FILES
)):
if
parse_done
:
if
parse_done
:
...
@@ -80,6 +109,9 @@ def parse_flow(recv_stamp):
...
@@ -80,6 +109,9 @@ def parse_flow(recv_stamp):
flow_record
[
flow_field
[
'target'
]]
=
getattr
(
flow
,
flow_field
[
'target'
])
flow_record
[
flow_field
[
'target'
]]
=
getattr
(
flow
,
flow_field
[
'target'
])
else
:
else
:
flow_record
[
flow_field
[
'target'
]]
=
None
flow_record
[
flow_field
[
'target'
]]
=
None
# map interface indexes to actual interface names
flow_record
[
'if_in'
]
=
interfaces
.
if_device
(
flow_record
[
'if_ndx_in'
])
flow_record
[
'if_out'
]
=
interfaces
.
if_device
(
flow_record
[
'if_ndx_out'
])
yield
flow_record
yield
flow_record
# send None to mark last record
# send None to mark last record
yield
None
yield
None
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment