def check(self, instance): if not self.cadvisor_url: cadvisor_url = instance.get("cadvisor_url", None) detect_cadvisor_url = instance.get("kubernetes_detect_cadvisor", False) if not cadvisor_url: if detect_cadvisor_url: kubernetes_connector = utils.KubernetesConnector( self.connection_timeout) host = kubernetes_connector.get_agent_pod_host() cadvisor_url = "http://{}:4194".format(host) else: exception_message = "Either cAdvisor url or kubernetes detect cAdvisor must be set when " \ "monitoring a Kubernetes Node." self.log.error(exception_message) raise Exception(exception_message) self.cadvisor_url = "{}/{}".format(cadvisor_url, "api/v2.0/stats?count=1") dimensions = self._set_dimensions(None, instance) try: host_metrics = requests.get(self.cadvisor_url, self.connection_timeout).json() except Exception as e: self.log.error( "Error communicating with cAdvisor to collect data - {}". format(e)) else: self._parse_send_metrics(host_metrics, dimensions)
def check(self, instance): dimensions = self._set_dimensions(None, instance) del dimensions['hostname'] if not self.auto_detect_endpoints: metric_endpoint = instance.get("metric_endpoint", None) if not metric_endpoint: self.log.error("metric_endpoint must be defined for each instance") return endpoint_dimensions = instance.get("default_dimensions", {}) endpoint_dimensions.update(dimensions) self.report_endpoint_metrics(metric_endpoint, endpoint_dimensions) else: self.kubernetes_labels = instance.get('kubernetes_labels', KUBERNETES_LABELS) if not self.kubernetes_connector: self.kubernetes_connector = utils.KubernetesConnector(self.connection_timeout) if self.detect_method == "pod": if not self.kubelet_url: try: host = self.kubernetes_connector.get_agent_pod_host() self.kubelet_url = "http://{}:10255/pods".format(host) except Exception as e: self.log.error("Could not obtain current host from Kubernetes API {}. " "Skipping check".format(e)) return metric_endpoints = self._get_metric_endpoints_by_pod(dimensions) # Detect by service else: metric_endpoints = self._get_metric_endpoints_by_service(dimensions) for metric_endpoint, endpoint_dimensions in six.iteritems(metric_endpoints): endpoint_dimensions.update(dimensions) self.report_endpoint_metrics(metric_endpoint, endpoint_dimensions)
def check(self, instance): dimensions = self._set_dimensions(None, instance) del dimensions['hostname'] if not self.auto_detect_endpoints: metric_endpoint = instance.get("metric_endpoint", None) if not metric_endpoint: self.log.error( "metric_endpoint must be defined for each instance") return endpoint_dimensions = instance.get("default_dimensions", {}) endpoint_dimensions.update(dimensions) self.report_endpoint_metrics(metric_endpoint, endpoint_dimensions) else: self.kubernetes_labels = instance.get('kubernetes_labels', KUBERNETES_LABELS) if not self.kubernetes_connector: self.kubernetes_connector = utils.KubernetesConnector( self.connection_timeout) # Check if we need to clear pod cache so it does not build up over time if self.k8s_pod_cache is not None: if not self.cache_start_time: self.cache_start_time = time.time() else: current_time = time.time() if (current_time - self.cache_start_time) > 86400: self.cache_start_time = current_time self.k8s_pod_cache = {} self.initialize_pod_cache() if self.detect_method == "pod": if not self.kubelet_url: try: host = self.kubernetes_connector.get_agent_pod_host() self.kubelet_url = "http://{}:10255/pods".format(host) except Exception as e: self.log.error( "Could not obtain current host from Kubernetes API {}. " "Skipping check".format(e)) return prometheus_endpoints = self._get_metric_endpoints_by_pod( dimensions) # Detect by service else: prometheus_endpoints = self._get_metric_endpoints_by_service( dimensions) for prometheus_endpoint in prometheus_endpoints: self.report_endpoint_metrics( prometheus_endpoint.scrape_endpoint, prometheus_endpoint.dimensions, prometheus_endpoint.whitelist, prometheus_endpoint.metric_types, prometheus_endpoint.report_pod_label_owner)
def prepare_run(self): """Set up Kubernetes connection information""" instance = self.instances[0] self.host = instance.get("host", None) derive_host = instance.get("derive_host", False) if not self.host: if derive_host: self.kubernetes_connector = utils.KubernetesConnector(self.connection_timeout) self.host = self.kubernetes_connector.get_agent_pod_host() else: exception_message = "Either host or derive host must be set when " \ "running Kubernetes plugin." self.log.exception(exception_message) raise Exception(exception_message)
def check(self, instance): if not self.cadvisor_url: cadvisor_url = instance.get("cadvisor_url", None) detect_cadvisor_url = instance.get("kubernetes_detect_cadvisor", False) if not cadvisor_url: if detect_cadvisor_url: kubernetes_connector = utils.KubernetesConnector( self.connection_timeout) host = kubernetes_connector.get_agent_pod_host() cadvisor_url = "http://{}:4194".format(host) else: exception_message = "Either cAdvisor url or kubernetes " \ "detect cAdvisor must be set when " \ "monitoring a Kubernetes Node." self.log.error(exception_message) raise Exception(exception_message) self.cadvisor_url = "{}/{}".format(cadvisor_url, "api/v2.0/stats?count=1") dimensions = self._set_dimensions(None, instance) try: host_metrics = requests.get(self.cadvisor_url, self.connection_timeout).json() except Exception as e: self.log.error( "Error communicating with cAdvisor to collect data - {}". format(e)) else: # Retrieve machine info only once if not self.cadvisor_machine_url: # Replace path in current cadvisor_url result = urlparse(self.cadvisor_url) self.cadvisor_machine_url = urlunparse( result._replace(path="api/v2.0/machine")) try: machine_info = requests.get( self.cadvisor_machine_url).json() except Exception as ex: self.log.error( "Error communicating with cAdvisor to collect machine data - {}" .format(ex)) else: self._parse_machine_info(machine_info) self._parse_send_metrics(host_metrics, dimensions)
def prepare_run(self): """Set up Kubernetes connection information""" instance = self.instances[0] host = instance.get("host", None) derive_api_url = instance.get("derive_api_url", None) if not host: if derive_api_url: self.kubernetes_connector = utils.KubernetesConnector( self.connection_timeout) else: exception_message = "Either Kubernetes API url (host and port)" \ " or derive_api_url=True must be set" \ " when running Kubernetes API plugin." self.log.error(exception_message) raise Exception(exception_message) else: kubernetes_api_port = instance.get("kubernetes_api_port", "8080") self.kubernetes_api = "http://{}:{}".format( host, kubernetes_api_port)
# (C) Copyright 2017 Hewlett Packard Enterprise Development LP # coding=utf-8 from monasca_agent.collector.checks import utils kubernetes_connector = utils.KubernetesConnector(3) print(kubernetes_connector.get_agent_pod_host(return_host_name=True))