class Sysresources::SysResource

Public Instance Methods

cores() click to toggle source

get cpu cores #

# File lib/sysresources.rb, line 65
def cores 
  raw =  Sys::CPU.processors
  cnt = raw[0]['cpu_cores']
  return cnt.to_i
end
get_cpu() click to toggle source

get cpu percent #

# File lib/sysresources.rb, line 81
def get_cpu
  cmd = %x(ps -eo "%C").split("\n")
  sum = cmd.map(&:to_f).reduce(0,:+)
  cpu = (sum/cores).round(2)
  stat(cpu) + "%"
  rescue    
    $error
end
get_disk() click to toggle source

get disk stats #

# File lib/sysresources.rb, line 43
def get_disk
  multi = 1024
  dirc = File.exist?($root) ? $root : "/"
  root = $disk.stat(dirc)
  size = root.block_size
  avil = root.blocks_available
  gb = size * avil /multi/multi/multi
  stat("#{gb}G")
  rescue    
    $error
end
get_load() click to toggle source

get cpu load #

# File lib/sysresources.rb, line 72
def get_load
  info = Sys::CPU.load_avg
  load = info.join(", ")
  stat(load)
  rescue    
    $error
end
get_proc() click to toggle source

get running processes #

# File lib/sysresources.rb, line 101
def get_proc
  proc = %x(ps -A --no-headers | wc -l)
  stat(proc)
  rescue    
    $error
end
get_ram() click to toggle source

get memory stats #

# File lib/sysresources.rb, line 56
def get_ram
  cmd = %x(free -h)
  free = cmd.split(" ")[9]
  stat(free) 
  rescue    
    $error
end
get_stat() click to toggle source

get stats #

# File lib/sysresources.rb, line 117
def get_stat
  fromsys = {
    "disk:"    => get_disk, 
    "ram:"     => get_ram, 
    "cpu:"     => get_cpu, 
    "uptime:"  => get_upt, 
    "process:" => get_proc
  }
  fromsys.each do |k,v|
    puts " --------- #{name(k)}"+"#{v}"
  end
end
get_upt() click to toggle source

get uptime #

# File lib/sysresources.rb, line 91
def get_upt
  cmd = %x(uptime)   
  upt = cmd.split(",")[0]
  upt = upt.split("up")[1]
  stat(upt.strip())
  rescue    
    $error
end
header() click to toggle source

get header #

# File lib/sysresources.rb, line 109
def header
  host = %x(hostname).strip()
  end_ = "\n\n"
  for_ = "* #{host} #{$header} *"
  puts "\n"+for_+end_
end
name(name) click to toggle source

color name #

# File lib/sysresources.rb, line 37
def name(name)
  name = name.to_s
  name.white    
end
print_cpu() click to toggle source

prints cpu stat #

print_disk() click to toggle source

prints disk stat #

print_proc() click to toggle source

prints proc stat #

print_ram() click to toggle source

prints ram stat #

print_upt() click to toggle source

print upt stat #

run_stats() click to toggle source

print stats #

# File lib/sysresources.rb, line 161
def run_stats     
  header
  get_stat  
  exit(0) 
end
stat(stat) click to toggle source

color stat #

# File lib/sysresources.rb, line 31
def stat(stat)
  stat = stat.to_s
  stat.yellow
end