Skip to content
Open
33 changes: 21 additions & 12 deletions centinel/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def run(self, data_dir=None):
logging.info("Finished running experiments. "
"Look in %s for results." % (self.config['dirs']['results_dir']))

def run_exp(self, name, exp_config=None, schedule_name=None):
def run_exp(self, name, exp_config=None, schedule_name=None, throw=False):
if name[-3:] == ".py":
name = name[:-3]
if name not in self.experiments:
Expand Down Expand Up @@ -336,14 +336,20 @@ def run_exp(self, name, exp_config=None, schedule_name=None):
except Exception as exp:
logging.exception("Failed to run tcpdump: %s" % (exp,))

try:
# run the experiment
exp.run()
except Exception as exception:
logging.exception("Error running %s: %s" % (name, exception))
results["runtime_exception"] = str(exception)
except KeyboardInterrupt:
logging.warn("Keyboard interrupt received, stopping experiment...")
if throw:
try:
exp.run()
except KeyboardInterrupt:
logging.warn("Keyboard interrupt received, stopping experiment...")
else:
try:
# run the experiment
exp.run()
except Exception as exception:
logging.exception("Error running %s: %s" % (name, exception))
results["runtime_exception"] = str(exception)
except KeyboardInterrupt:
logging.warn("Keyboard interrupt received, stopping experiment...")


# save any external results that the experiment has generated
Expand Down Expand Up @@ -373,6 +379,7 @@ def run_exp(self, name, exp_config=None, schedule_name=None):
"%s" % exp)
logging.debug("Finished writing external files for %s" % name)

pcap_file_path = None
if tcpdump_started:
logging.info("Waiting for tcpdump to process packets...")
# 5 seconds should be enough. this hasn't been tested on
Expand Down Expand Up @@ -465,15 +472,16 @@ def run_exp(self, name, exp_config=None, schedule_name=None):
logging.info("%s took %s to finish." % (name, time_taken))

logging.debug("Saving %s results to file" % name)
result_file_path = None
try:
# pretty printing results will increase file size, but files are
# compressed before sending.
result_file_path = self\
.get_result_file(name, start_time.strftime("%Y-%m-%dT%H%M%S.%f"))
result_file = bz2.BZ2File(result_file_path, "w")
json.dump(results, result_file, indent=2, separators=(',', ': '),
# ignore encoding errors, these will be dealt with on the server
ensure_ascii=False)
# ignore encoding errors, these will be dealt with on the server
# Use dumps instead of dump because it handles unicode objects better
result_file.write(json.dumps(results, ensure_ascii=False).encode('utf-8'))
result_file.close()

# free up memory by deleting results from memory
Expand All @@ -483,6 +491,7 @@ def run_exp(self, name, exp_config=None, schedule_name=None):
logging.exception("Error saving results for "
"%s to file: %s" % (name, exception))
logging.debug("Done saving %s results to file" % name)
return (result_file_path, pcap_file_path)

def consolidate_results(self):
# bundle and compress result files
Expand Down
1 change: 1 addition & 0 deletions centinel/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, command, output_callback, timeout=10):
self.exception = None
self.error = False
self.notifications = ""
self.kill_switch = lambda: None

self.thread = threading.Thread(target=self._invoke_cmd)
self.thread.setDaemon(1)
Expand Down
Loading