2013-06-05 5 views
2

저는 CPU 사용량, CPU 온도, RAM 사용량, 가동 시간 및 사용 가능한 디스크 크기와 같이 Pi의 시스템을 좋아합니다. 파이썬에서이 작업을 수행하는 방법을 알고 있지만 Ruby에서는 작동하지 않습니다. 누군가 이걸 어떻게 얻을 수 있는지 말해 주시겠습니까? 내 Siriproxy에 필요하기 때문에 Ruby 여야한다고 생각합니다. 플러그인은 Ruby로 작성되었습니다.Ruby로 systeminfos를 얻는 방법?

Tankanks in advantage!

#!/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] 
+1

이 질문은 [http://stackoverflow.com/questions/690151/getting-output-of-system-calls-in-ruby](http://stackoverflow.com/questions/ 도움이 될 수 있습니다 답변 690151/get-output-of-system-in-ruby) –

답변

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