+ def update_skewmon(self, vnf_name, resource_name, action):\r
+\r
+ ret = ''\r
+\r
+ config_file_path = '/tmp/skewmon.cfg'\r
+ configfile = open(config_file_path, 'a+')\r
+ try:\r
+ config = json.load(configfile)\r
+ except BaseException:\r
+ # not a valid json file or empty\r
+ config = {}\r
+\r
+ # initialize config file\r
+ if len(self.skewmon_metrics) == 0:\r
+ config = {}\r
+ json.dump(config, configfile)\r
+ configfile.close()\r
+\r
+ docker_name = 'mn.' + vnf_name\r
+ vnf_container = self.dockercli.containers.get(docker_name)\r
+ key = resource_name + '_' + vnf_container.short_id\r
+ vnf_id = vnf_container.id\r
+\r
+ if action == 'start':\r
+ # add a new vnf to monitor\r
+ config[key] = dict(VNF_NAME=vnf_name,\r
+ VNF_ID=vnf_id,\r
+ VNF_METRIC=resource_name)\r
+ ret = 'adding to skewness monitor: {0} {1} '.format(\r
+ vnf_name, resource_name)\r
+ logging.info(ret)\r
+ elif action == 'stop':\r
+ # remove vnf to monitor\r
+ config.pop(key)\r
+ ret = 'removing from skewness monitor: {0} {1} '.format(\r
+ vnf_name, resource_name)\r
+ logging.info(ret)\r
+\r
+ self.skewmon_metrics = config\r
+ configfile = open(config_file_path, 'w')\r
+ json.dump(config, configfile)\r
+ configfile.close()\r
+\r
+ try:\r
+ skewmon_container = self.dockercli.containers.get('skewmon')\r
+\r
+ # remove container if config is empty\r
+ if len(config) == 0:\r
+ ret += 'stopping skewness monitor'\r
+ logging.info('stopping skewness monitor')\r
+ skewmon_container.remove(force=True)\r
+\r
+ except docker.errors.NotFound:\r
+ # start container if not running\r
+ ret += 'starting skewness monitor'\r
+ logging.info('starting skewness monitor')\r
+ volumes = {'/sys/fs/cgroup': {'bind': '/sys/fs/cgroup', 'mode': 'ro'},\r
+ '/tmp/skewmon.cfg': {'bind': '/config.txt', 'mode': 'ro'}}\r
+ self.dockercli.containers.run('skewmon',\r
+ detach=True,\r
+ volumes=volumes,\r
+ labels=['com.containernet'],\r
+ name='skewmon'\r
+ )\r
+ # Wait a while for containers to be completely started\r
+ started = False\r
+ wait_time = 0\r
+ while not started:\r
+ list1 = self.dockercli.containers.list(\r
+ filters={'status': 'running', 'name': 'prometheus'})\r
+ if len(list1) >= 1:\r
+ time.sleep(1)\r
+ started = True\r
+ if wait_time > 5:\r
+ return 'skewmon not started'\r
+ time.sleep(1)\r
+ wait_time += 1\r
+ return ret\r
+\r
+ def term(self, vnf_list=[]):\r
+ """\r
+ Start a terminal window for the specified VNFs\r
+ (start a terminal for all VNFs if vnf_list is empty)\r
+ :param vnf_list:\r
+ :return:\r
+ """\r
+\r
+ if vnf_list is None:\r
+ vnf_list = []\r
+ if not isinstance(vnf_list, list):\r
+ vnf_list = str(vnf_list).split(',')\r
+ vnf_list = map(str.strip, vnf_list)\r
+ logging.info('vnf_list: {}'.format(vnf_list))\r
+\r
+ return self.start_xterm(vnf_list)\r
+\r
+ # start an xterm for the specfified vnfs\r
+\r
+ def start_xterm(self, vnf_names):\r
+ # start xterm for all vnfs\r
+ for vnf_name in vnf_names:\r
+ terminal_cmd = "docker exec -it mn.{0} /bin/bash".format(vnf_name)\r
+\r
+ cmd = ['xterm', '-xrm', 'XTerm*selectToClipboard: true', '-xrm', 'XTerm.vt100.allowTitleOps: false',\r
+ '-T', vnf_name,\r
+ '-e', terminal_cmd]\r
+ Popen(cmd)\r
+\r
+ ret = 'xterms started for {0}'.format(vnf_names)\r
+ if len(vnf_names) == 0:\r
+ ret = 'vnf list is empty, no xterms started'\r
+ return ret\r