forked from bitkeks/python-netflow-v9-softflowd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
99 lines (79 loc) · 3.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
Example collector script for NetFlow v9.
This file belongs to https://github.com/cooox/python-netflow-v9-softflowd.
Copyright 2017, 2018 Dominik Pataky <[email protected]>
Licensed under MIT License. See LICENSE.
"""
import logging
import argparse
import sys
import socketserver
import time
import json
import os.path
logging.getLogger().setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logging.getLogger().addHandler(ch)
try:
from netflow.collector_v9 import ExportPacket
except ImportError:
logging.warn("Netflow v9 not installed as package! Running from directory.")
from src.netflow.collector_v9 import ExportPacket
parser = argparse.ArgumentParser(description='A sample netflow collector.')
parser.add_argument('--host', type=str, default='',
help='collector listening address')
parser.add_argument('--port', '-p', type=int, default=2055,
help='collector listener port')
parser.add_argument('--file', '-o', type=str, dest='output_file',
default="{}.json".format(int(time.time())),
help='collector export JSON file')
parser.add_argument('--debug', '-D', action='store_true',
help='Enable debug output')
class SoftflowUDPHandler(socketserver.BaseRequestHandler):
# We need to save the templates our NetFlow device
# send over time. Templates are not resended every
# time a flow is sent to the collector.
TEMPLATES = {}
@classmethod
def get_server(cls, host, port):
logging.info("Listening on interface {}:{}".format(host, port))
server = socketserver.UDPServer((host, port), cls)
return server
@classmethod
def set_output_file(cls, path):
cls.output_file = path
def handle(self):
if not os.path.exists(self.output_file):
with open(self.output_file, 'w') as fh:
fh.write(json.dumps({}))
with open(self.output_file, 'r') as fh:
existing_data = json.loads(fh.read())
data = self.request[0]
host = self.client_address[0]
s = "Received data from {}, length {}".format(host, len(data))
logging.debug(s)
export = ExportPacket(data, self.TEMPLATES)
self.TEMPLATES.update(export.templates)
s = "Processed ExportPacket with {} flows.".format(export.header.count)
logging.debug(s)
# Append new flows
existing_data[time.time()] = [flow.data for flow in export.flows]
with open(self.output_file, 'w') as fh:
fh.write(json.dumps(existing_data))
if __name__ == "__main__":
args = parser.parse_args()
SoftflowUDPHandler.set_output_file(args.output_file)
server = SoftflowUDPHandler.get_server(args.host, args.port)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
try:
logging.debug("Starting the NetFlow listener")
server.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
raise