计算CPU 百分比 - 基于openstack kvm 虚拟机采集片段代码分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def get_vm_cpu_rate(self, uuid):
    """
    get cpu rate
    100 * diff_vm_cpu_time / (diff_sys_cpu_time * 1 * 1e9)
    return cpurate%
    """
    result = 0
    vm_info = self.vms_info.get(uuid, None)
    vm_info_before = self.vms_info_before.get(uuid, None)
    if not vm_info or not vm_info_before:
        return result
    info = vm_info.get("cpu_mem_state_info"None)
    info_before = vm_info_before.get("cpu_mem_state_info"None)
    cpu_time = info[-1]
    cpu_time_before = info_before[-1]
    last = self.vms_info_timestamp
    before = self.vms_info_before_timestamp
    if cpu_time and cpu_time_before:
        result = 100 * abs(cpu_time_before - cpu_time) / \
                (abs(last - before) * 1 * 1e9)
    return round(result, 2)


上一篇:小程序上云,有点猛


下一篇:自定义Unity 容器的扩展 --- Unity Application Block Event Broker