获取当前树莓派状态(温度、cpu、内存、硬盘)
本帖最后由 hugcoday 于 2013-6-17 15:54 编辑建立脚本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())
# 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))然后执行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% 好帖。顶你。 好帖。顶你。 顶楼主!
不会这个啊,求C版本。多谢!! 顶一个。python太强大了。 顶,很好用 感谢,非常好。 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
显示这个。。。。 andrew211 发表于 2013-11-3 17:24 static/image/common/back.gif
File "get.py", line 18
return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline() ...
把所有的\换成\\试试看
手边没有环境,暂时不方便验证,原理上应该是\引起的 andrew211 发表于 2013-11-3 17:24 static/image/common/back.gif
File "get.py", line 18
return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline() ...
这个脚本 是python 2.7版本的,python 3.2下没有测试
页:
[1]
2