コード例 #1
0
ファイル: host_select.py プロジェクト: jhaiduce/cylc-flow
def _get_metrics(hosts, metrics, data=None):
    """Retrieve host metrics using SSH if necessary.

    Note hosts will not appear in the returned results if:
    * They are not contactable.
    * There is an error in the command which returns the results.

    Args:
        hosts (list):
            List of host fqdns.
        metrics (list):
            List in the form [(function, arg1, arg2, ...), ...]
        data (dict):
            Used for logging success/fail outcomes of the form {host: {}}

    Examples:
        Command failure:
        >>> _get_metrics(['localhost'], [['elephant']])
        ({}, {'localhost': {'get_metrics': 'Command failed (exit: 1)'}})

    Returns:
        dict - {host: {(function, arg1, arg2, ...): result}}

    """
    host_stats = {}
    proc_map = {}
    if not data:
        data = {host: dict() for host in hosts}

    # Start up commands on hosts
    cmd = ['psutil']
    kwargs = {'stdin_str': json.dumps(metrics), 'capture_process': True}
    for host in hosts:
        if is_remote_host(host):
            proc_map[host] = remote_cylc_cmd(cmd, host=host, **kwargs)
        else:
            proc_map[host] = run_cmd(['cylc'] + cmd, **kwargs)

    # Collect results from commands
    while proc_map:
        for host, proc in list(proc_map.copy().items()):
            if proc.poll() is None:
                continue
            del proc_map[host]
            out, err = (f.decode() for f in proc.communicate())
            if proc.wait():
                # Command failed in verbose/debug mode
                LOG.warning('Could not evaluate "%s" (return code %d)\n%s',
                            host, proc.returncode, err)
                data[host]['get_metrics'] = (
                    f'Command failed (exit: {proc.returncode})')
            else:
                host_stats[host] = dict(
                    zip(
                        metrics,
                        # convert JSON dicts -> namedtuples
                        _deserialise(metrics, parse_dirty_json(out))))
        sleep(0.01)
    return host_stats, data
コード例 #2
0
def _psutil(metrics_json):
    metrics = parse_dirty_json(metrics_json)

    ret = [getattr(psutil, key[0])(*key[1:]) for key in metrics]

    # serialise
    for ind, item in enumerate(ret):
        if hasattr(item, '_asdict'):
            ret[ind] = item._asdict()

    return ret
コード例 #3
0
ファイル: psutil.py プロジェクト: ColemanTom/cylc
def main(parser, options):
    metrics = parse_dirty_json(sys.stdin.read())

    ret = [getattr(psutil, key[0])(*key[1:]) for key in metrics]

    # serialise
    for ind, item in enumerate(ret):
        if hasattr(item, '_asdict'):
            ret[ind] = item._asdict()

    print(json.dumps(ret))
コード例 #4
0
ファイル: psutil.py プロジェクト: oliver-sanders/cylc-flow
def _psutil(metrics_json):
    metrics = parse_dirty_json(metrics_json)
    methods = [getattr(psutil, key[0]) for key in metrics]

    try:
        ret = [method(*key[1:]) for key, method in zip(metrics, methods)]
    except Exception as exc:
        # error extracting metrics from psutil e.g:
        # * requesting a method which does not exist
        # * requesting information on a resource which does not exist
        print(exc, file=sys.stderr)
        sys.exit(2)

    # serialise
    for ind, item in enumerate(ret):
        if hasattr(item, '_asdict'):
            ret[ind] = item._asdict()
        elif hasattr(item, 'as_dict'):
            ret[ind] = item.as_dict()

    return ret