sys.py 文件源码

python
阅读 39 收藏 0 点赞 0 评论 0

项目:centos-base-consul 作者: zeroc0d3lab 项目源码 文件源码
def system_load(pl, format='{avg:.1f}', threshold_good=1, threshold_bad=2,
                track_cpu_count=False, short=False):
    '''Return system load average.

    Highlights using ``system_load_good``, ``system_load_bad`` and
    ``system_load_ugly`` highlighting groups, depending on the thresholds
    passed to the function.

    :param str format:
        format string, receives ``avg`` as an argument
    :param float threshold_good:
        threshold for gradient level 0: any normalized load average below this
        value will have this gradient level.
    :param float threshold_bad:
        threshold for gradient level 100: any normalized load average above this
        value will have this gradient level. Load averages between
        ``threshold_good`` and ``threshold_bad`` receive gradient level that
        indicates relative position in this interval:
        (``100 * (cur-good) / (bad-good)``).
        Note: both parameters are checked against normalized load averages.
    :param bool track_cpu_count:
        if True powerline will continuously poll the system to detect changes
        in the number of CPUs.
    :param bool short:
        if True only the sys load over last 1 minute will be displayed.

    Divider highlight group used: ``background:divider``.

    Highlight groups used: ``system_load_gradient`` (gradient) or ``system_load``.
    '''
    global cpu_count
    try:
        cpu_num = cpu_count = _cpu_count() if cpu_count is None or track_cpu_count else cpu_count
    except NotImplementedError:
        pl.warn('Unable to get CPU count: method is not implemented')
        return None
    ret = []
    for avg in os.getloadavg():
        normalized = avg / cpu_num
        if normalized < threshold_good:
            gradient_level = 0
        elif normalized < threshold_bad:
            gradient_level = (normalized - threshold_good) * 100.0 / (threshold_bad - threshold_good)
        else:
            gradient_level = 100
        ret.append({
            'contents': format.format(avg=avg),
            'highlight_groups': ['system_load_gradient', 'system_load'],
            'divider_highlight_group': 'background:divider',
            'gradient_level': gradient_level,
        })

        if short:
            return ret

    ret[0]['contents'] += ' '
    ret[1]['contents'] += ' '
    return ret
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号