Flower in my dev/Python

<PYTHON>[psutil]

꽃선생 2015. 8. 21. 14:37

[psutil]


-cpu_times : CPU 사용 정보


-cpu_percent : CPU 사용률(%)


-cpu_count : CPU 개수(논리)


-cpu_times_percent : CPU 사용 정보(%)



- virtual_memory : 메모리 사용 정보


- swap_memory : 스왑 사용 정보



- disk_partitions : 디스크 사용 정보


- disk_usage : 디스크 사용량




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def getLoad(self, now):
    cpu_result, mem_result, swap_result, disk_result, network_result = {}, {}, {}, {}, {}
 
    # CPU
    try:
        cpu = psutil.cpu_times_percent()
        total = cpu.user + cpu.system + cpu.idle
        if total > 100:
            cpu_result['user'= 100 - (cpu.system + cpu.idle)
        else:
            cpu_result['user'= cpu.user
 
        if total < 100:
            cpu_result['idle'= 100 - (cpu.system + cpu_result['user'])
        else:
            cpu_result['idle'= cpu.idle
 
        cpu_result['kernel'= cpu.system
        cpu_result['idle'= cpu.idle
 
    except Exception as e:
        log.err(e)
 
    # mem
    try:
        mem = psutil.virtual_memory()
        mem_result['total'= mem.total/1024.    # kbytes
        mem_result['used'= mem.used/1024.
        mem_result['free'= mem.available/1024.
    except Exception as e:
        log.err(e)
 
    # swap
    try:
        swap = psutil.swap_memory()
        swap_result['total'= swap.total/1024.    # kbytes
        swap_result['used'= swap.used/1024.
        swap_result['free'= swap.free/1024.
    except Exception as e:
        log.err(e)
 
    # disk
    for partition in psutil.disk_partitions():
        if ('removable' in partition.opts) or ('cdrom' in partition.opts):
            continue
 
        mountPoint = partition.mountpoint
        diskInfo = psutil.disk_usage(mountPoint)
        disk_result[mountPoint.replace(":""_").replace('\\''')] = (diskInfo.percent, diskInfo.total)
cs