def deleteslice(): url = "http://%s/stats/disableinsert" %\ settings.properties['sdncontroller'] r_ = requests.get(url=url) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Disable insert body: %s" % (r_.json())) url = "http://%s/stats/flowentry/delete" %\ settings.properties['sdncontroller'] logger.debug("Flows to be deleted: %s" % delflows.flows) for f in delflows.flows: logger.info("post %s: %s" % ( url, f, )) r_ = requests.post(url=url, headers={'content-type': 'application/json'}, data=json.dumps(f)) if r_.status_code != requests.codes.ok: logger.error("Error in the response: %s" % r_) bottle.abort(500, r_) return bottle.HTTPResponse(body=[], status=201)
def verify_settings(): logger.debug("Agent settings: %s" % (settings.properties)) dir_ = samples_dir(settings.properties['samples']) if not os.path.isdir(dir_): return (False, "Not a valid directory: %s" % dir_) return (True, None)
def verify_settings(): logger.debug("Collector settings: %s" % (settings.properties)) for k, v in settings.properties['peers'].items(): for x in v: if len(x.split(':')) != 2: return (False, "Not a valid address form: %s" % x) return (True, None)
def main(argv=None): if not argv: argv = sys.argv try: parser_ = argparse.ArgumentParser( description='Data Preprocessing on Demand COLLECTOR-server', epilog='Report bugs to ' + '<*****@*****.**>', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser_.add_argument('-a', '--address', default='0.0.0.0', help='server address') parser_.add_argument('-p', '--port', default=9000, help='server port') parser_.add_argument('-r', '--reload', default=False, action="store_true", help='reload the web-server') args_ = parser_.parse_args() except Exception as e: logger.error("Got an Exception parsing flags/options: %s" % (e, )) return False logger.debug("%s" % (args_, )) # verify the provided settings ret, error = verify_settings() if not ret: logger.error("Settings failure: %s" % (error, )) return False try: RTTMANAGER.start() logger.info("Starting COLLECTOR-server main cycle on %s:%s" % ( args_.address, args_.port, )) bottle.run(host=args_.address, port=args_.port, debug=True, server='paste', reloader=args_.reload) except KeyboardInterrupt: logger.warning("User interruption!") except Exception as ex: logger.error("Exception: %s" % (ex, )) return False RTTMANAGER.stop() logger.warning("Bye Bye...") return True
def verify_settings(): logger.debug("Collector settings: %s" % (settings.properties)) for k, v in settings.properties['peers'].items(): for x in v: if len(x.split(':')) != 2: return (False, "Not a valid address form: %s" % x) return (True, None)
def createslice(): url = "http://%s/stats/enableinsert" %\ settings.properties['sdncontroller'] r_ = requests.get(url=url) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Enable insert body: %s" % (r_.json())) return bottle.HTTPResponse(body=[], status=201)
def createslice(): url = "http://%s/stats/enableinsert" %\ settings.properties['sdncontroller'] r_ = requests.get(url=url) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Enable insert body: %s" % (r_.json())) return bottle.HTTPResponse(body=[], status=201)
def stop(self): timeout = 5 # do not block for a long time! logger.debug("Stopping the rtt-manager service") self.__close() try: if self.is_alive(): logger.debug("Joining %dsecs" % (timeout,)) self.join(timeout=timeout) logger.info("rtt-manager service successfully stopped!") except Exception as e: logger.error("RunTime error: %s" % (str(e),))
def main(argv=None): if not argv: argv = sys.argv try: parser_ = argparse.ArgumentParser( description='Data Preprocessing on Demand COLLECTOR-server', epilog='Report bugs to ' + '<*****@*****.**>', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser_.add_argument( '-a', '--address', default='0.0.0.0', help='server address') parser_.add_argument( '-p', '--port', default=9000, help='server port') parser_.add_argument( '-r', '--reload', default=False, action="store_true", help='reload the web-server') args_ = parser_.parse_args() except Exception as e: logger.error("Got an Exception parsing flags/options: %s" % (e,)) return False logger.debug("%s" % (args_,)) # verify the provided settings ret, error = verify_settings() if not ret: logger.error("Settings failure: %s" % (error,)) return False try: RTTMANAGER.start() logger.info("Starting COLLECTOR-server main cycle on %s:%s" % (args_.address, args_.port,)) bottle.run(host=args_.address, port=args_.port, debug=True, server='paste', reloader=args_.reload) except KeyboardInterrupt: logger.warning("User interruption!") except Exception as ex: logger.error("Exception: %s" % (ex,)) return False RTTMANAGER.stop() logger.warning("Bye Bye...") return True
def localize_file(name): logger.debug("localize_file: %s" % (name)) ret = [] for k, v in settings.properties['peers'].items(): for x in v: r_ = requests.get(url="http://%s/smosfile/%s" % (x, name)) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Response %s body: %s" % (x, r_.json())) if r_.json().get('status') == "ok": ret.append(x) return ret
def localize_file(name): logger.debug("localize_file: %s" % (name)) ret = [] for k, v in settings.properties['peers'].items(): for x in v: r_ = requests.get(url="http://%s/smosfile/%s" % (x, name)) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Response %s body: %s" % (x, r_.json())) if r_.json().get('status') == "ok": ret.append(x) return ret
def rtt(): mon_table = RTTMANAGER.get_table() logger.debug("MonitorTable: %s" % (mon_table)) body = {"text": "", "color": "red"} tmp = [] for entry in mon_table: body["text"] += "%s: <b>%s</b> ms<br>" % (entry['name'], entry['rtt']) if entry['rtt'] != 0: tmp.append(1) if len(tmp) == len(mon_table): body["color"] = "green" elif len(tmp) > 0: body["color"] = "yellow" return bottle.HTTPResponse(body=body, status=200)
def rtt(): mon_table = RTTMANAGER.get_table() logger.debug("MonitorTable: %s" % (mon_table)) body = {"text": "", "color": "red"} tmp = [] for entry in mon_table: body["text"] += "%s: <b>%s</b> ms<br>" % (entry['name'], entry['rtt']) if entry['rtt'] != 0: tmp.append(1) if len(tmp) == len(mon_table): body["color"] = "green" elif len(tmp) > 0: body["color"] = "yellow" return bottle.HTTPResponse(body=body, status=200)
def do_action(self, entry): value = 0 try: logger.debug("Entry: %s" % (entry)) resp = requests.get(url="http://%s:%s/ping/%s" % (entry['srcaddr'], entry['srcport'], entry['dstaddr'])) if resp.status_code != requests.codes.ok: logger.error(resp.text) else: logger.debug("Response body: %s" % (resp.json())) value = resp.json().get('rtt') except Exception as e: logger.error("Action error: %s" % (str(e),)) finally: with self.__mutex: entry['rtt'] = value
def smosfiles(): logger.info("get-smosfiles") body = {'smosfiles': []} for k, v in settings.properties['peers'].items(): logger.debug("Testbed: %s" % (k, )) for x in v: r_ = requests.get(url="http://%s/smosfiles" % x) logger.debug("Response body: %s" % r_.json()) test = { 'testbed': k, 'address': x.split(':')[0], 'port': x.split(':')[1] } for data in r_.json()['smosfiles']: test['id'] = data['id'] test['data'] = data['data'] body['smosfiles'].append(test) return bottle.HTTPResponse(body=pretty_dumps(body), status=200)
def smosfiles(): logger.info("get-smosfiles") body = {'smosfiles': []} for k, v in settings.properties['peers'].items(): logger.debug("Testbed: %s" % (k,)) for x in v: r_ = requests.get(url="http://%s/smosfiles" % x) logger.debug("Response body: %s" % r_.json()) test = { 'testbed': k, 'address': x.split(':')[0], 'port': x.split(':')[1] } for data in r_.json()['smosfiles']: test['id'] = data['id'] test['data'] = data['data'] body['smosfiles'].append(test) return bottle.HTTPResponse(body=pretty_dumps(body), status=200)
def ping(host): logger.info("get-ping: %s" % (host)) body = {'rtt': 0} try: cmd = "ping -c 1 -W 3 " + host ret = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) ret = ret.split('\n')[-3:] xstats, tstats = ret[0].split(","), ret[1].split("=")[1].split("/") loss, pmin, avg, pmax =\ float(xstats[2].split("%")[0]), float(tstats[0]),\ float(tstats[1]), float(tstats[2]) logger.debug("packet-loss=%s, min=%s, avg=%s, max=%s" % (loss, pmin, avg, pmax)) body['rtt'] = avg except Exception as e: bottle.abort(500, str(e)) return bottle.HTTPResponse(body=pretty_dumps(body), status=200)
def deleteslice(): url = "http://%s/stats/disableinsert" %\ settings.properties['sdncontroller'] r_ = requests.get(url=url) if r_.status_code != requests.codes.ok: logger.error(r_.text) else: logger.debug("Disable insert body: %s" % (r_.json())) url = "http://%s/stats/flowentry/delete" %\ settings.properties['sdncontroller'] logger.debug("Flows to be deleted: %s" % delflows.flows) for f in delflows.flows: logger.info("post %s: %s" % (url, f,)) r_ = requests.post( url=url, headers={'content-type': 'application/json'}, data=json.dumps(f)) if r_.status_code != requests.codes.ok: logger.error("Error in the response: %s" % r_) bottle.abort(500, r_) return bottle.HTTPResponse(body=[], status=201)
def submit(): if 'application/json' not in bottle.request.headers['content-type']: bottle.abort(500, "Application Type must be json!") logger.info("post-submit: %s" % (bottle.request.json)) owners = localize_file(bottle.request.json.get('file')) logger.debug("Owners: %s" % (owners, )) # path is a list of tuple, in which each element is a # source/destination value path = path_min_rtt(owners, bottle.request.json.get('host')) logger.debug("Path with minimum RTT: %s" % (path)) body = [] for p in path: # call the agent to make the copy url = "http://%s/scp" % p[0] payload = { "data": [bottle.request.json.get('file')], "username": bottle.request.json.get('username'), "password": bottle.request.json.get('password'), "host": p[1], "destination": bottle.request.json.get('destination') } logger.debug("post %s: %s" % ( url, payload, )) r_ = requests.post(url=url, headers={'content-type': 'application/json'}, data=json.dumps(payload)) if r_.status_code != requests.codes.ok: logger.error(r_.text) bottle.abort(500, r_.text) else: logger.debug("Response %s body: %s" % (x, r_.json())) test = { "source": p[0], "destination": p[1], "value": r_.json().get('scp') } body.append(test) return bottle.HTTPResponse(body=pretty_dumps(body), status=201)
def submit(): if 'application/json' not in bottle.request.headers['content-type']: bottle.abort(500, "Application Type must be json!") logger.info("post-submit: %s" % (bottle.request.json)) owners = localize_file(bottle.request.json.get('file')) logger.debug("Owners: %s" % (owners,)) # path is a list of tuple, in which each element is a # source/destination value path = path_min_rtt(owners, bottle.request.json.get('host')) logger.debug("Path with minimum RTT: %s" % (path)) body = [] for p in path: # call the agent to make the copy url = "http://%s/scp" % p[0] payload = { "data": [bottle.request.json.get('file')], "username": bottle.request.json.get('username'), "password": bottle.request.json.get('password'), "host": p[1], "destination": bottle.request.json.get('destination') } logger.debug("post %s: %s" % (url, payload,)) r_ = requests.post( url=url, headers={'content-type': 'application/json'}, data=json.dumps(payload)) if r_.status_code != requests.codes.ok: logger.error(r_.text) bottle.abort(500, r_.text) else: logger.debug("Response %s body: %s" % (x, r_.json())) test = { "source": p[0], "destination": p[1], "value": r_.json().get('scp') } body.append(test) return bottle.HTTPResponse(body=pretty_dumps(body), status=201)
def scp(): logger.debug("post-scp") if bottle.request.headers['content-type'] != 'application/json': bottle.abort(500, "Application Type must be json!") logger.info("Body: %s" % (bottle.request.json)) # We assume that only the 1 file should be transmitted! if len(bottle.request.json.get('data')) != 1: bottle.abort(500, "Unsupported data length!") data = bottle.request.json.get('data')[0] user = bottle.request.json.get('username') pswd = bottle.request.json.get('password') host = bottle.request.json.get('host') dest = bottle.request.json.get('destination') source = samples_dir(settings.properties['samples']) + '/' + data destination = dest + '/' + data logger.debug("Source: %s, Destination: %s" % (source, destination)) body = { 'scp': { 'uid': data + "__" + str(time.time()), 'starttime': time.time(), 'endtime': None, } } # Create a connection to the server ssh = None try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) ssh.connect(host, username=user, password=pswd, timeout=10) sftp = ssh.open_sftp() ret = sftp.put(source, destination) body['scp']['endtime'] = time.time() logger.debug("Result: %s" % (ret)) except Exception as e: bottle.abort(500, str(e)) finally: if ssh: ssh.close() return bottle.HTTPResponse(body=pretty_dumps(body), status=200)
def start(self): logger.debug("Starting the rtt-manager service (frequency=%d)" % ( self.__interval)) super(RTTManager, self).start()
def path_min_rtt(owners, destination): logger.debug("path_min_rtt: %s, %s" % (owners, destination)) # XXX_FIXME_XXX: here we are assuming to return a "direct-link" between # the source and destination. Can we have other choises ? return [(owners[0], destination)]
def root(): logger.debug("Enter in the root server") return bottle.static_file('home.html', root=STATIC_FILES_PATH)
def path_min_rtt(owners, destination): logger.debug("path_min_rtt: %s, %s" % (owners, destination)) # XXX_FIXME_XXX: here we are assuming to return a "direct-link" between # the source and destination. Can we have other choises ? return [(owners[0], destination)]
def root(): logger.debug("Enter in the root server") return bottle.static_file('home.html', root=STATIC_FILES_PATH)