def find_metric(self, datasource, sysname=None, info_object=None): descr = datasource.description if descr in self.cpus: metric = metric_path_for_cpu_load(sysname, 'cpu', self.get_interval(descr)) elif descr in self.memories: metric = metric_prefix_for_memory(sysname, descr) elif descr in self.bandwidths: if descr.endswith(('Max', 'max')): if descr.startswith('c5000'): metric = metric_path_for_bandwith_peak(sysname, True) else: metric = metric_path_for_bandwith_peak(sysname, False) else: if descr.startswith('c5000'): metric = metric_path_for_bandwith(sysname, True) else: metric = metric_path_for_bandwith(sysname, False) elif descr == 'sysUpTime': metric = metric_path_for_sysuptime(sysname) else: _logger.info('Could not find metric for %s' % descr) metric = None return metric
def get_multiple_cpu_load(items, time_interval): """ Gets the CPU load of netboxes, averaged over a time interval, and adds to the load properties of the items. :param items: A dictionary of {sysname: properties lazy_dict, ...} :param time_interval: A dict(start=..., end=...) describing the desired time interval in terms valid to Graphite web. """ target_map = { escape_metric_name(sysname): netbox for sysname, netbox in iteritems(items) } targets = [] for sysname, netbox in iteritems(items): if not sysname: continue targets.extend([ 'highestMax(%s,1)' % path for path in (metric_path_for_cpu_load(sysname, '*', interval=5), metric_path_for_cpu_utilization(sysname, '*')) ]) _logger.debug("getting %s graphite cpu targets in chunks", len(targets)) data = {} for chunk in chunks(targets, METRIC_CHUNK_SIZE): data.update(_get_metric_average(chunk, time_interval)) for key, value in iteritems(data): for sysname, netbox in iteritems(target_map): if sysname in key: if not is_nan(value): netbox['load'] = value break
def get_cpu_load(sysname, time_interval): """Returns the average 5 minute CPU load of sysname. Question is, of _which_ CPU? Let's just get the one that has the highest maximum value. :param sysname: The sysname of the device whose CPU load we're to get. :param time_interval: A dict(start=..., end=...) describing the desired time interval in terms valid to Graphite web. :returns: A floating number representation of the load between 0 and 100.0 (possibly higher in some multi-CPU settings). """ data = None for path in ( metric_path_for_cpu_load(sysname, '*', interval=5), metric_path_for_cpu_utilization(sysname, '*') ): target = 'highestMax(%s,1)' % path try: data = get_metric_average(target, start=time_interval['start'], end=time_interval['end'], ignore_unknown=True) if data: break except Exception: data = None result = data.values()[0] if data else float('nan') _logger.debug("get_cpu_load(%r, %r) == %r", sysname, time_interval, result) return result
def find_metric(self, datasource, sysname=None, info_object=None): descr = datasource.description if descr in self.cpus: metric = metric_path_for_cpu_load( sysname, 'cpu', self.get_interval(descr)) elif descr in self.memories: metric = metric_prefix_for_memory(sysname, descr) elif descr in self.bandwidths: if descr.endswith(('Max', 'max')): if descr.startswith('c5000'): metric = metric_path_for_bandwith_peak( sysname, True) else: metric = metric_path_for_bandwith_peak( sysname, False) else: if descr.startswith('c5000'): metric = metric_path_for_bandwith( sysname, True) else: metric = metric_path_for_bandwith( sysname, False) elif descr == 'sysUpTime': metric = metric_path_for_sysuptime(sysname) else: _logger.info('Could not find metric for %s' % descr) metric = None return metric
def _get_cpu_loadavg(self, mib): load = yield mib.get_cpu_loadavg() timestamp = time.time() metrics = [] if load: self._logger.debug("Found CPU loadavg from %s: %s", mib.mib['moduleName'], load) for cpuname, loadlist in load.items(): for interval, value in loadlist: path = metric_path_for_cpu_load(self.netbox, cpuname, interval) metrics.append((path, (timestamp, value))) defer.returnValue(metrics)