获取树莓派当前状态和数据(温度、CPU、内存、硬盘)

20131215220851551-0

本篇介绍如何使用Python编写一个能输出树莓派各种状态和数据的脚本,包括CPU温度、CPU占用情况、内存占用情况和硬盘占用。

建立脚本:

sudo nano get.py

贴入如下代码:

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 getCPUuse():
    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])


# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()

# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)

# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]

if __name__ == '__main__':
    print('')
    print('CPU Temperature = '+CPU_temp)
    print('CPU Use = '+CPU_usage)
    print('')
    print('RAM Total = '+str(RAM_total)+' MB')
    print('RAM Used = '+str(RAM_used)+' MB')
    print('RAM Free = '+str(RAM_free)+' MB')
    print('')  
    print('DISK Total Space = '+str(DISK_total)+'B')
    print('DISK Used Space = '+str(DISK_used)+'B')
    print('DISK Used Percentage = '+str(DISK_perc))

然后执行:

chmod +x get.py
python get.py

预期输出结果如下:

CPU Temperature = 53.0
CPU Use = 13.5

RAM Total = 497.0 MB
RAM Used = 116.0 MB
RAM Free = 381.0 MB

DISK Total Space = 3.6GB
DISK Used Space = 1.8GB
DISK Used Percentage = 53%

4 评论

  1. cpu温度获取这样写会不会更轻巧些
    def getCPUtemperature():
    return os.popen( ‘/opt/vc/bin/vcgencmd measure_temp’ ).read()[5:9]
    或者直接读取系统自己的状态更新,省去模拟shell
    def getCPUtemperature():
    with open( “/sys/class/thermal/thermal_zone0/temp” ) as tempFile:
    res = tempFile.read()
    res=str(float(res)/1000)
    return res

  2. 同理,getRaminfo()可以写成: return os.popen(‘free|tail -n +2’).readline().split()[1:4]
    getDiskSpace()可以写成:return os.popen(‘df -h /|tail -n +2’).readline().split()[1:5]

    popen的效率不高,但是配合shell相当灵活~~

  3. cpu占用率那个几点几是百分百么?为什么我图形界面显示100%了py还是几点几

3 Trackback / Pingback

  1. 树莓派 Raspberry Pi Model B+ 入手折腾记 (4) – 搜集项目应用篇 | LT
  2. 获取树莓派当前状态和数据(温度、CPU、内存、硬盘) | 树莓派实验室 – 蚊子的网站
  3. python读取树莓派状态(CPU温度,使用率,RAM使用率,硬盘使用率) | All Stack

发表评论

你的邮件地址不会公开


*