def find_metric(self, datasource, sysname=None, info_object=None): hc_octets = re.compile(r'ifhc(in|out)octets', re.IGNORECASE) descr = datasource.description if hc_octets.match(descr): descr = re.sub(r'(?i)hc', '', descr) return metric_path_for_interface(info_object.netbox.sysname, info_object.ifname, descr)
def _get_target(direction): assert direction.lower() in ('in', 'out') path = metric_path_for_interface( interface.netbox.sysname, interface.ifname, 'if{0}{1}'.format(direction.capitalize(), kind)) meta = get_metric_meta(path) return meta['target'], meta.get('unit', None)
def get_link_load(sysname, ifname, time_interval): """Gets the link load of the interface, averaged over a time interval. :param sysname: The sysname of the device we're measuring from. :param ifname: An interface name. :param time_interval: A dict(start=..., end=...) describing the desired time interval in terms valid to Graphite web. :returns: An (avg_in_Mbps, avg_out_Mbps) tuple. """ in_bps = out_bps = float('nan') if sysname and ifname: targets = [metric_path_for_interface(sysname, ifname, counter) for counter in ('ifInOctets', 'ifOutOctets')] targets = [get_metric_meta(t)['target'] for t in targets] try: data = get_metric_average(targets, start=time_interval['start'], end=time_interval['end']) except GraphiteUnreachableError: _logger.error("graphite unreachable on load query for %s:%s (%r)", sysname, ifname, time_interval) return in_bps, out_bps for key, value in data.iteritems(): if 'ifInOctets' in key: in_bps = value / MEGABIT elif 'ifOutOctets' in key: out_bps = value / MEGABIT return in_bps, out_bps
def find_metric(self, datasource, sysname=None, info_object=None): hc_octets = re.compile(r'ifhc(in|out)octets', re.IGNORECASE) descr = datasource.description if hc_octets.match(descr): descr = re.sub(r'(?i)hc', '', descr) return metric_path_for_interface( info_object.netbox.sysname, info_object.ifname, descr)
def _get_target(direction): assert direction.lower() in ('in', 'out') path = metric_path_for_interface( interface.netbox.sysname, interface.ifname, 'if{0}{1}'.format(direction.capitalize(), kind) ) meta = get_metric_meta(path) return meta['target'], meta.get('unit', None)
def get_interface_data(interface): """Get ifin/outoctets for an interface using a single request""" in_bps = out_bps = None targets = [metric_path_for_interface(interface.netbox.sysname, interface.ifname, counter) for counter in (INOCTETS, OUTOCTETS)] targets = [get_metric_meta(t)['target'] for t in targets] data = get_metric_average(targets, start=TRAFFIC_TIMEPERIOD) for key, value in iteritems(data): if 'ifInOctets' in key: in_bps = value elif 'ifOutOctets' in key: out_bps = value return in_bps, out_bps
def get_multiple_link_load(items, time_interval): """ Gets the link load of the interfaces, averaged over a time interval, and adds to the load properties of the items. :param items: A dictionary of {(sysname, ifname): properties lazy_dict, ...} :param time_interval: A dict(start=..., end=...) describing the desired time interval in terms valid to Graphite web. """ target_map = {} for (sysname, ifname), properties in items.items(): if not (sysname and ifname): continue targets = [ metric_path_for_interface(sysname, ifname, counter) for counter in ('ifInOctets', 'ifOutOctets') ] targets = [get_metric_meta(t)['target'] for t in targets] target_map.update({t: properties for t in targets}) _logger.debug( "getting %s graphite traffic targets in chunks", len(target_map.keys()) ) data = {} for chunk in chunks(target_map.keys(), METRIC_CHUNK_SIZE): data.update(_get_metric_average(chunk, time_interval)) for key, value in data.items(): properties = target_map.get(key, None) if properties: if value: bps = value / MEGABIT if 'ifInOctets' in key: properties['load_in'] = bps elif 'ifOutOctets' in key: properties['load_out'] = bps else: _logger.error( "no match for key %r (%r) in data returned from graphite", key, value ) missing = set(target_map).difference(data) if missing: _logger.debug("missed %d targets in graphite response", len(missing))
def _fetch_data(interface): in_bps = out_bps = speed = None if isinstance(interface, Interface): speed = interface.speed targets = [metric_path_for_interface(interface.netbox.sysname, interface.ifname, counter) for counter in ('ifInOctets', 'ifOutOctets')] targets = [get_metric_meta(t)['target'] for t in targets] data = get_metric_average(targets, start=TRAFFIC_TIMEPERIOD) for key, value in data.iteritems(): if 'ifInOctets' in key: in_bps = value elif 'ifOutOctets' in key: out_bps = value return InterfaceLoad(in_bps, out_bps, speed)
def _fetch_data(interface): in_bps = out_bps = speed = None if isinstance(interface, Interface): speed = interface.speed targets = [ metric_path_for_interface(interface.netbox.sysname, interface.ifname, counter) for counter in ('ifInOctets', 'ifOutOctets') ] targets = [get_metric_meta(t)['target'] for t in targets] data = get_metric_average(targets, start=TRAFFIC_TIMEPERIOD) for key, value in data.iteritems(): if 'ifInOctets' in key: in_bps = value elif 'ifOutOctets' in key: out_bps = value return InterfaceLoad(in_bps, out_bps, speed)
def _make_metrics(self, stats, timestamp=None): timestamp = timestamp or time.time() hc_counters = False for row in stats.itervalues(): hc_counters = use_hc_counters(row) or hc_counters for key in LOGGED_COUNTERS: if key not in row: continue path = metric_path_for_interface( self.netbox, row['ifName'] or row['ifDescr'], key) value = row[key] if value is not None: yield (path, (timestamp, value)) if stats: if hc_counters: self._logger.debug("High Capacity counters used") else: self._logger.debug("High Capacity counters NOT used")
def _make_metrics(self, stats, timestamp=None): timestamp = timestamp or time.time() hc_counters = False for row in stats.itervalues(): hc_counters = use_hc_counters(row) or hc_counters for key in LOGGED_COUNTERS: if key not in row: continue path = metric_path_for_interface( self.netbox, row['ifName'] or row['ifDescr'], key) value = row[key] if value is not None: yield (path, (timestamp, value)) if stats: if hc_counters: self._logger.debug("High Capacity counters used") else: self._logger.debug("High Capacity counters NOT used")
def _get_traffic_counter_metrics_for(interface): return [ metric_path_for_interface(interface.netbox, interface.ifname, counter) for counter in (INOCTETS, OUTOCTETS) ]