Fetch the metrics from the stored proc
328244
def do_stored_procedure_check(self, instance, proc):
        

        guardSql = instance.get('proc_only_if')
        custom_tags = instance.get("tags", [])

        if (guardSql and self.proc_check_guard(instance, guardSql)) or not guardSql:
            self.open_db_connections(instance, self.DEFAULT_DB_KEY)
            cursor = self.get_cursor(instance, self.DEFAULT_DB_KEY)

            try:
                self.log.debug("Calling Stored Procedure : {}".format(proc))
                if self._get_connector(instance) == 'adodbapi':
                    cursor.callproc(proc)
                else:
                    # pyodbc does not support callproc; use execute instead.
                    # Reference: https://github.com/mkleehammer/pyodbc/wiki/Calling-Stored-Procedures
                    call_proc = '{{CALL {}}}'.format(proc)
                    cursor.execute(call_proc)

                rows = cursor.fetchall()
                self.log.debug("Row count ({}) : {}".format(proc, cursor.rowcount))

                for row in rows:
                    tags = [] if row.tags is None or row.tags == '' else row.tags.split(',')
                    tags.extend(custom_tags)

                    if row.type.lower() in self.proc_type_mapping:
                        self.proc_type_mapping[row.type](row.metric, row.value, tags)
                    else:
                        self.log.warning(
                            '{} is not a recognised type from procedure {}, metric {}'.format(
                                row.type, proc, row.metric
                            )
                        )

            except Exception as e:
                self.log.warning("Could not call procedure {}: {}".format(proc, e))

            self.close_cursor(cursor)
            self.close_db_connections(instance, self.DEFAULT_DB_KEY)
        else:
            self.log.info("Skipping call to {} due to only_if".format(proc))
169166
def _compute_needed_metrics(self, instance, available_metrics):
        
        i_key = self._instance_key(instance)
        if self.in_compatibility_mode(instance):
            if instance.get('all_metrics', False):
                return available_metrics

            wanted_metrics = []
            # Get only the basic metrics
            for counter_id in available_metrics:
                # No cache yet, skip it for now
                if not self.metadata_cache.contains(i_key, counter_id):
                    self.log.debug(
                        "No metadata found for counter {}, will not collect it".format(ensure_unicode(counter_id))
                    )
                    continue
                metadata = self.metadata_cache.get_metadata(i_key, counter_id)
                if metadata.get('name') in BASIC_METRICS:
                    wanted_metrics.append(vim.PerformanceManager.MetricId(counterId=counter_id, instance="*"))

            return wanted_metrics
        else:
            # The metadata cache contains only metrics of the desired level, so use it to filter the metrics to keep
            return [
                vim.PerformanceManager.MetricId(counterId=counter_id, instance="*")
                for counter_id in available_metrics
                if self.metadata_cache.contains(i_key, counter_id)
            ]
246023
def retrieveVals(self):
        """"""
        if self._stats is None:
            serverInfo = MemcachedInfo(self._host,  self._port, self._socket_file)
            stats = serverInfo.getStats()
        else:
            stats = self._stats
        if stats is None:
            raise Exception("Undetermined error accesing stats.")        
        stats['set_hits'] = stats.get('total_items')
        if stats.has_key('cmd_set') and stats.has_key('total_items'): 
            stats['set_misses'] = stats['cmd_set'] - stats['total_items']
        self.saveState(stats)
        if self.hasGraph('memcached_connections'):
            self.setGraphVal('memcached_connections', 'conn', 
                             stats.get('curr_connections'))
        if self.hasGraph('memcached_items'):
            self.setGraphVal('memcached_items', 'items', 
                             stats.get('curr_items'))
        if self.hasGraph('memcached_memory'):
            self.setGraphVal('memcached_memory', 'bytes', 
                             stats.get('bytes'))
        if self.hasGraph('memcached_connrate'):
            self.setGraphVal('memcached_connrate', 'conn', 
                             stats.get('total_connections'))
        if self.hasGraph('memcached_traffic'):
            self.setGraphVal('memcached_traffic', 'rxbytes', 
                             stats.get('bytes_read'))
            self.setGraphVal('memcached_traffic', 'txbytes', 
                             stats.get('bytes_written'))
        if self.hasGraph('memcached_reqrate'):
            self.setGraphVal('memcached_reqrate', 'set', 
                             stats.get('cmd_set'))
            self.setGraphVal('memcached_reqrate', 'get', 
                             stats.get('cmd_get'))
            if self.graphHasField('memcached_reqrate', 'del'):
                self.setGraphVal('memcached_reqrate', 'del',
                                 safe_sum([stats.get('delete_hits'), 
                                           stats.get('delete_misses')]))
            if self.graphHasField('memcached_reqrate', 'cas'):
                self.setGraphVal('memcached_reqrate', 'cas',
                                 safe_sum([stats.get('cas_hits'), 
                                           stats.get('cas_misses'), 
                                           stats.get('cas_badval')]))
            if self.graphHasField('memcached_reqrate', 'incr'):
                self.setGraphVal('memcached_reqrate', 'incr',
                                 safe_sum([stats.get('incr_hits'), 
                                           stats.get('incr_misses')]))
            if self.graphHasField('memcached_reqrate', 'decr'):
                self.setGraphVal('memcached_reqrate', 'decr',
                                 safe_sum([stats.get('decr_hits'), 
                                           stats.get('decr_misses')]))
        if self.hasGraph('memcached_statget'):
            self.setGraphVal('memcached_statget', 'hit', 
                             stats.get('get_hits'))
            self.setGraphVal('memcached_statget', 'miss', 
                             stats.get('get_misses'))
            self.setGraphVal('memcached_statget', 'total', 
                             safe_sum([stats.get('get_hits'),
                                       stats.get('get_misses')]))
        if self.hasGraph('memcached_statset'):
            self.setGraphVal('memcached_statset', 'hit', 
                             stats.get('set_hits'))
            self.setGraphVal('memcached_statset', 'miss', 
                             stats.get('set_misses'))
            self.setGraphVal('memcached_statset', 'total', 
                             safe_sum([stats.get('set_hits'),
                                       stats.get('set_misses')]))
        if self.hasGraph('memcached_statdel'):
            self.setGraphVal('memcached_statdel', 'hit', 
                             stats.get('delete_hits'))
            self.setGraphVal('memcached_statdel', 'miss', 
                             stats.get('delete_misses'))
            self.setGraphVal('memcached_statdel', 'total', 
                             safe_sum([stats.get('delete_hits'),
                                       stats.get('delete_misses')]))
        if self.hasGraph('memcached_statcas'):
            self.setGraphVal('memcached_statcas', 'hit', 
                             stats.get('cas_hits'))
            self.setGraphVal('memcached_statcas', 'miss', 
                             stats.get('cas_misses'))
            self.setGraphVal('memcached_statcas', 'badval', 
                             stats.get('cas_badval'))
            self.setGraphVal('memcached_statcas', 'total', 
                             safe_sum([stats.get('cas_hits'),
                                       stats.get('cas_misses'),
                                       stats.get('cas_badval')]))
        if self.hasGraph('memcached_statincrdecr'):
            self.setGraphVal('memcached_statincrdecr', 'incr_hit', 
                             stats.get('incr_hits'))
            self.setGraphVal('memcached_statincrdecr', 'decr_hit', 
                             stats.get('decr_hits'))
            self.setGraphVal('memcached_statincrdecr', 'incr_miss', 
                             stats.get('incr_misses'))
            self.setGraphVal('memcached_statincrdecr', 'decr_miss', 
                             stats.get('decr_misses'))
            self.setGraphVal('memcached_statincrdecr', 'total', 
                             safe_sum([stats.get('incr_hits'),
                                       stats.get('decr_hits'),
                                       stats.get('incr_misses'),
                                       stats.get('decr_misses')]))
        if self.hasGraph('memcached_statevict'):
            self.setGraphVal('memcached_statevict', 'evict', 
                             stats.get('evictions'))
            if self.graphHasField('memcached_statevict', 'reclaim'):
                self.setGraphVal('memcached_statevict', 'reclaim', 
                                 stats.get('reclaimed'))
        if self.hasGraph('memcached_statauth'):
            self.setGraphVal('memcached_statauth', 'reqs', 
                             stats.get('auth_cmds'))
            self.setGraphVal('memcached_statauth', 'errors', 
                             stats.get('auth_errors'))
        if self.hasGraph('memcached_hitpct'):
            prev_stats = self._prev_stats
            for (field_name,  field_hits,  field_misses) in (
                    ('set',  'set_hits',  'set_misses'),
                    ('get',  'get_hits',  'get_misses'), 
                    ('del',  'delete_hits',  'delete_misses'), 
                    ('cas',  'cas_hits',  'cas_misses'), 
                    ('incr',  'incr_hits',  'incr_misses'), 
                    ('decr',  'decr_hits',  'decr_misses')
                ):
                if prev_stats:
                    if (stats.has_key(field_hits) 
                        and prev_stats.has_key(field_hits)
                        and stats.has_key(field_misses) 
                        and prev_stats.has_key(field_misses)):
                        hits = stats[field_hits] - prev_stats[field_hits]
                        misses = stats[field_misses] - prev_stats[field_misses]
                        total = hits + misses
                        if total > 0:
                            val = 100.0 * hits / total
                        else:
                            val = 0
                        self.setGraphVal('memcached_hitpct',  field_name, 
                                         round(val,  2))
647044
def _get_archiver_metrics(self, key, db):
        
        # While there's only one set for now, prepare for future additions to
        # the table, mirroring _get_bgw_metrics()
        metrics = self.archiver_metrics.get(key)

        if self._is_9_4_or_above(key, db) and metrics is None:
            # Collect from only one instance. See _get_bgw_metrics() for details on why.
            sub_key = key[:2]
            if sub_key in self.db_archiver_metrics:
                self.archiver_metrics[key] = None
                self.log.debug(
                    "Not collecting archiver metrics for key: {0} as "
                    "they are already collected by another instance".format(key)
                )
                return None

            self.db_archiver_metrics.append(sub_key)

            self.archiver_metrics[key] = dict(self.COMMON_ARCHIVER_METRICS)
            metrics = self.archiver_metrics.get(key)

        if not metrics:
            return None

        return {'descriptors': [], 'metrics': metrics, 'query': "select %s FROM pg_stat_archiver", 'relation': False}
1241648
private Map<MetricQuery, List<Metric>> getSubQueryMetrics(List<MetricQuery> queries) {
		Map<MetricQuery, Future<List<Metric>>> queryFutureMap = new HashMap<>();

		for (MetricQuery query : queries) {
			String requestBody = fromEntity(query);
			String requestUrl = query.getMetricQueryContext().getReadEndPoint() + "/api/query";
			queryFutureMap.put(query, _executorService.submit(new QueryWorker(requestUrl, query.getMetricQueryContext().getReadEndPoint(), requestBody)));
		}

		Map<MetricQuery, List<Metric>> subQueryMetricsMap = new HashMap<>();

		for (Entry<MetricQuery, Future<List<Metric>>> entry : queryFutureMap.entrySet()) {
			List<Metric> metrics = new ArrayList<>();
			List<Metric> m = null;
			try {
				m = entry.getValue().get();
			} catch (InterruptedException | ExecutionException e) {
				_logger.warn("Failed to get metrics from TSDB. Reason: " + e.getMessage());
				try {
					String readBackupEndPoint = _readBackupEndPointsMap.get(entry.getKey().getMetricQueryContext().getReadEndPoint()); 
					if (!readBackupEndPoint.isEmpty()) {
						_logger.warn("Trying to read from Backup endpoint");
						m = new QueryWorker(readBackupEndPoint + "/api/query", readBackupEndPoint, fromEntity(entry.getKey())).call();
					}
				} catch (Exception ex) {
					_logger.warn("Failed to get metrics from Backup TSDB. Reason: " + ex.getMessage());
					continue;
				}
			}

			if (m != null) {
				for (Metric metric : m) {
					if (metric != null) {
						metric.setQuery(entry.getKey());
						metrics.add(metric);
					}
				}
			}

			subQueryMetricsMap.put(entry.getKey(), metrics);
		}
		return subQueryMetricsMap;
	}
664751
def retrieveVals(self):
        """"""
        if self.hasGraph('sys_loadavg'):
            self._loadstats = self._sysinfo.getLoadAvg()
            if self._loadstats:
                self.setGraphVal('sys_loadavg', 'load15min', self._loadstats[2])
                self.setGraphVal('sys_loadavg', 'load5min', self._loadstats[1])
                self.setGraphVal('sys_loadavg', 'load1min', self._loadstats[0])
        if self._cpustats and self.hasGraph('sys_cpu_util'):
            for field in self.getGraphFieldList('sys_cpu_util'):
                self.setGraphVal('sys_cpu_util', 
                                 field, int(self._cpustats[field] * 1000))
        if self._memstats:
            if self.hasGraph('sys_mem_util'):
                for field in self.getGraphFieldList('sys_mem_util'):
                    self.setGraphVal('sys_mem_util', 
                                     field, self._memstats[field])
            if self.hasGraph('sys_mem_avail'):
                for field in self.getGraphFieldList('sys_mem_avail'):
                    self.setGraphVal('sys_mem_avail', 
                                     field, self._memstats[field])
            if self.hasGraph('sys_mem_huge'):
                for field in ['Rsvd', 'Surp', 'Free']:
                    fkey = 'HugePages_' + field
                    if self._memstats.has_key(fkey):
                        self.setGraphVal('sys_mem_huge', field, 
                            self._memstats[fkey] * self._memstats['Hugepagesize'])
        if self.hasGraph('sys_processes'):
            if self._procstats is None:
                self._procstats = self._sysinfo.getProcessStats()
            if self._procstats:
                self.setGraphVal('sys_processes', 'running', 
                                 self._procstats['procs_running'])
                self.setGraphVal('sys_processes', 'blocked', 
                                 self._procstats['procs_blocked'])
        if self.hasGraph('sys_forks'):
            if self._procstats is None:
                self._procstats = self._sysinfo.getProcessStats()
            if self._procstats:
                self.setGraphVal('sys_forks', 'forks', 
                                 self._procstats['processes'])
        if self.hasGraph('sys_intr_ctxt'):
            if self._procstats is None:
                self._procstats = self._sysinfo.getProcessStats()
            if self._procstats:
                for field in self.getGraphFieldList('sys_intr_ctxt'):
                    self.setGraphVal('sys_intr_ctxt', field, 
                                     self._procstats[field])
        if self.hasGraph('sys_vm_paging'):
            if self._vmstats is None:
                self._vmstats = self._sysinfo.getVMstats()
            if self._vmstats:
                self.setGraphVal('sys_vm_paging', 'in', 
                                 self._vmstats['pgpgin'])
                self.setGraphVal('sys_vm_paging', 'out', 
                                 self._vmstats['pgpgout'])
        if self.hasGraph('sys_vm_swapping'):
            if self._vmstats is None:
                self._vmstats = self._sysinfo.getVMstats()
            if self._vmstats:
                self.setGraphVal('sys_vm_swapping', 'in', 
                                 self._vmstats['pswpin'])
                self.setGraphVal('sys_vm_swapping', 'out', 
                                 self._vmstats['pswpout'])
824113
private void readBufferAndLogMetrics(final Operation currentOp,
    final ByteBuffer rbuf, final MemcachedNode node) throws IOException {
    currentOp.readFromBuffer(rbuf);
    if (currentOp.getState() == OperationState.COMPLETE) {
      getLogger().debug("Completed read op: %s and giving the next %d "
        + "bytes", currentOp, rbuf.remaining());
      Operation op = node.removeCurrentReadOp();
      assert op == currentOp : "Expected to pop " + currentOp + " got "
        + op;

      if (op.hasErrored()) {
        metrics.markMeter(OVERALL_RESPONSE_FAIL_METRIC);
      } else {
        metrics.markMeter(OVERALL_RESPONSE_SUCC_METRIC);
      }
    } else if (currentOp.getState() == OperationState.RETRY) {
      handleRetryInformation(currentOp.getErrorMsg());
      getLogger().debug("Reschedule read op due to NOT_MY_VBUCKET error: "
        + "%s ", currentOp);
      ((VBucketAware) currentOp).addNotMyVbucketNode(
        currentOp.getHandlingNode());
      Operation op = node.removeCurrentReadOp();
      assert op == currentOp : "Expected to pop " + currentOp + " got "
        + op;

      retryOperation(currentOp);
      metrics.markMeter(OVERALL_RESPONSE_RETRY_METRIC);
    }
  }
637410
def _collect_for_instance(self, instance, connection):
        """"""
        with connection.cursor() as cursor:
            for queue, metrics in self.get_queue_info(instance, cursor):
                for name, metric in metrics.items():
                    self.publish('.'.join((instance, queue, name)), metric)

        with connection.cursor() as cursor:
            consumers = self.get_consumer_info(instance, cursor)
            for queue, consumer, metrics in consumers:
                for name, metric in metrics.items():
                    key_parts = (instance, queue, 'consumers', consumer, name)
                    self.publish('.'.join(key_parts), metric)
475466
def _get_proc_name(proc):
    '''
    
    '''
    try:
        return salt.utils.data.decode(proc.name() if PSUTIL2 else proc.name)
    except (psutil.NoSuchProcess, psutil.AccessDenied):
        return []
176454
def getWorkerInfo(dataTask):
    """"""
    workertime = []
    workertasks = []
    for fichier, vals in dataTask.items():
        if hasattr(vals, 'values'):
            #workers_names.append(fichier)
            # Data from worker
            totaltime = sum([a['executionTime'] for a in vals.values()])
            totaltasks = sum([1 for a in vals.values()])
            workertime.append(totaltime)
            workertasks.append(totaltasks)
    return workertime, workertasks
1090723
def get_default_metrics(config):
    
    archivers = list_archivers(config)
    default_metrics = []

    for archiver in archivers:
        index = get_archiver_index(config, archiver)

        if len(index) == 0:
            logger.warning("No records found in the index, no metrics available")
            return []

        operators = index[0]["operators"]
        for operator in operators:
            o = resolve_operator(operator)
            if o.cls.default_metric_index is not None:
                metric = o.cls.metrics[o.cls.default_metric_index]
                default_metrics.append("{0}.{1}".format(o.cls.name, metric.name))
    return default_metrics
31379
def load(self):
        """"""
        LOGGER.debug('%s: start checking: %s', self.name, self.query)
        if self.waiting:
            self.notify('warning', 'Process takes too much time', target='waiting', ntype='common')
        else:
            self.waiting = True
            try:
                response = yield self.client.fetch(self.url, auth_username=self.auth_username,
                                                   auth_password=self.auth_password,
                                                   request_timeout=self.request_timeout,
                                                   connect_timeout=self.connect_timeout,
                                                   validate_cert=self.validate_cert)
                records = (
                    GraphiteRecord(line, self.default_nan_value, self.ignore_nan)
                    for line in response.buffer)
                data = [
                    (None if record.empty else getattr(record, self.method), record.target)
                    for record in records]
                if len(data) == 0:
                    raise ValueError('No data')
                self.check(data)
                self.notify('normal', 'Metrics are loaded', target='loading', ntype='common')
            except Exception as e:
                self.notify(
                    self.loading_error, 'Loading error: %s' % e, target='loading', ntype='common')
            self.waiting = False
199662
def _collect_monitor_metrics(self, conn, tags):
        
        for entry in conn.entries:
            # Get metrics from monitor backend
            dn = entry.entry_dn.lower()
            if dn.endswith(self.CONNECTIONS_METRICS_DN):
                self._handle_connections_entry(entry, tags)
            elif dn.endswith(self.OPERATIONS_METRICS_DN):
                self._handle_operations_entry(entry, tags)
            elif dn.endswith(self.STATISTICS_METRICS_DN):
                self._handle_statistics_entry(entry, tags)
            elif dn.endswith(self.THREADS_METRICS_DN):
                self._handle_threads_entry(entry, tags)
            elif dn.endswith(self.TIME_METRICS_DN):
                self._handle_time_entry(entry, tags)
            elif dn.endswith(self.WAITERS_METRICS_DN):
                self._handle_waiters_entry(entry, tags)
403031
def statsd_metric(name, count, elapsed):
    """"""
    with statsd.pipeline() as pipe:
        pipe.incr(name, count)
        pipe.timing(name, int(round(1000 * elapsed)))
366933
def _psutil_config_to_stats(self, instance):
        
        process_metrics = instance.get('process_metrics', self.init_config.get('process_metrics', None))
        if not process_metrics:
            self.log.error('No metrics configured for AgentMetrics check!')
            return {}

        methods, metric_types = zip(
            *[(p['name'], p.get('type', GAUGE))
                for p in process_metrics if _is_affirmative(p.get('active'))]
        )

        names_to_metric_types = {}
        for i, m in enumerate(methods):
            names_to_metric_types[AgentMetrics._get_statistic_name_from_method(m)] = metric_types[i]

        stats = AgentMetrics._collect_internal_stats(methods)
        return stats, names_to_metric_types
371486
def do_perf_counter_check(self, instance):
        
        custom_tags = instance.get('tags', [])
        if custom_tags is None:
            custom_tags = []
        instance_key = self._conn_key(instance, self.DEFAULT_DB_KEY)
        instance_by_key = self.instances_per_type_metrics[instance_key]
        with self.open_managed_db_connections(instance, self.DEFAULT_DB_KEY):
            # if the server was down at check __init__ key could be missing.
            if instance_key not in self.instances_metrics:
                self._make_metric_list_to_collect(instance, self.custom_metrics)
            metrics_to_collect = self.instances_metrics[instance_key]

            with self.get_managed_cursor(instance, self.DEFAULT_DB_KEY) as cursor:
                simple_rows = SqlSimpleMetric.fetch_all_values(cursor, instance_by_key["SqlSimpleMetric"], self.log)
                fraction_results = SqlFractionMetric.fetch_all_values(
                    cursor, instance_by_key["SqlFractionMetric"], self.log
                )
                waitstat_rows, waitstat_cols = SqlOsWaitStat.fetch_all_values(
                    cursor, instance_by_key["SqlOsWaitStat"], self.log
                )
                vfs_rows, vfs_cols = SqlIoVirtualFileStat.fetch_all_values(
                    cursor, instance_by_key["SqlIoVirtualFileStat"], self.log
                )
                clerk_rows, clerk_cols = SqlOsMemoryClerksStat.fetch_all_values(
                    cursor, instance_by_key["SqlOsMemoryClerksStat"], self.log  # noqa: E501
                )

                for metric in metrics_to_collect:
                    try:
                        if type(metric) is SqlSimpleMetric:
                            metric.fetch_metric(cursor, simple_rows, custom_tags)
                        elif type(metric) is SqlFractionMetric or type(metric) is SqlIncrFractionMetric:
                            metric.fetch_metric(cursor, fraction_results, custom_tags)
                        elif type(metric) is SqlOsWaitStat:
                            metric.fetch_metric(cursor, waitstat_rows, waitstat_cols, custom_tags)
                        elif type(metric) is SqlIoVirtualFileStat:
                            metric.fetch_metric(cursor, vfs_rows, vfs_cols, custom_tags)
                        elif type(metric) is SqlOsMemoryClerksStat:
                            metric.fetch_metric(cursor, clerk_rows, clerk_cols, custom_tags)

                    except Exception as e:
                        self.log.warning("Could not fetch metric {} : {}".format(metric.datadog_name, e))
809879
def collect(self):
        
        lxc_metrics = ["memory.usage_in_bytes", "memory.limit_in_bytes"]
        if os.path.isdir(self.config["sys_path"]) is False:
            self.log.debug("sys_path '%s' isn't directory.",
                           self.config["sys_path"])
            return {}

        collected = {}
        for item in os.listdir(self.config["sys_path"]):
            fpath = "%s/%s" % (self.config["sys_path"], item)
            if os.path.isdir(fpath) is False:
                continue

            for lxc_metric in lxc_metrics:
                filename = "%s/%s" % (fpath, lxc_metric)
                metric_name = "%s.%s" % (
                    item.replace(".", "_"),
                    lxc_metric.replace("_in_bytes", ""))
                self.log.debug("Trying to collect from %s", filename)
                collected[metric_name] = self._read_file(filename)

        for key in collected.keys():
            if collected[key] is None:
                continue

            for unit in self.config["byte_unit"]:
                value = diamond.convertor.binary.convert(
                    collected[key],
                    oldUnit="B",
                    newUnit=unit)
                new_key = "%s_in_%ss" % (key, unit)
                self.log.debug("Publishing '%s %s'", new_key, value)
                self.publish(new_key, value, metric_type="GAUGE")