如何使用Ruby获取systeminfos?

如何使用Ruby获取systeminfos?,ruby,raspberry-pi,system-information,Ruby,Raspberry Pi,System Information,我喜欢获取Pi的系统信息,如CPU使用率、CPU温度、RAM使用率、正常运行时间和可用磁盘大小。我知道如何在Python中实现这一点,但在Ruby中无法实现。有人能告诉我怎样才能做到这一点吗?我想它一定是Ruby,因为我的Siriproxy需要它,插件是用Ruby编写的 坦克处于优势 这是Python脚本: #!/usr/bin/env python import os, time # Return CPU temperature as a character string def get

我喜欢获取Pi的系统信息,如CPU使用率、CPU温度、RAM使用率、正常运行时间和可用磁盘大小。我知道如何在Python中实现这一点,但在Ruby中无法实现。有人能告诉我怎样才能做到这一点吗?我想它一定是Ruby,因为我的Siriproxy需要它,插件是用Ruby编写的

坦克处于优势

这是Python脚本:

#!/usr/bin/env python

import os, time


# 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_free = DISK_stats[1]
DISK_perc = DISK_stats[3]

这些方法定义应该对您有所帮助。我无法测试它们,但它们应该非常接近,如果不是现场的话

def get_cpu_temperature
  %x{vcgencmd measure_temp}.lines.first.sub(/temp=/, '').sub(/C\n/, '')
end

def get_ram_info
  %x{free}.lines.to_a[1].split[1,3]
end

def get_cpu_use
  %x{top -n1}.lines.find{ |line| /Cpu\(s\):/.match(line) }.split[1]
end

def get_disk_Space
    %x{df -h /}.lines.to_a[1].split[1,4]
end

这个问题的答案可能会对你有所帮助