(self, instance)
Calls asynchronously _collect_metrics_async on all MORs, as the
        job queue is processed the Aggregator will receive the metrics.
Calls asynchronously _collect_metrics_async on all MORs, as the
        job queue is processed the Aggregator will receive the metrics.
Calls asynchronously _collect_metrics_async on all MORs as the job queue is processed the Aggregator will receive the metrics .
def collect_metrics(self, instance):
        """
        Calls asynchronously _collect_metrics_async on all MORs, as the
        job queue is processed the Aggregator will receive the metrics.
        """
        i_key = self._instance_key(instance)
        if not self.mor_cache.contains(i_key):
            self.log.debug("Not collecting metrics for instance '%s', nothing to do yet.", i_key)
            return

        server_instance = self._get_server_instance(instance)
        max_historical_metrics = DEFAULT_MAX_HIST_METRICS

        if self._should_collect_historical(instance):
            try:
                if 'max_query_metrics' in instance:
                    max_historical_metrics = int(instance['max_query_metrics'])
                    self.log.info("Collecting up to %d metrics", max_historical_metrics)
                else:
                    vcenter_settings = server_instance.content.setting.QueryOptions("config.vpxd.stats.maxQueryMetrics")
                    max_historical_metrics = int(vcenter_settings[0].value)
                if max_historical_metrics < 0:
                    max_historical_metrics = float('inf')
            except Exception as e:
                self.log.debug(
                    "Error getting maxQueryMetrics setting "
                    "(max_historical_metrics=%s, DEFAULT_MAX_HIST_METRICS=%s): %s",
                    max_historical_metrics,
                    DEFAULT_MAX_HIST_METRICS,
                    e,
                )

        # TODO: Remove me once the fix for `max_query_metrics` is here by default
        mors_batch_method = (
            self.mor_cache.mors_batch
            if is_affirmative(instance.get('fix_max_query_metrics'))
            else self.mor_cache.legacy_mors_batch
        )

        vm_count = 0
        custom_tags = instance.get('tags', [])
        tags = ["vcenter_server:{}".format(ensure_unicode(instance.get('name')))] + custom_tags

        n_mors = self.mor_cache.instance_size(i_key)
        if not n_mors:
            if self._is_main_instance(instance):
                self.gauge('vsphere.vm.count', vm_count, tags=tags)
            self.log.debug("No Mor objects to process for instance '%s', skip...", i_key)
            return

        self.log.debug("Collecting metrics for %s mors", ensure_unicode(n_mors))

        # Request metrics for several objects at once. We can limit the number of objects with batch_size
        # If batch_size is 0, process everything at once
        batch_size = self.batch_morlist_size or n_mors
        for batch in mors_batch_method(i_key, batch_size, max_historical_metrics):
            query_specs = []
            for mor in itervalues(batch):
                if mor['mor_type'] == 'vm':
                    vm_count += 1
                if mor['mor_type'] not in REALTIME_RESOURCES and ('metrics' not in mor or not mor['metrics']):
                    continue

                query_spec = vim.PerformanceManager.QuerySpec()
                query_spec.entity = mor["mor"]
                query_spec.intervalId = mor.get("interval")
                if mor['mor_type'] in REALTIME_RESOURCES:
                    query_spec.metricId = self.metadata_cache.get_metric_ids(i_key)
                    query_spec.maxSample = 1  # Request a single datapoint
                else:
                    query_spec.metricId = mor["metrics"]
                    # We cannot use `maxSample` for historical metrics, let's specify a timewindow that will
                    # contain at least one element
                    query_spec.startTime = datetime.now() - timedelta(hours=2)
                query_specs.append(query_spec)

            if query_specs:
                self.pool.apply_async(self._collect_metrics_async, args=(instance, query_specs))

        if self._is_main_instance(instance):
            self.gauge('vsphere.vm.count', vm_count, tags=tags)
def collect_metrics ( self , instance ) : i_key = self . _instance_key ( instance ) if not self . mor_cache . contains ( i_key ) : self . log . debug ( "Not collecting metrics for instance '%s', nothing to do yet." , i_key ) return server_instance = self . _get_server_instance ( instance ) max_historical_metrics = DEFAULT_MAX_HIST_METRICS if self . _should_collect_historical ( instance ) : try : if 'max_query_metrics' in instance : max_historical_metrics = int ( instance [ 'max_query_metrics' ] ) self . log . info ( "Collecting up to %d metrics" , max_historical_metrics ) else : vcenter_settings = server_instance . content . setting . QueryOptions ( "config.vpxd.stats.maxQueryMetrics" ) max_historical_metrics = int ( vcenter_settings [ 0 ] . value ) if max_historical_metrics < 0 : max_historical_metrics = float ( 'inf' ) except Exception as e : self . log . debug ( "Error getting maxQueryMetrics setting " "(max_historical_metrics=%s, DEFAULT_MAX_HIST_METRICS=%s): %s" , max_historical_metrics , DEFAULT_MAX_HIST_METRICS , e , ) # TODO: Remove me once the fix for `max_query_metrics` is here by default mors_batch_method = ( self . mor_cache . mors_batch if is_affirmative ( instance . get ( 'fix_max_query_metrics' ) ) else self . mor_cache . legacy_mors_batch ) vm_count = 0 custom_tags = instance . get ( 'tags' , [ ] ) tags = [ "vcenter_server:{}" . format ( ensure_unicode ( instance . get ( 'name' ) ) ) ] + custom_tags n_mors = self . mor_cache . instance_size ( i_key ) if not n_mors : if self . _is_main_instance ( instance ) : self . gauge ( 'vsphere.vm.count' , vm_count , tags = tags ) self . log . debug ( "No Mor objects to process for instance '%s', skip..." , i_key ) return self . log . debug ( "Collecting metrics for %s mors" , ensure_unicode ( n_mors ) ) # Request metrics for several objects at once. We can limit the number of objects with batch_size # If batch_size is 0, process everything at once batch_size = self . batch_morlist_size or n_mors for batch in mors_batch_method ( i_key , batch_size , max_historical_metrics ) : query_specs = [ ] for mor in itervalues ( batch ) : if mor [ 'mor_type' ] == 'vm' : vm_count += 1 if mor [ 'mor_type' ] not in REALTIME_RESOURCES and ( 'metrics' not in mor or not mor [ 'metrics' ] ) : continue query_spec = vim . PerformanceManager . QuerySpec ( ) query_spec . entity = mor [ "mor" ] query_spec . intervalId = mor . get ( "interval" ) if mor [ 'mor_type' ] in REALTIME_RESOURCES : query_spec . metricId = self . metadata_cache . get_metric_ids ( i_key ) query_spec . maxSample = 1 # Request a single datapoint else : query_spec . metricId = mor [ "metrics" ] # We cannot use `maxSample` for historical metrics, let's specify a timewindow that will # contain at least one element query_spec . startTime = datetime . now ( ) - timedelta ( hours = 2 ) query_specs . append ( query_spec ) if query_specs : self . pool . apply_async ( self . _collect_metrics_async , args = ( instance , query_specs ) ) if self . _is_main_instance ( instance ) : self . gauge ( 'vsphere.vm.count' , vm_count , tags = tags )