Class: Sysresources::SysResource

Inherits:
Object
  • Object
show all
Defined in:
lib/sysresources.rb

Instance Method Summary collapse

Instance Method Details

#coresObject

get cpu cores #



58
59
60
61
62
# File 'lib/sysresources.rb', line 58

def cores 
  raw =  Sys::CPU.processors
  cnt = raw[0]['cpu_cores']
  return cnt.to_i
end

#get_cpuObject

get cpu percent #



74
75
76
77
78
79
80
81
# File 'lib/sysresources.rb', line 74

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_diskObject

get disk stats #



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sysresources.rb', line 36

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_loadObject

get cpu load #



65
66
67
68
69
70
71
# File 'lib/sysresources.rb', line 65

def get_load
  info = Sys::CPU.load_avg
  load = info.join(", ")
  stat(load)
  rescue    
    $error
end

#get_procObject

get running processes #



94
95
96
97
98
99
# File 'lib/sysresources.rb', line 94

def get_proc
  proc = %x(ps -A --no-headers | wc -l)
  stat(proc)
  rescue    
    $error
end

#get_ramObject

get memory stats #



49
50
51
52
53
54
55
# File 'lib/sysresources.rb', line 49

def get_ram
  cmd = %x(free -h)
  free = cmd.split(" ")[9]
  stat(free) 
  rescue    
    $error
end

#get_statObject

get stats #



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sysresources.rb', line 110

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 " xxxxxxxxx #{name(k)}"+"#{v}"
  end
end

#get_uptObject

get uptime #



84
85
86
87
88
89
90
91
# File 'lib/sysresources.rb', line 84

def get_upt
  cmd = %x(uptime)   
  upt = cmd.split(",")[0]
  upt = upt.split("up")[1]
  stat(upt.strip())
  rescue    
    $error
end

#headerObject

get header #



102
103
104
105
106
107
# File 'lib/sysresources.rb', line 102

def header
  host = %x(hostname).strip()
  end_ = "\n\n"
  for_ = "[ #{host} #{$header} ]"
  puts "\n"+for_+end_
end

#name(name) ⇒ Object

color name #



30
31
32
33
# File 'lib/sysresources.rb', line 30

def name(name)
  name = name.to_s
  name.white    
end

#run_statsObject

print stats #



124
125
126
127
128
# File 'lib/sysresources.rb', line 124

def run_stats     
  header
  get_stat  
  exit(0) 
end

#stat(stat) ⇒ Object

color stat #



24
25
26
27
# File 'lib/sysresources.rb', line 24

def stat(stat)
  stat = stat.to_s
  stat.yellow
end