Skip to content

Commit 565f829

Browse files
committed
Add verbose flag to analyzer
Adds a new flag, '-v' or '--verbose', to the analyzer.py script. It uses a new print method and also skips some parts of the script if not passed on the CLI.
1 parent adb02ea commit 565f829

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

analyzer.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
logger = logging.getLogger(__name__)
2727

2828

29+
def printv(message, *args, **kwargs):
30+
if args.verbose == True:
31+
print(message.format(*args, **kwargs))
32+
33+
2934
@functools.lru_cache(maxsize=None)
3035
def resolve_hostname(ip):
3136
return socket.getfqdn(ip)
@@ -160,6 +165,8 @@ def total_packets(self):
160165
help="The file to analyze (defaults to stdin if not provided)")
161166
parser.add_argument('-p', '--packets', dest='packets_threshold', type=int, default=10,
162167
help="Number of packets representing the lower bound in connections to be processed")
168+
parser.add_argument('-v', '--verbose', dest="verbose", action="store_true",
169+
help="Enable verbose output.")
163170
args = parser.parse_args()
164171

165172
# Using a file and using stdin differ in their further usage for gzip.open
@@ -198,6 +205,8 @@ def total_packets(self):
198205
skipped = 0
199206
skipped_threshold = args.packets_threshold
200207

208+
first_line = True # print header line before first line
209+
201210
for key in sorted(data):
202211
timestamp = datetime.fromtimestamp(float(key)).strftime("%Y-%m-%d %H:%M.%S")
203212
client = data[key]["client"]
@@ -236,13 +245,22 @@ def total_packets(self):
236245
skipped += 1
237246
continue
238247

239-
print("{timestamp}: {service:<14} | {size:8} | {duration:9} | {packets:5} | Between {src_host} ({src}) and {dest_host} ({dest})" \
248+
if first_line:
249+
print("{:19} | {:14} | {:8} | {:9} | {:7} | Involved hosts".format("Timestamp", "Service", "Size", "Duration", "Packets"))
250+
print("-" * 100)
251+
first_line = False
252+
253+
print("{timestamp} | {service:<14} | {size:8} | {duration:9} | {packets:7} | Between {src_host} ({src}) and {dest_host} ({dest})" \
240254
.format(timestamp=timestamp, service=con.service.upper(), src_host=con.hostnames.src, src=con.src,
241255
dest_host=con.hostnames.dest, dest=con.dest, size=con.human_size, duration=con.human_duration,
242256
packets=con.total_packets))
243257

244258
if skipped > 0:
245-
print(f"{skipped} connections skipped, because they had less than {skipped_threshold} packets.")
259+
print(f"{skipped} connections skipped, because they had less than {skipped_threshold} packets (this value can be set with the -p flag).")
260+
261+
if not args.verbose:
262+
# Exit here if no debugging session was wanted
263+
exit(0)
246264

247265
if len(pending) > 0:
248266
print(f"There are {len(pending)} first_switched entries left in the pending dict!")
@@ -260,4 +278,4 @@ def total_packets(self):
260278
print(first_switched, peer, flow["IPV6_DST_ADDR"], flow["IN_PKTS"])
261279

262280
if all_noise:
263-
print("They were all noise!")
281+
print("They were all noise!")

0 commit comments

Comments
 (0)