fix monitoring bug (cleanup monitored metrics list after vnf removal)
authorstevenvanrossem <steven.vanrossem@intec.ugent.be>
Sun, 7 May 2017 20:40:44 +0000 (22:40 +0200)
committerstevenvanrossem <steven.vanrossem@intec.ugent.be>
Sun, 7 May 2017 20:40:44 +0000 (22:40 +0200)
src/emuvim/dcemulator/monitoring.py

index 78b8007..6418e37 100755 (executable)
@@ -38,6 +38,7 @@ from subprocess import Popen, check_call
 import os\r
 import docker\r
 import json\r
+from copy import deepcopy\r
 \r
 logging.basicConfig(level=logging.INFO)\r
 \r
@@ -272,7 +273,7 @@ class DCNetworkMonitor():
             link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
             vnf_interface = link_dict[0]['src_port_id']\r
 \r
-        for metric_dict in self.network_metrics:\r
+        for metric_dict in deepcopy(self.network_metrics):\r
             if metric_dict['vnf_name'] == vnf_name and metric_dict['vnf_interface'] == vnf_interface \\r
                     and metric_dict['metric_key'] == metric:\r
 \r
@@ -302,20 +303,17 @@ class DCNetworkMonitor():
             elif metric_dict['vnf_name'] == vnf_name and vnf_interface is None and metric is None:\r
                 self.monitor_lock.acquire()\r
                 self.network_metrics.remove(metric_dict)\r
-                for collector in self.registry._collectors:\r
-                    collector_dict = collector._metrics.copy()\r
-                    for name, interface, id in collector_dict:\r
-                        if name == vnf_name:\r
-                            logging.info('3 name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames,\r
-                                                                           collector._metrics))\r
-                            collector.remove(name, interface, 'None')\r
+                logging.info('remove metric from monitor: vnf_name:{0} vnf_interface:{1} mon_port:{2}'.format(metric_dict['vnf_name'], metric_dict['vnf_interface'], metric_dict['mon_port']))\r
 \r
                 delete_from_gateway(self.pushgateway, job='sonemu-SDNcontroller')\r
                 self.monitor_lock.release()\r
-                logging.info('Stopped monitoring vnf: {0}'.format(vnf_name))\r
-                return 'Stopped monitoring: {0}'.format(vnf_name)\r
+                continue\r
 \r
-        return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface)\r
+        if vnf_interface is None and metric is None:\r
+            logging.info('Stopped monitoring vnf: {0}'.format(vnf_name))\r
+            return 'Stopped monitoring: {0}'.format(vnf_name)\r
+        else:\r
+            return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface)\r
 \r
 \r
 # get all metrics defined in the list and export it to Prometheus\r
@@ -430,7 +428,7 @@ class DCNetworkMonitor():
 \r
                 else:\r
                     time_delta = (port_uptime - metric_dict['previous_monitor_time'])\r
-                    metric_rate = (this_measurement - metric_dict['previous_measurement']) / float(time_delta)\r
+                    #metric_rate = (this_measurement - metric_dict['previous_measurement']) / float(time_delta)\r
 \r
                 metric_dict['previous_measurement'] = this_measurement\r
                 metric_dict['previous_monitor_time'] = port_uptime\r
@@ -438,6 +436,7 @@ class DCNetworkMonitor():
 \r
         logging.exception('metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface))\r
         logging.exception('monport:{0}, dpid:{1}'.format(mon_port, switch_dpid))\r
+        logging.exception('monitored network_metrics:{0}'.format(self.network_metrics))\r
         logging.exception('port dict:{0}'.format(port_stat_dict))\r
         return 'metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface)\r
 \r