'Return an aggregate of CPU stats across all CPUs
When figures are not available, False is sent back.'

def check(self, agentConfig):

    def format_results(us, sy, wa, idle, st, guest=None):
        data = {'cpuUser': us, 'cpuSystem': sy, 'cpuWait': wa, 'cpuIdle': idle, 'cpuStolen': st, 'cpuGuest': guest}
        return dict(((k, v) for (k, v) in data.iteritems() if (v is not None)))
    def get_value(legend, data, name, filter_value=None):
        'Using    the    legend    and    a    metric    name,    get    the    value    or    None    from    the    data    line'
        if (name in legend):
            value = to_float(data[legend.index(name)])
            if (filter_value is not None):
                if (value > filter_value):
                    return None
            return value
        else:
            self.logger.debug(('Cannot    extract    cpu    value    %s    from    %s    (%s)' % (name, data, legend)))
            return 0.0
    try:
        if Platform.is_linux():
            (output, _, _) = get_subprocess_output(['mpstat', '1', '3'], self.logger)
            mpstat = output.splitlines()
            legend = [l for l in mpstat if (('%usr' in l) or ('%user' in l))]
            avg = [l for l in mpstat if ('Average' in l)]
            if ((len(legend) == 1) and (len(avg) == 1)):
                headers = [h for h in legend[0].split() if (h not in ('AM', 'PM'))]
                data = avg[0].split()
                cpu_metrics = {'%usr': None, '%user': None, '%nice': None, '%iowait': None, '%idle': None, '%sys': None, '%irq': None, '%soft': None, '%steal': None, '%guest': None}
                for cpu_m in cpu_metrics:
                    cpu_metrics[cpu_m] = get_value(headers, data, cpu_m, filter_value=110)
                if any([(v is None) for v in cpu_metrics.values()]):
                    self.logger.warning(('Invalid    mpstat    data:    %s' % data))
                cpu_user = ((cpu_metrics['%usr'] + cpu_metrics['%user']) + cpu_metrics['%nice'])
                cpu_system = ((cpu_metrics['%sys'] + cpu_metrics['%irq']) + cpu_metrics['%soft'])
                cpu_wait = cpu_metrics['%iowait']
                cpu_idle = cpu_metrics['%idle']
                cpu_stolen = cpu_metrics['%steal']
                cpu_guest = cpu_metrics['%guest']
                return format_results(cpu_user, cpu_system, cpu_wait, cpu_idle, cpu_stolen, cpu_guest)
            else:
                return False
        elif (sys.platform == 'darwin'):
            (iostats, _, _) = get_subprocess_output(['iostat', '-C', '-w', '3', '-c', '2'], self.logger)
            lines = [l for l in iostats.splitlines() if (len(l) > 0)]
            legend = [l for l in lines if ('us' in l)]
            if (len(legend) == 1):
                headers = legend[0].split()
                data = lines[(-1)].split()
                cpu_user = get_value(headers, data, 'us')
                cpu_sys = get_value(headers, data, 'sy')
                cpu_wait = 0
                cpu_idle = get_value(headers, data, 'id')
                cpu_st = 0
                return format_results(cpu_user, cpu_sys, cpu_wait, cpu_idle, cpu_st)
            else:
                self.logger.warn(('Expected    to    get    at    least    4    lines    of    data    from    iostat    instead    of    just    ' + str(iostats[:max(80, len(iostats))])))
                return False
        elif sys.platform.startswith('freebsd'):
            (iostats, _, _) = get_subprocess_output(['iostat', '-w', '3', '-c', '2'], self.logger)
            lines = [l for l in iostats.splitlines() if (len(l) > 0)]
            legend = [l for l in lines if ('us' in l)]
            if (len(legend) == 1):
                headers = legend[0].split()
                data = lines[(-1)].split()
                cpu_user = get_value(headers, data, 'us')
                cpu_nice = get_value(headers, data, 'ni')
                cpu_sys = get_value(headers, data, 'sy')
                cpu_intr = get_value(headers, data, 'in')
                cpu_wait = 0
                cpu_idle = get_value(headers, data, 'id')
                cpu_stol = 0
                return format_results((cpu_user + cpu_nice), (cpu_sys + cpu_intr), cpu_wait, cpu_idle, cpu_stol)
            else:
                self.logger.warn(('Expected    to    get    at    least    4    lines    of    data    from    iostat    instead    of    just    ' + str(iostats[:max(80, len(iostats))])))
                return False
        elif (sys.platform == 'sunos5'):
            (output, _, _) = get_subprocess_output(['mpstat', '-aq', '1', '2'], self.logger)
            mpstat = output.splitlines()
            lines = [l for l in mpstat if (len(l) > 0)]
            lines = lines[(len(lines) / 2):]
            legend = [l for l in lines if ('SET' in l)]
            assert (len(legend) == 1)
            if (len(legend) == 1):
                headers = legend[0].split()
                d_lines = [l for l in lines if ('SET' not in l)]
                user = [get_value(headers, l.split(), 'usr') for l in d_lines]
                kern = [get_value(headers, l.split(), 'sys') for l in d_lines]
                wait = [get_value(headers, l.split(), 'wt') for l in d_lines]
                idle = [get_value(headers, l.split(), 'idl') for l in d_lines]
                size = [get_value(headers, l.split(), 'sze') for l in d_lines]
                count = sum(size)
                rel_size = [(s / count) for s in size]
                dot = (lambda v1, v2: reduce(operator.add, map(operator.mul, v1, v2)))
                return format_results(dot(user, rel_size), dot(kern, rel_size), dot(wait, rel_size), dot(idle, rel_size), 0.0)
        else:
            self.logger.warn('CPUStats:    unsupported    platform')
            return False
    except Exception:
        self.logger.exception('Cannot    compute    CPU    stats')
        return False
