ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <PYTHON>시스템 정보 가져오기(CPU, RAM, DISK)
    Flower in my dev/Python 2015. 4. 21. 14:03
    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
    import time, random
    import psutil
    import os
     
    # Return CPU temperature as a character string
    def getCPUTemperature():
        res = os.popen('vcgencmd measure_temp').readline()
        return(res.replace("temp=","").replace("'C\n",""))
     
    # Return RAM information (unit=kb) in a list
    # Index 0: total RAM
    # Index 1: used RAM
    # Index 2: free RAM
    def getRAMInfo():
        p = os.popen('free')
        i = 0
        while 1:
            i = i + 1
            line = p.readline()
            if i==2:
                return(line.split()[1:4])
     
    # Return % of CPU used by user as a character string
    def getCPUUsage():
        cpu = psutil.cpu_percent(interval=1)
        return cpu*10
        #return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\
        #        )))
     
    # Return information about disk space as a list (unit included)
    # Index 0: total disk space
    # Index 1: used disk space
    # Index 2: remaining disk space
    # Index 3: percentage of disk used
    def getDiskSpace():
        p = os.popen("df -h /")
        i = 0
        while 1:
            i = i +1
            line = p.readline()
            if i==2:
                return(line.split()[1:5])
    cs

     

    psutil 모듈을 설치하여야 동작한다.

     

    pip install psutil

    'Flower in my dev > Python' 카테고리의 다른 글

    <PYTHON> TCP_Echo_Server  (0) 2015.04.21
    <PYTHON> UDP_Echo_Server  (0) 2015.04.21
    <PYTHON>[loop]  (0) 2015.04.14
    <PYTHON>[리스트, 튜플, 사전]  (0) 2015.04.13
    <PYTHON>[내장함수]  (0) 2015.04.13

    댓글

Designed by Tistory.