+ def stop_metric(self, vnf_name, vnf_interface, metric):\r
+\r
+ for metric_dict in 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
+ self.monitor_lock.acquire()\r
+\r
+ self.network_metrics.remove(metric_dict)\r
+\r
+ #this removes the complete metric, all labels...\r
+ #REGISTRY.unregister(self.prom_metrics[metric_dict['metric_key']])\r
+ #self.registry.unregister(self.prom_metrics[metric_dict['metric_key']])\r
+\r
+ for collector in self.registry._collectors :\r
+ logging.info('name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames, collector._metrics))\r
+ """\r
+ INFO:root:name:sonemu_rx_count_packets\r
+ labels:('vnf_name', 'vnf_interface')\r
+ metrics:{(u'tsrc', u'output'): < prometheus_client.core.Gauge\r
+ object\r
+ at\r
+ 0x7f353447fd10 >}\r
+ """\r
+ logging.info('{0}'.format(collector._metrics.values()))\r
+ #if self.prom_metrics[metric_dict['metric_key']]\r
+ if (vnf_name, vnf_interface) in collector._metrics:\r
+ logging.info('2 name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames,\r
+ collector._metrics))\r
+ #collector._metrics = {}\r
+ collector.remove(vnf_name, vnf_interface)\r
+\r
+ # set values to NaN, prometheus api currently does not support removal of metrics\r
+ #self.prom_metrics[metric_dict['metric_key']].labels(vnf_name, vnf_interface).set(float('nan'))\r
+\r
+ # this removes the complete metric, all labels...\r
+ # 1 single monitor job for all metrics of the SDN controller\r
+ # we can only remove from the pushgateway grouping keys(labels) which we have defined for the add_to_pushgateway\r
+ # we can not specify labels from the metrics to be removed\r
+ # if we need to remove the metrics seperatelty, we need to give them a separate grouping key, and probably a diffferent registry also\r
+ delete_from_gateway(self.pushgateway, job='sonemu-SDNcontroller')\r
+\r
+ self.monitor_lock.release()\r
+\r
+ logging.info('Stopped monitoring: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric))\r
+ return 'Stopped monitoring: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric)\r
+\r
+\r
+ # get all metrics defined in the list and export it to Prometheus\r
+ def get_network_metrics(self):\r
+ while self.start_monitoring:\r
+\r
+ self.monitor_lock.acquire()\r
+\r
+ # group metrics by dpid to optimize the rest api calls\r
+ dpid_list = [metric_dict['switch_dpid'] for metric_dict in self.network_metrics]\r
+ dpid_set = set(dpid_list)\r
+\r
+ for dpid in dpid_set:\r
+\r
+ # query Ryu\r
+ ret = self.REST_cmd('stats/port', dpid)\r
+ port_stat_dict = ast.literal_eval(ret)\r
+\r
+ metric_list = [metric_dict for metric_dict in self.network_metrics\r
+ if int(metric_dict['switch_dpid'])==int(dpid)]\r
+ #logging.info('1set prom packets:{0} '.format(self.network_metrics))\r
+ for metric_dict in metric_list:\r
+ self.set_network_metric(metric_dict, port_stat_dict)\r
+\r
+ self.monitor_lock.release()\r
+ time.sleep(1)\r
+\r
+ # add metric to the list to export to Prometheus, parse the Ryu port-stats reply\r
+ def set_network_metric(self, metric_dict, port_stat_dict):\r
+ # vnf tx is the datacenter switch rx and vice-versa\r
+ metric_key = self.switch_tx_rx(metric_dict['metric_key'])\r
+ switch_dpid = metric_dict['switch_dpid']\r
+ vnf_name = metric_dict['vnf_name']\r
+ vnf_interface = metric_dict['vnf_interface']\r
+ previous_measurement = metric_dict['previous_measurement']\r
+ previous_monitor_time = metric_dict['previous_monitor_time']\r
+ mon_port = metric_dict['mon_port']\r
+\r
+ for port_stat in port_stat_dict[str(switch_dpid)]:\r
+ if int(port_stat['port_no']) == int(mon_port):\r
+ port_uptime = port_stat['duration_sec'] + port_stat['duration_nsec'] * 10 ** (-9)\r
+ this_measurement = int(port_stat[metric_key])\r
+ #logging.info('set prom packets:{0} {1}:{2}'.format(this_measurement, vnf_name, vnf_interface))\r
+\r
+ # set prometheus metric\r
+ self.prom_metrics[metric_dict['metric_key']].\\r
+ labels({'vnf_name':vnf_name, 'vnf_interface':vnf_interface}).\\r
+ set(this_measurement)\r
+ #push_to_gateway(self.pushgateway, job='SDNcontroller',\r
+ # grouping_key={'metric':metric_dict['metric_key']}, registry=self.registry)\r
+\r
+ # 1 single monitor job for all metrics of the SDN controller\r
+ pushadd_to_gateway(self.pushgateway, job='sonemu-SDNcontroller', registry=self.registry)\r
+\r
+ if previous_monitor_time <= 0 or previous_monitor_time >= port_uptime:\r
+ metric_dict['previous_measurement'] = int(port_stat[metric_key])\r
+ metric_dict['previous_monitor_time'] = port_uptime\r
+ # do first measurement\r
+ #logging.info('first measurement')\r
+ time.sleep(1)\r
+ self.monitor_lock.release()\r
+\r
+ metric_rate = self.get_network_metrics()\r
+ return metric_rate\r
+\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
+ logging.info('metric: {0} rate:{1}'.format(metric_dict['metric_key'], metric_rate))\r
+\r
+ metric_dict['previous_measurement'] = this_measurement\r
+ metric_dict['previous_monitor_time'] = port_uptime\r
+ return metric_rate\r
+\r
+ logging.exception('metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface))\r
+ return 'metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface)\r