Skip to content

Commit

Permalink
tracer: add logging about tracing events and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Mar 21, 2024
1 parent 2ec5380 commit 59d2814
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sipssert/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, scenario_file, controller, test_set, set_logs_dir, set_defaul
nets = self.networks if self.networks else []
if self.network:
nets.append(self.network)
self.tracer = tracer.Tracer(self.scen_logs_dir, "capture", nets)
self.tracer = tracer.Tracer(self.scen_logs_dir, "capture", nets, self.name)
self.timeout = self.config.get("timeout", 0)
container_prefix = f"{test_set.name}/{self.name}"
self.tasks = tasks_list.TasksList("tasks", self.dirname, self.scen_logs_dir,
Expand Down
33 changes: 28 additions & 5 deletions sipssert/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,60 @@
import os
import time
import subprocess
from sipssert import logger

class Tracer():

"""Class that implements the network capturing"""

def __init__(self, directory, name, net=[]):
def __init__(self, directory, filename, net=[], name=None):
# TODO: use tshark instead of tcpdump
if len(net) == 0 or (len(net) == 1 and net == "host") or len(net) > 1:
self.interface = "any"
else:
self.interface = net[0]
self.capture_file = os.path.join(directory, f"{name}.pcap")
self.name = name if name else filename
self.capture_file = os.path.join(directory, f"{filename}.pcap")
self.process = None

def status(self):
if not self.process:
return None, None
rc = self.process.returncode
if rc and rc != 0:
ret = self.process.communicate()[1]
if ret:
ret = ret.decode('utf-8')
else:
ret = None
return rc, ret

def stop(self):
"""Stops started tcpdump"""
if not self.process:
return
self.process.terminate()
self.process.wait()
rc, err = self.status()
self.process = None
logger.slog.debug(f"stopped tracer for {self.name}")
if rc and rc != 0:
logger.slog.error(f"tracer {self.name} failed with ({rc}):\n{err}")

def start(self):
"""Starts a tcpdump for a scenario"""
self.process = subprocess.Popen(['tcpdump',
'-i', self.interface,
'-w', self.capture_file],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
# wait for proc to start
time.sleep(0.5)
stderr=subprocess.PIPE)
rc, err = self.status()
if rc and rc != 0:
logger.slog.error(f"could not start tracer {self.name} ({rc}):\n{err}")
# wait for proc to start
else:
logger.slog.debug(f"started tracer for {self.name}")
time.sleep(0.5)


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

0 comments on commit 59d2814

Please sign in to comment.