Example #1
0
File: db.py Project: plan1230/nav
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
Example #2
0
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
Example #3
0
    def _get_cpu_utilization(self, mib):
        utilization = yield mib.get_cpu_utilization()
        timestamp = time.time()
        metrics = []

        if utilization:
            self._logger.debug("Found CPU utilization from %s: %s",
                               mib.mib['moduleName'], utilization)
            for cpuname, value in utilization.items():
                path = metric_path_for_cpu_utilization(self.netbox, cpuname)
                metrics.append((path, (timestamp, value)))
        defer.returnValue(metrics)