def SetupJobs(Jobs, mm_hostname): #****************************** # Do not change these... LANA_to_LANB = True LANB_to_LANA = False #****************************** BandSettings = mm2client.Bands() # A Band is for our 100ms/1megabit path # B band for the 1000ms/1megabit path A_Band = 1 B_Band = 2 #************************************************** # Here we set rate limitation value that we will use #************************************************** OneMegabit = 1000000 # Some might argue that one megabit is 1048576 (2**20) OneKilobit = 100000 # Some might argue that one kilobit is 1024000 #************************************************** # Here we set the two delay values. # Note: These are round trip times. #************************************************** LongRoundtrip = 1000 ShortRoundtrip = 100 # We will divide the round trip into two equal one-way parts. HalfLongRoundtrip = LongRoundtrip/2 HalfShortRoundtrip = ShortRoundtrip/2 #************************************************** # Set up our classification filters. # We will be using these as a two-way switch to # send selected packets first into the A band and then # into the B band and then back to the A band etc. # We will let any other forms of traffic flow via # Band 5 which will get no impairments. # There things that don't get picked up by the # filters below are almost certainly low traffic # things like OSPF. #************************************************** Filt_ToBandA = [setfilters.FiltSetting("arp", A_Band), setfilters.FiltSetting("ipv4", A_Band), setfilters.FiltSetting("ipv6", A_Band) ] Filt_ToBandB = [setfilters.FiltSetting("arp", B_Band), setfilters.FiltSetting("ipv4", B_Band), setfilters.FiltSetting("ipv6", B_Band) ] # Define A_Band to have 100ms round trip, 1megabit/second rate limit BandSettings.SetDelayAmount(A_Band, LANA_to_LANB, HalfShortRoundtrip) BandSettings.SetDelayAmount(A_Band, LANB_to_LANA, HalfShortRoundtrip) BandSettings.SetDelayReorder(A_Band, LANA_to_LANB, False) BandSettings.SetDelayReorder(A_Band, LANB_to_LANA, False) BandSettings.SetRateLimit(A_Band, LANA_to_LANB, OneMegabit) BandSettings.SetRateLimit(A_Band, LANB_to_LANA, OneMegabit) # Define B_Band with 1000ms round trip, 1kilobit/second rate limit BandSettings.SetDelayAmount(B_Band, LANA_to_LANB, HalfLongRoundtrip) BandSettings.SetDelayAmount(B_Band, LANB_to_LANA, HalfLongRoundtrip) BandSettings.SetDelayReorder(B_Band, LANA_to_LANB, False) BandSettings.SetDelayReorder(B_Band, LANB_to_LANA, False) BandSettings.SetRateLimit(B_Band, LANA_to_LANB, OneKilobit) BandSettings.SetRateLimit(B_Band, LANB_to_LANA, OneKilobit) #************************************************** # Create an initial job at start-time zero. # Here is where we establish our baseline. # Note that this baseline is a different thing than # the defaults, which are simply the absence of # filters and impairments. #************************************************** Jobs.AddRequest("Establish Baseline - 100ms RT delay, 1,000,000 bit/sec rate limit", mm_hostname, 0, BandSettings, Filt_ToBandA, Filt_ToBandA) #************************************************** # Set up the repeating schedule of jobs #************************************************** interval = 60 # Seconds per item items_per_group = 2 num_groups = 720 # Number of groups ==> 24 hours #Remember range() stops at < stop value not <= for secs in range(interval, items_per_group*num_groups*interval, items_per_group*interval): Jobs.AddRequest("Job %u: 1000ms RT delay, 100,000 bit/sec rate limit" % \ secs, mm_hostname, secs, None, Filt_ToBandB, Filt_ToBandB) nxtsecs = secs + interval Jobs.AddRequest("Job %u: 100ms RT delay, 1,000,000 bit/sec rate limit" % \ nxtsecs, mm_hostname, nxtsecs, None, Filt_ToBandA, Filt_ToBandA)
def main(): global UseSyslog global AllFilterNames global TeamName #****************************** # jobtick() - Where we run the job #****************************** def jobtick(): Jobs.RunNextRequest(datetime.datetime.now()) #****************************** # Deal with the arguments #****************************** parser = argparse.ArgumentParser(description="Start impairment pattern.") parser.add_argument("-s", "--skip", type=int, help="Skip ahead: start at N seconds.") parser.add_argument("-C", "--do_not_use_initial_defaults", action="store_true", dest="no_initial_defaults", help="Do not establish defaults before running scheduled tasks.") parser.add_argument("-D", "--initial_defaults_then_exit", action="store_true", dest="initial_defaults_only", help="Establish defaults and then exit, supersedes -C.") parser.add_argument("-l", "--loop", action="store_true", help="Loop/Cycle forever.") parser.add_argument("-S", "--syslog", action="store_true", help="Send reports to the system defined syslog server (level LOG_INFO).") parser.add_argument("-T", "--team", required=True, metavar="<team name>", help="Team name.") parser.add_argument("mm_hostname", metavar="<Mini Maxwell host name or IP address>", help='The host name or IP address of the Mini Maxwell/Maxwell G.') pargs = parser.parse_args() TeamName = pargs.team if pargs.syslog: UseSyslog = True mm_hostname = pargs.mm_hostname if AllFilterNames is None: AllFilterNames = setfilters.GetAllFilterNames(mm_hostname) #************************************************** # Do we just set the defaults and then exit? #************************************************** if pargs.initial_defaults_only: # Coerce default filter settings and impairments to a known (default) state ShowMessage("Coercing filters and impairments to default state, then exiting") SetMM(mm_hostname, mm2client.Bands(), [], [], AllFilterNames) ShowMessage("Stopping - Complete") sys.exit() #************************************************** # Set default values (unless suppressed) #************************************************** if pargs.no_initial_defaults: ShowMessage("Not setting initial defaults") else: # Coerce default filter settings and impairments to a known (default) state ShowMessage("Coercing filters and impairments to default state") SetMM(mm_hostname, mm2client.Bands(), [], [], AllFilterNames) skipsecs = pargs.skip #************************************************** # Main scheduling loop # Note: This loop does not repeat setting of the # default values. But it does repeat the initial # (time zero) job. # Also note that any skipping of initial time is # done only on the first iteration of this loop. # When looping the first job of the new iteration # will start immediately. #************************************************** while True: StartTime = datetime.datetime.now() Jobs = RunList(StartTime, skipsecs) skipsecs = 0 # We only do the skip on the first cycle SetupJobs(Jobs, mm_hostname) # Come up to date with initial and skipped tasks. ShowMessage("Running initial and skipped tasks to establish state.") while Jobs.RunNextRequest(StartTime): pass # Yes, we want to pass, all the work is done in the RunNextRequest() ShowMessage("Beginning scheduled events") # Give ourselves a lifetime. # Add a few seconds to let the last job complete. sleepsecs = (Jobs.LastRequestStartTime - datetime.datetime.now()).total_seconds() + 5 # Begin the timer system with a one second interval rt = RepeatedTimer(1, jobtick) try: ShowMessage("Start running for %u seconds." % sleepsecs) time.sleep(sleepsecs) finally: rt.stop() # better in a try/finally block to make sure the program ends! if not pargs.loop: break ShowMessage("Beginning again") ShowMessage("Stopping - Finished")