查看: 9163|回复: 19
收起左侧

获取当前树莓派状态(温度、cpu、内存、硬盘)

2013-6-17 15:52:53 | 显示全部楼层 |阅读模式
本帖最后由 hugcoday 于 2013-6-17 15:54 编辑

建立脚本
  1. sudo nano get.py
复制代码
输入

  1. import os

  2. # Return CPU temperature as a character string                                      
  3. def getCPUtemperature():
  4.     res = os.popen('vcgencmd measure_temp').readline()
  5.     return(res.replace("temp=","").replace("'C\n",""))

  6. # Return RAM information (unit=kb) in a list                                       
  7. # Index 0: total RAM                                                               
  8. # Index 1: used RAM                                                                 
  9. # Index 2: free RAM                                                                 
  10. def getRAMinfo():
  11.     p = os.popen('free')
  12.     i = 0
  13.     while 1:
  14.         i = i + 1
  15.         line = p.readline()
  16.         if i==2:
  17.             return(line.split()[1:4])

  18. # Return % of CPU used by user as a character string                                
  19. def getCPUuse():
  20.     return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\
  21. )))

  22. # Return information about disk space as a list (unit included)                     
  23. # Index 0: total disk space                                                         
  24. # Index 1: used disk space                                                         
  25. # Index 2: remaining disk space                                                     
  26. # Index 3: percentage of disk used                                                  
  27. def getDiskSpace():
  28.     p = os.popen("df -h /")
  29.     i = 0
  30.     while 1:
  31.         i = i +1
  32.         line = p.readline()
  33.         if i==2:
  34.             return(line.split()[1:5])


  35. # CPU informatiom
  36. CPU_temp = getCPUtemperature()
  37. CPU_usage = getCPUuse()

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

  44. # Disk information
  45. DISK_stats = getDiskSpace()
  46. DISK_total = DISK_stats[0]
  47. DISK_used = DISK_stats[1]
  48. DISK_perc = DISK_stats[3]

  49. if __name__ == '__main__':
  50.   print('')
  51.   print('CPU Temperature = '+CPU_temp)
  52.   print('CPU Use = '+CPU_usage)
  53.   print('')
  54.   print('RAM Total = '+str(RAM_total)+' MB')
  55.   print('RAM Used = '+str(RAM_used)+' MB')
  56.   print('RAM Free = '+str(RAM_free)+' MB')
  57.   print('')  
  58.   print('DISK Total Space = '+str(DISK_total)+'B')
  59.   print('DISK Used Space = '+str(DISK_used)+'B')
  60.   print('DISK Used Percentage = '+str(DISK_perc))
复制代码
然后执行
  1. chmod +x get.py
复制代码
执行
  1. python get.py
复制代码
输出
  1. CPU Temperature = 53.0
  2. CPU Use = 13.5

  3. RAM Total = 497.0 MB
  4. RAM Used = 116.0 MB
  5. RAM Free = 381.0 MB

  6. DISK Total Space = 3.6GB
  7. DISK Used Space = 1.8GB
  8. DISK Used Percentage = 53%
复制代码
回复

使用道具 举报

2013-6-17 18:39:50 | 显示全部楼层
好帖。顶你。
回复 支持 反对

使用道具 举报

2013-6-17 18:39:52 | 显示全部楼层
好帖。顶你。
回复 支持 反对

使用道具 举报

2013-6-18 10:50:20 | 显示全部楼层
顶楼主!
不会这个啊,求C版本。多谢!!
回复 支持 反对

使用道具 举报

2013-6-18 11:14:33 | 显示全部楼层
顶一个。python太强大了。
回复 支持 反对

使用道具 举报

2013-6-18 18:16:24 | 显示全部楼层
顶,很好用
回复 支持 反对

使用道具 举报

2013-7-2 01:19:30 | 显示全部楼层
感谢,非常好。
回复 支持 反对

使用道具 举报

2013-11-3 17:24:48 | 显示全部楼层
File "get.py", line 18
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\
                                                                                    ^
SyntaxError: unexpected character after line continuation character

显示这个。。。。
回复 支持 反对

使用道具 举报

2013-11-4 15:24:32 | 显示全部楼层
andrew211 发表于 2013-11-3 17:24
File "get.py", line 18
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline() ...

把所有的\换成\\试试看
手边没有环境,暂时不方便验证,原理上应该是\引起的
回复 支持 反对

使用道具 举报

 楼主| 2013-11-6 09:52:08 | 显示全部楼层
andrew211 发表于 2013-11-3 17:24
File "get.py", line 18
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline() ...

这个脚本 是python 2.7版本的,python 3.2下没有测试
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热点推荐

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部