Skip to content

Commit e0e929c

Browse files
committed
Use netflow submodule in our bot
1 parent 7f13228 commit e0e929c

File tree

6 files changed

+191
-4
lines changed

6 files changed

+191
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "python-netflow-v9-softflowd"]
2-
path = python-netflow-v9-softflowd
1+
[submodule "pynetflow"]
2+
path = pynetflow
33
url = https://github.com/bitkeks/python-netflow-v9-softflowd.git

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ url = "https://pypi.org/simple"
44
verify_ssl = true
55

66
[dev-packages]
7+
pylint = "*"
78

89
[packages]
910
requests = "*"
11+
python-dotenv = "*"
12+
ansicolors = "*"
1013

1114
[requires]
1215
python_version = "3.6"

Pipfile.lock

Lines changed: 113 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netflowbot.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import argparse
2+
import gzip
3+
import json
4+
import logging
5+
import os
6+
import sys
7+
import time
8+
9+
10+
from colors import color
11+
import dotenv
12+
13+
14+
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/pynetflow')
15+
from pynetflow.main import get_export_packets
16+
17+
18+
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(message)s',
19+
datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
20+
logging.addLevelName(logging.DEBUG, color("DBG", 7))
21+
logging.addLevelName(logging.INFO, "INF")
22+
logging.addLevelName(logging.WARNING, color('WRN', fg='red'))
23+
logging.addLevelName(logging.ERROR, color('ERR', bg='red'))
24+
log = logging.getLogger("{}.{}".format(__name__, "base"))
25+
26+
27+
def wait_for_grafolean(backend_url):
28+
while True:
29+
url = '{}/status/info'.format(backend_url)
30+
log.info("Checking Grafolean status...")
31+
try:
32+
r = requests.get(url)
33+
r.raise_for_status()
34+
status_info = r.json()
35+
if status_info['db_migration_needed'] == False and status_info['user_exists'] == True:
36+
log.info("Grafolean backend is ready.")
37+
return
38+
except:
39+
pass
40+
log.info("Grafolean backend not available / initialized yet, waiting.")
41+
time.sleep(10)
42+
43+
44+
if __name__ == "__main__":
45+
dotenv.load_dotenv()
46+
47+
# backend_url = os.environ.get('BACKEND_URL')
48+
# jobs_refresh_interval = int(os.environ.get('JOBS_REFRESH_INTERVAL', 120))
49+
50+
# if not backend_url:
51+
# raise Exception("Please specify BACKEND_URL and BOT_TOKEN / BOT_TOKEN_FROM_FILE env vars.")
52+
53+
# wait_for_grafolean(backend_url)
54+
55+
# bot_token = os.environ.get('BOT_TOKEN')
56+
# if not bot_token:
57+
# # bot token can also be specified via contents of a file:
58+
# bot_token_from_file = os.environ.get('BOT_TOKEN_FROM_FILE')
59+
# if bot_token_from_file:
60+
# with open(bot_token_from_file, 'rt') as f:
61+
# bot_token = f.read()
62+
63+
# if not bot_token:
64+
# raise Exception("Please specify BOT_TOKEN / BOT_TOKEN_FROM_FILE env var.")
65+
66+
port = int(os.environ.get('PORT', 2055))
67+
try:
68+
for ts, client, export in get_export_packets('0.0.0.0', port):
69+
print(len(export.flows))
70+
except KeyboardInterrupt:
71+
log.info("KeyboardInterrupt -> exit")
72+
pass

0 commit comments

Comments
 (0)