-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multiple exporters and mulitple observation domains
- Loading branch information
Showing
8 changed files
with
80 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,53 @@ | ||
import struct | ||
from .field import Field | ||
from .flow import Flow, flowset_templates | ||
from .template import DataTemplate | ||
|
||
|
||
def on_flowset_template(line: bytes): | ||
def on_flowset_template( | ||
line: bytes, | ||
source: str, | ||
source_id: int, | ||
source_uptime: int, | ||
): | ||
pos = 0 | ||
while pos < len(line): | ||
template_id, field_count = struct.unpack('>HH', line[pos:pos+4]) | ||
# rfc 3954 5.1 | ||
# NetFlow Collectors SHOULD use the combination of the source IP | ||
# address and the Source ID field to separate different export | ||
# streams originating from the same Exporter. | ||
key = source, source_id, template_id | ||
template = flowset_templates.get(key) | ||
if template and template.source_uptime < source_uptime: | ||
pos += 4 + field_count * 4 | ||
continue | ||
|
||
fields = [ | ||
Field(*struct.unpack('>HH', line[i:i+4])) | ||
for i in range(pos + 4, pos + 4 + field_count * 4, 4) | ||
] | ||
|
||
pos += 4 + field_count * 4 | ||
flowset_templates[template_id] = ( | ||
flowset_templates[key] = DataTemplate( | ||
'>' + ''.join(f._fmt for f in fields), | ||
sum(f.length for f in fields), | ||
fields, | ||
[f.id for f in fields], | ||
source_uptime, | ||
) | ||
|
||
|
||
def on_flowset(line: bytes, flowset_id: int): | ||
if flowset_id in flowset_templates: | ||
fmt, length, _, _ = flowset_templates[flowset_id] | ||
for i in range(0, len(line) - 4, length): | ||
values = struct.unpack(fmt, line[i:i+length]) | ||
yield Flow(flowset_id, values) | ||
def on_flowset( | ||
line: bytes, | ||
flowset_id: int, | ||
source: str, | ||
source_id: int, | ||
): | ||
key = source, source_id, flowset_id | ||
template = flowset_templates.get(key) | ||
if template: | ||
fmt = template._fmt | ||
pad = len(line) % template.length | ||
for values in struct.iter_unpack(fmt, line[:-pad] if pad else line): | ||
yield Flow(template, values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import List | ||
from .field import Field | ||
|
||
|
||
class DataTemplate: | ||
__slots__ = ( | ||
'_fmt', | ||
'length', | ||
'fields', | ||
'index', | ||
'source_uptime', | ||
) | ||
|
||
def __init__(self, fmt: str, length: int, fields: List[Field], | ||
index: List[int], source_uptime: int): | ||
self._fmt = fmt | ||
self.fields = fields | ||
self.index = index | ||
self.length = length | ||
self.source_uptime = source_uptime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters