-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-flows.py
executable file
·86 lines (70 loc) · 2.69 KB
/
tag-flows.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
#!/usr/bin/env python3
#
# Copyright (c) 2014, Richard Mortier <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
import sys, bisect
TAGS = {}
NAMES = {}
def err(s):
print(s, flush=True, file=sys.stderr)
def fopen(f):
if f == "-": return sys.stdin
else:
return open(f)
if __name__ == '__main__':
[tags, names, flows] = sys.argv[1:4]
with fopen(tags) as f:
for line in f:
_, name, tags = line.strip().split("|")
tags = tags.split(",")
name = name.strip()
if len(name) > 0:
TAGS[name] = set(
tag.strip() for tag in tags if len(tag.strip()) > 0)
with fopen(names) as f:
for line in f:
line = line.strip()
if line.startswith("#"):
filename = line.split()[1]
err("# %s" % filename)
continue
ni, ts, ns, qip, qname, aname, aip = line.split(",")
if qip not in NAMES: NAMES[qip] = {}
if aip not in NAMES[qip]: NAMES[qip][aip] = set()
NAMES[qip][aip].add(qname)
NAMES[qip][aip].add(aname)
with fopen(flows) as f:
for line in f:
try:
fields = line.strip("\n").split("\t")
[ i, start,end,duration,
src,dst, pkts,bytes, flags, urls ] = fields
except:
err(line, flush=True)
raise
if src.startswith("10.8"):
qip = ".".join(src.split(".")[:4])
aip = ".".join(dst.split(".")[:4])
else:
aip = ".".join(src.split(".")[:4])
qip = ".".join(dst.split(".")[:4])
names = set()
tags = set()
if qip in NAMES:
if aip in NAMES[qip]:
names = NAMES[qip][aip]
for n in names:
if n in TAGS: tags |= TAGS[n]
print("%s\t%s\t%s" % (
"\t".join(fields), ";".join(names), ";".join(tags)))