Skip to content

How to write an experiment and run it in Centinel

Abbas Razaghpanah edited this page Dec 19, 2016 · 2 revisions

How to write a Python Experiment

A Python experiment has to contain a class derived from the Experiment class, imported from centinel.experiment package. The name of the experiment is a class variable named "name". This variable should be the same as the name of the file. An experiment name is expected to be unique. Python experiments should be placed in ~/.centinel/experiments.

The input file (plain text, can be a simple list of URLs) should be placed in ~/.centinel/data. When you run centinel (without arguments), it will run all of the experiments according to scheduler.info. If an experiment is not mentioned in scheduler.info, it will be run every time Centinel is run.

###Example Python Test

import os
from centinel.experiment import Experiment

class PingExperiment(Experiment): # This experiment class is derived from Experiment
    name = "ping" # The name of the test. This is necessary

    def __init__(self, input_file):
        self.input_file  = input_file # The .txt file is passed in through the __init__ method
        self.results = [] # An array of dicts that contains results

    def run(self):
        for line in self.input_file: # For each url in the text file...
            self.host = line.strip()
            self.ping_test()

    def ping_test(self):
        result = { # creates dict to store results in
            "host" : self.host,
        }

        print "Running ping to " + self.host      
        response = os.system("ping -c 1 " + self.host + " >/dev/null 2>&1")
        
        if response == 0:
            result["success"] = 'true'
        else:
            result["success"] = 'false'

        self.results.append(result) # Add the dict to the array
                                    # Results are stored in a json format and written after the experiment