2

I like to get my Pi's systeminfos like CPU usage, CPU temp, RAM usage, uptime and the available disk size. I know how to do this in Python, but it wont work in Ruby. Can someone please tell me how I can achieve this? I think it must be Ruby, because I need it for my Siriproxy and the plugin is written in Ruby.

Tnanks in advantage!

This is the Python script:

#!/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]
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • 1
    This questions answer may help you [http://stackoverflow.com/questions/690151/getting-output-of-system-calls-in-ruby](http://stackoverflow.com/questions/690151/getting-output-of-system-calls-in-ruby) – Dan Garman Jun 05 '13 at 15:34

1 Answers1

3

These method definitions should help you. I'm not in a position to test them, but they should be pretty close if not spot on.

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
Borodin
  • 126,100
  • 9
  • 70
  • 144