Refactored code to fit with PEP8 specifications and work with LFcliBase.

This commit is contained in:
Logan Lipke
2020-06-04 11:23:08 -07:00
parent a5a8dc370d
commit 29c18611da

View File

@@ -2,87 +2,73 @@
import os import os
import sys import sys
import time import time
sys.path.append('py-json') sys.path.append('py-json')
import json
import pprint
import datetime import datetime
from LANforge import LFRequest from LANforge import LFRequest
from LANforge import LFUtils from LANforge import LFUtils
import argparse import argparse
import re import re
import math
import string
import emailHelper import emailHelper
from LANforge.lfcli_base import LFCliBase
debugOn = False
sender = "lanforge@candelatech.com"
def jsonReq(mgrURL, reqURL, data, debug=False):
lf_r = LFRequest.LFRequest(mgrURL + reqURL)
lf_r.addPostData(data)
if debug:
json_response = lf_r.jsonPost(debug)
LFUtils.debug_printer.pprint(json_response)
sys.exit(1)
else:
lf_r.jsonPost(debug)
def execWrap(cmd): class StressTester(LFCliBase):
if os.system(cmd) != 0: def __init__(self, lfhost, lfport, _sender="lanforge@candelatech.com", _debug_on=False):
print("\nError with " + cmd + ",bye\n") self.sender = _sender
exit(1) super().__init__(lfhost, lfport, False)
def getJsonInfo(mgrURL, reqURL): def run(self):
lf_r = LFRequest.LFRequest(mgrURL + reqURL) parser = argparse.ArgumentParser(description="Create max stations for each radio")
json_response = lf_r.getAsJson(debugOn) parser.add_argument("--test_duration", type=str,
return json_response help="Full duration for the test to run. Should be specified by a number followed by a "
"character. d for days, h for hours, m for minutes, s for seconds")
parser.add_argument("--test_end_time", type=str,
help="Specify a time and date to end the test. Should be formatted as "
"year-month-date_hour:minute. Date should be specified in numbers and time "
"should be 24 "
"hour format. Ex: 2020-5-14_14:30")
parser.add_argument("--report_interval", type=str,
help="How often a report is made. Should be specified by a "
"number followed by a character. d for days, h for hours, "
"m for minutes, s for seconds")
parser.add_argument("--output_dir", type=str, help="Directory to ouptut to")
parser.add_argument("--output_prefix", type=str,
help="Name of the file. Timestamp and .html will be appended to the end")
parser.add_argument("--email", type=str, help="Email address of recipient")
args = None
parser = argparse.ArgumentParser(description="Create max stations for each radio") try:
parser.add_argument("--test_duration", type=str, help="Full duration for the test to run. Should be specified by a number followed by a character. d for days, h for hours, m for minutes, s for seconds")
parser.add_argument("--test_end_time", type=str, help="Specify a time and date to end the test. Should be formatted as year-month-date_hour:minute. Date should be specified in numbers and time should be 24 hour format. Ex: 2020-5-14_14:30")
parser.add_argument("--report_interval", type=str, help="How often a report is made. Should be specified by a number followed by a character. d for days, h for hours, m for minutes, s for seconds")
parser.add_argument("--output_dir", type=str, help="Directory to ouptut to")
parser.add_argument("--output_prefix", type=str, help="Name of the file. Timestamp and .html will be appended to the end")
parser.add_argument("--email", type=str, help="Email address of recipient")
args = None
try:
args = parser.parse_args() args = parser.parse_args()
if (args.test_duration is not None): if args.test_duration is not None:
pattern = re.compile("^(\d+)([dhms]$)") pattern = re.compile("^(\d+)([dhms]$)")
td = pattern.match(args.test_duration) td = pattern.match(args.test_duration)
if td != None: if td is not None:
durTime = int(td.group(1)) dur_time = int(td.group(1))
durMeasure = str(td.group(2)) dur_measure = str(td.group(2))
now = datetime.datetime.now() now = datetime.datetime.now()
if durMeasure == "d": if dur_measure == "d":
durationTime = datetime.timedelta(days = durTime) duration_time = datetime.timedelta(days=dur_time)
elif durMeasure == "h": elif dur_measure == "h":
durationTime = datetime.timedelta(hours = durTime) duration_time = datetime.timedelta(hours=dur_time)
elif durMeasure == "m": elif dur_measure == "m":
durationTime = datetime.timedelta(minutes = durTime) duration_time = datetime.timedelta(minutes=dur_time)
else: else:
durationTime = datetime.timedelta(seconds = durTime) duration_time = datetime.timedelta(seconds=dur_time)
else: else:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
elif (args.test_end_time is not None): elif args.test_end_time is not None:
now = datetime.datetime.now() now = datetime.datetime.now()
try: try:
endTime = datetime.datetime.strptime(args.test_end_time,"%Y-%m-%d_%H:%M") end_time = datetime.datetime.strptime(args.test_end_time, "%Y-%m-%d_%H:%M")
if endTime < now: if end_time < now:
raise ValueError
parser.print_help() parser.print_help()
parser.exit() raise ValueError
else: else:
curTime = datetime.datetime.now() cur_time = datetime.datetime.now()
durationTime = endTime - curTime duration_time = end_time - cur_time
except ValueError as exception: except ValueError as exception:
print(exception) print(exception)
@@ -93,22 +79,21 @@ try:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
if args.report_interval is not None:
if (args.report_interval is not None):
pattern = re.compile("^(\d+)([dhms])$") pattern = re.compile("^(\d+)([dhms])$")
ri = pattern.match(args.report_interval) ri = pattern.match(args.report_interval)
if ri != None: if ri is not None:
intTime = int(ri.group(1)) int_time = int(ri.group(1))
intMeasure = str(ri.group(2)) int_measure = str(ri.group(2))
if intMeasure == "d": if int_measure == "d":
intervalTime = datetime.timedelta(days = intTime) interval_time = datetime.timedelta(days=int_time)
elif intMeasure == "h": elif int_measure == "h":
intervalTime = datetime.timedelta(hours = intTime) interval_time = datetime.timedelta(hours=int_time)
elif intMeasure == "m": elif int_measure == "m":
intervalTime = datetime.timedelta(minutes = intTime) interval_time = datetime.timedelta(minutes=int_time)
else: else:
intervalTime = datetime.timedelta(seconds = intTime) interval_time = datetime.timedelta(seconds=int_time)
else: else:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
@@ -116,314 +101,318 @@ try:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
if (args.output_dir != None): if args.output_dir is not None:
outputDir = args.output_dir output_dir = args.output_dir
else: else:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
if (args.output_prefix != None): if args.output_prefix is not None:
outputPrefix = args.output_prefix output_prefix = args.output_prefix
else: else:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
if (args.email != None): if args.email is not None:
recipient = args.email recipient = args.email
else: else:
parser.print_help() parser.print_help()
parser.exit() parser.exit()
except Exception as e:
except Exception as e: parser.print_help()
logging.exception(e)
usage()
exit(2) exit(2)
super().check_connect()
stations = [] stations = []
radios = {"wiphy0":200, #max 200 radios = {"wiphy0": 200, # max 200
"wiphy1":200, #max 200 "wiphy1": 200, # max 200
"wiphy2":64, #max 64 "wiphy2": 64, # max 64
"wiphy3":200} #max 200 "wiphy3": 200} # max 200
#radioName:numStations # radioName:numStations
radio_ssid_map = {"wiphy0":"jedway-wpa2-x2048-4-1", radio_ssid_map = {"wiphy0": "jedway-wpa2-x2048-4-1",
"wiphy1":"jedway-wpa2-x2048-5-3", "wiphy1": "jedway-wpa2-x2048-5-3",
"wiphy2":"jedway-wpa2-x2048-5-1", "wiphy2": "jedway-wpa2-x2048-5-1",
"wiphy3":"jedway-wpa2-x2048-4-4"} "wiphy3": "jedway-wpa2-x2048-4-4"}
ssid_passphrase_map = {"jedway-wpa2-x2048-4-1":"jedway-wpa2-x2048-4-1", ssid_passphrase_map = {"jedway-wpa2-x2048-4-1": "jedway-wpa2-x2048-4-1",
"jedway-wpa2-x2048-5-3":"jedway-wpa2-x2048-5-3", "jedway-wpa2-x2048-5-3": "jedway-wpa2-x2048-5-3",
"jedway-wpa2-x2048-5-1":"jedway-wpa2-x2048-5-1", "jedway-wpa2-x2048-5-1": "jedway-wpa2-x2048-5-1",
"jedway-wpa2-x2048-4-4":"jedway-wpa2-x2048-4-4"} "jedway-wpa2-x2048-4-4": "jedway-wpa2-x2048-4-4"}
paddingNum = 1000 #uses all but the first number to create names for stations padding_num = 1000 # uses all but the first number to create names for stations
mgrURL = "http://localhost:8080/" # clean up old stations
print("Cleaning up old Stations")
#clean up old stations for radio, numStations in radios.items():
print("Cleaning up old Stations") for i in range(0, numStations):
sta_name = "sta" + radio[-1:] + str(padding_num + i)[1:]
for radio, numStations in radios.items(): if super().json_get("port/1/1/" + sta_name) is not None:
for i in range(0,numStations): req_url = "cli-json/rm_vlan"
staName = "sta" + radio[-1:] + str(paddingNum + i)[1:]
if getJsonInfo(mgrURL, "port/1/1/"+staName) != None:
reqURL = "cli-json/rm_vlan"
data = { data = {
"shelf":1, "shelf": 1,
"resource":1, "resource": 1,
"port":staName "port": sta_name
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
reqURL = "cli-json/rm_cx" req_url = "cli-json/rm_cx"
data = { data = {
"test_mgr":"default_tm", "test_mgr": "default_tm",
"cx_name":staName "cx_name": sta_name
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
reqURL = "cli-json/rm_endp" req_url = "cli-json/rm_endp"
data = { data = {
"endp_name":staName + "-A" "endp_name": sta_name + "-A"
} }
super().json_post(req_url, data)
jsonReq(mgrURL, reqURL, data) req_url = "cli-json/rm_endp"
reqURL = "cli-json/rm_endp"
data = { data = {
"endp_name":staName + "-B" "endp_name": sta_name + "-B"
} }
super().json_post(req_url, data)
#create new stations # create new stations
print("Creating Stations") print("Creating Stations")
reqURL = "cli-json/add_sta" req_url = "cli-json/add_sta"
for radio, numStations in radios.items(): for radio, numStations in radios.items():
for i in range(0,numStations): for i in range(0, numStations):
staName = "sta" + radio[-1:] + str(paddingNum + i)[1:] sta_name = "sta" + radio[-1:] + str(padding_num + i)[1:]
stations.append(staName) stations.append(sta_name)
data = { data = {
"shelf":1, "shelf": 1,
"resource":1, "resource": 1,
"radio":radio, "radio": radio,
"sta_name":staName, "sta_name": sta_name,
"ssid":radio_ssid_map[radio], "ssid": radio_ssid_map[radio],
"key":ssid_passphrase_map[radio_ssid_map[radio]], "key": ssid_passphrase_map[radio_ssid_map[radio]],
"mode":1, "mode": 1,
"mac":"xx:xx:xx:xx:*:xx", "mac": "xx:xx:xx:xx:*:xx",
"flags":0x400 "flags": 0x400
} }
#print("Creating station {}".format(staName)) # print("Creating station {}".format(sta_name))
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
time.sleep(0.5) time.sleep(0.5)
#LFUtils.portDhcpUpRequest(1, staName) # LFUtils.portDhcpUpRequest(1, sta_name)
time.sleep(10)
time.sleep(10) # check eth1 for ip
eth1_ip = super().json_get("port/1/1/eth1")
#check eth1 for ip if eth1_ip['interface']['ip'] == "0.0.0.0":
eth1IP = getJsonInfo(mgrURL, "port/1/1/eth1")
if eth1IP['interface']['ip'] == "0.0.0.0":
print("Switching eth1 to dhcp") print("Switching eth1 to dhcp")
LFUtils.portDownRequest(1,"eth1") LFUtils.portDownRequest(1, "eth1")
time.sleep(1) time.sleep(1)
reqURL = "cli-json/set_port" req_url = "cli-json/set_port"
data = { data = {
"shelf":1, "shelf": 1,
"resource":1, "resource": 1,
"port":"eth1", "port": "eth1",
"current_flags":0x80000000, "current_flags": 0x80000000,
"interest":0x4002 "interest": 0x4002
} }
jsonReq(mgrURL,reqURL,data) super().json_post(req_url, data)
#LFUtils.portDhcpUpRequest(1,"eth1") # LFUtils.portDhcpUpRequest(1,"eth1")
time.sleep(5) time.sleep(5)
LFUtils.portUpRequest(1,"eth1") LFUtils.portUpRequest(1, "eth1")
time.sleep(10)
time.sleep(10) # create cross connects
print("Creating cross connects")
for sta_name in stations:
cmd = (
"./lf_firemod.pl --action create_cx --cx_name " + sta_name + " --use_ports eth1," + sta_name + " --use_speeds 2600,2600 --endp_type udp > sst.log")
LFUtils.execWrap(cmd)
#create cross connects # set stations to dchp up
print("Creating cross connects") print("Turning on DHCP for stations")
for staName in stations: for sta_name in stations:
cmd = ("./lf_firemod.pl --action create_cx --cx_name " + staName + " --use_ports eth1," + staName + " --use_speeds 2600,2600 --endp_type udp > sst.log") # print("Setting {} flags".format(sta_name))
execWrap(cmd) req_url = "cli-json/set_port"
#set stations to dchp up
print("Turning on DHCP for stations")
for staName in stations:
#print("Setting {} flags".format(staName))
reqURL = "cli-json/set_port"
data = { data = {
"shelf":1, "shelf": 1,
"resource":1, "resource": 1,
"port":staName, "port": sta_name,
"current_flags":0x80000000, "current_flags": 0x80000000,
"interest":0x4002 "interest": 0x4002
} }
jsonReq(mgrURL,reqURL,data) super().json_post(req_url, data)
#LFUtils.portDhcpUpRequest(1,staName) # LFUtils.portDhcpUpRequest(1,sta_name)
time.sleep(15)
time.sleep(15) # start traffic through cxs
print("Starting CX Traffic")
for name in stations:
cmd = (
"./lf_firemod.pl --mgr localhost --quiet 0 --action do_cmd --cmd \"set_cx_state default_tm " + name + " RUNNING\" >> sst.log")
LFUtils.execWrap(cmd)
#start traffic through cxs # create weblog for monitoring stations
print("Starting CX Traffic") cur_time = datetime.datetime.now().strftime("%Y-%m-%d_%H%M")
for name in stations: web_log = output_dir + output_prefix + "{}.html".format(cur_time)
cmd = ("./lf_firemod.pl --mgr localhost --quiet 0 --action do_cmd --cmd \"set_cx_state default_tm " + name + " RUNNING\" >> sst.log")
execWrap(cmd)
try:
web_log_file = open(web_log, "w")
#create weblog for monitoring stations except IOError as err:
curTime = datetime.datetime.now().strftime("%Y-%m-%d_%H%M")
webLog = outputDir + outputPrefix + "{}.html".format(curTime)
try:
f = open(webLog,"w")
except IOError as err:
print(err) print(err)
print("Please ensure correct permissions have been assigned in target directory") print("Please ensure correct permissions have been assigned in target directory")
sys.exit() sys.exit()
top = """<html>
<head>
<title>Test report</title>
<style>
body, td, p, div, span { font-size: 8pt; }
h1, h2, h3 { text-align: center; font-family: "Century Gothic",Arial,Helvetica,sans;}
</style>
</head>
<body>
<h1>Long test on %s</h1>
<p2>Key</p2>
<p1 style="background-color:rgb(0,255,0);">All stations associated and with ip</p1>
<p1 style="background-color:rgb(255,200,0);">All stations associated and at least one without ip</p1>
<p1 style="background-color:rgb(255,150,150);">No stations associated and without ip</p1>
<table>
""" % datetime.date.today()
top = """<html> web_log_file.write(top)
<head> web_log_file.close()
<title>Test report</title>
<style>
body, td, p, div, span { font-size: 8pt; }
h1, h2, h3 { text-align: center; font-family: "Century Gothic",Arial,Helvetica,sans;}
</style>
</head>
<body>
<h1>Long test on %s</h1>
<p2>Key</p2>
<p1 style="background-color:rgb(0,255,0);">All stations associated and with ip</p1>
<p1 style="background-color:rgb(255,200,0);">All stations associated and at least one without ip</p1>
<p1 style="background-color:rgb(255,150,150);">No stations associated and without ip</p1>
<table>
""" % datetime.date.today()
web_log_file = open(web_log, "a")
web_log_file.write("<tr>\n")
f.write(top) for name in radios:
f.close() web_log_file.write("<th>{}</th>\n".format(name))
f = open(webLog, "a") web_log_file.write("</tr>\n")
f.write("<tr>\n")
for name in radios: cur_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
f.write("<th>{}</th>\n".format(name)) subject = "Station Test Begin Report Notification"
body = "Report begun at {}\n See {}".format(cur_time, web_log)
email = emailHelper.writeEmail(body)
emailHelper.sendEmail(email, self.sender, recipient, subject)
f.write("</tr>\n") print("Logging Info to {}".format(web_log))
cur_time = datetime.datetime.now()
end_time = cur_time + duration_time
curTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") while cur_time <= end_time:
subject = "Station Test Begin Report Notification" web_log_file.write("<tr>\n")
body = "Report begun at {}\n See {}".format(curTime, webLog)
email = emailHelper.writeEmail(body)
emailHelper.sendEmail(email, sender, recipient, subject)
print("Logging Info to {}".format(webLog))
curTime = datetime.datetime.now()
endTime = curTime + durationTime
while curTime <= (endTime):
f.write("<tr>\n")
for radio, numStations in radios.items(): for radio, numStations in radios.items():
withoutIP = 0 without_ip = 0
dissociated = 0 dissociated = 0
good = 0 good = 0
for i in range(0,numStations): for i in range(0, numStations):
staName = "sta" + radio[-1:] + str(paddingNum + i)[1:] sta_name = "sta" + radio[-1:] + str(padding_num + i)[1:]
staStatus = getJsonInfo(mgrURL, "port/1/1/" + staName) sta_status = super().json_get("port/1/1/" + sta_name)
#print(staName) # print(sta_name)
if staStatus['interface']['ip'] == "0.0.0.0": if sta_status['interface']['ip'] == "0.0.0.0":
withoutIP += 1 without_ip += 1
if staStatus['interface']['ap'] == None: if sta_status['interface']['ap'] is None:
dissociated += 1 dissociated += 1
else: else:
good += 1 good += 1
if withoutIP and not dissociated: if without_ip and not dissociated:
f.write("<td style=\"background-color:rgb(255,200,0);\">{}/{}</td>\n".format(good,numStations)) #without IP assigned web_log_file.write("<td style=\"background-color:rgb(255,200,0);\">{}/{}</td>\n".format(good,
numStations)) # without IP assigned
elif dissociated: elif dissociated:
f.write("<td style=\"background-color:rgb(255,150,150);\">{}/{}</td>\n".format(good,numStations)) #dissociated from AP web_log_file.write("<td style=\"background-color:rgb(255,150,150);\">{}/{}</td>\n".format(good,
numStations)) # dissociated from AP
else: else:
f.write("<td style=\"background-color:rgb(0,255,0);\">{}/{}</td>\n".format(good,numStations)) #with IP and associated web_log_file.write("<td style=\"background-color:rgb(0,255,0);\">{}/{}</td>\n".format(good,
numStations)) # with IP and associated
f.write("<td>{}</td>\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))) web_log_file.write("<td>{}</td>\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
f.write("</tr>\n") web_log_file.write("</tr>\n")
curTime = datetime.datetime.now() cur_time = datetime.datetime.now()
intTime = curTime + intervalTime int_time = cur_time + interval_time
while curTime <= intTime: while cur_time <= int_time:
#print(curTime, intTime) # print(cur_time, int_time)
time.sleep(1) time.sleep(1)
curTime = datetime.datetime.now() cur_time = datetime.datetime.now()
#sleep(1) # sleep(1)
curTime = datetime.datetime.now() cur_time = datetime.datetime.now()
f.write("</table></body></html>\n") web_log_file.write("</table></body></html>\n")
f.close() web_log_file.close()
cur_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
subject = "Station Test End Report Notification"
body = "Report finished at {} see {}".format(cur_time, web_log)
email = emailHelper.writeEmail(body)
emailHelper.sendEmail(email, self.sender, recipient, subject)
curTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") print("Stopping CX Traffic")
subject = "Station Test End Report Notification" for sta_name in stations:
body = "Report finished at {} see {}".format(curTime, webLog) cmd = (
email = emailHelper.writeEmail(body) "./lf_firemod.pl --mgr localhost --quiet 0 --action do_cmd --cmd \"set_cx_state default_tm " + sta_name + " STOPPED\" >> sst.log")
emailHelper.sendEmail(email, sender, recipient, subject) LFUtils.execWrap(cmd)
time.sleep(10)
print("Stopping CX Traffic") # remove all created stations and cross connects
for name in stations:
cmd = ("./lf_firemod.pl --mgr localhost --quiet 0 --action do_cmd --cmd \"set_cx_state default_tm " + name + " STOPPED\" >> sst.log")
execWrap(cmd)
time.sleep(10) print("Cleaning Up...")
for sta_name in stations:
#remove all created stations and cross connects req_url = "cli-json/rm_vlan"
print("Cleaning Up...")
for staName in stations:
reqURL = "cli-json/rm_vlan"
data = { data = {
"shelf":1, "shelf": 1,
"resource":1, "resource": 1,
"port":staName "port": sta_name
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
reqURL = "cli-json/rm_cx" req_url = "cli-json/rm_cx"
data = { data = {
"test_mgr":"default_tm", "test_mgr": "default_tm",
"cx_name":staName "cx_name": sta_name
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
reqURL = "cli-json/rm_endp" req_url = "cli-json/rm_endp"
data = { data = {
"endp_name":staName + "-A" "endp_name": sta_name + "-A"
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
reqURL = "cli-json/rm_endp" req_url = "cli-json/rm_endp"
data = { data = {
"endp_name":staName + "-B" "endp_name": sta_name + "-B"
} }
jsonReq(mgrURL, reqURL, data) super().json_post(req_url, data)
def main():
lfjson_host = "localhost"
lfjson_port = 8080
test = StressTester(lfjson_host, lfjson_port)
test.run()
if __name__ == "__main__":
main()