凌风清羽 发表于 2016-1-9 00:56:56

【树莓派教程 】——获取树莓派当前状态和数据

本帖最后由 凌风清羽 于 2016-1-25 20:02 编辑

vi 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())

# 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())


# 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) / 1000,1)
RAM_used = round(int(RAM_stats) / 1000,1)
RAM_free = round(int(RAM_stats) / 1000,1)

# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats
DISK_used = DISK_stats
DISK_perc = DISK_stats

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))
然后执行python get.py
输出如图

测试了一下树莓派再有风扇和没有风扇时候的的温度,差距还是比较大啊,为了保护我们的小派还是装个风扇吧~~


(明天上图吧,网速实在不敢恭维~~是不是DF该升级服务器了呢,哈哈~~)




dsweiliang 发表于 2016-1-9 08:00:31

获取的是树莓派的数据?

大连林海 发表于 2016-1-9 08:53:44

很牛掰

凌风清羽 发表于 2016-1-9 12:59:34

dsweiliang 发表于 2016-1-9 08:00
获取的是树莓派的数据?

对的,树莓派的实时数据~~

凌风清羽 发表于 2016-1-9 12:59:51

大连林海 发表于 2016-1-9 08:53
很牛掰

新手请教~~~{:5_171:}

大连林海 发表于 2016-1-9 14:08:29

凌风清羽 发表于 2016-1-9 12:59
新手请教~~~

额 我是新手

孙毅 发表于 2016-1-9 16:41:21

大连林海 发表于 2016-1-9 14:08
额 我是新手

我也是新手。。。

大连林海 发表于 2016-1-9 20:42:42

孙毅 发表于 2016-1-9 16:41
我也是新手。。。

我也是新手。。。我也是新手。。。

丄帝De咗臂 发表于 2016-1-10 20:03:11

有木有用过pcduino,安卓系统的

凌风清羽 发表于 2016-1-10 20:43:56

丄帝De咗臂 发表于 2016-1-10 20:03
有木有用过pcduino,安卓系统的

木有:dizzy:,说rasp也可以装安卓啦

吹口琴的钢铁侠 发表于 2016-1-14 15:39:23

不能只贴代码啊= = 加点注释也行

凌风清羽 发表于 2016-1-14 17:13:32

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,

凌风清羽 发表于 2016-1-14 17:13:33

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,

凌风清羽 发表于 2016-1-14 17:13:34

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,

凌风清羽 发表于 2016-1-14 17:13:36

吹口琴的钢铁侠 发表于 2016-1-14 15:39
不能只贴代码啊= = 加点注释也行

恩恩,这几天有空就开始改进,
页: [1]
查看完整版本: 【树莓派教程 】——获取树莓派当前状态和数据