Class: Sysresources::SysResource
- Inherits:
-
Object
- Object
- Sysresources::SysResource
- Defined in:
- lib/sysresources.rb
Instance Method Summary collapse
-
#cores ⇒ Object
get cpu cores #.
-
#get_cpu ⇒ Object
get cpu percent #.
-
#get_disk ⇒ Object
get disk stats #.
-
#get_load ⇒ Object
get cpu load #.
-
#get_proc ⇒ Object
get running processes #.
-
#get_ram ⇒ Object
get memory stats #.
-
#get_stat ⇒ Object
get stats #.
-
#get_upt ⇒ Object
get uptime #.
-
#header ⇒ Object
get header #.
-
#name(name) ⇒ Object
color name #.
-
#print_cpu ⇒ Object
prints cpu stat #.
-
#print_disk ⇒ Object
prints disk stat #.
-
#print_proc ⇒ Object
prints proc stat #.
-
#print_ram ⇒ Object
prints ram stat #.
-
#print_upt ⇒ Object
print upt stat #.
-
#run_stats ⇒ Object
print stats #.
-
#stat(stat) ⇒ Object
color stat #.
Instance Method Details
#cores ⇒ Object
get cpu cores #
65 66 67 68 69 |
# File 'lib/sysresources.rb', line 65 def cores raw = Sys::CPU.processors cnt = raw[0]['cpu_cores'] return cnt.to_i end |
#get_cpu ⇒ Object
get cpu percent #
81 82 83 84 85 86 87 88 |
# 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 ⇒ Object
get disk stats #
43 44 45 46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
get cpu load #
72 73 74 75 76 77 78 |
# File 'lib/sysresources.rb', line 72 def get_load info = Sys::CPU.load_avg load = info.join(", ") stat(load) rescue $error end |
#get_proc ⇒ Object
get running processes #
101 102 103 104 105 106 |
# File 'lib/sysresources.rb', line 101 def get_proc proc = %x(ps -A --no-headers | wc -l) stat(proc) rescue $error end |
#get_ram ⇒ Object
get memory stats #
56 57 58 59 60 61 62 |
# File 'lib/sysresources.rb', line 56 def get_ram cmd = %x(free -h) free = cmd.split(" ")[9] stat(free) rescue $error end |
#get_stat ⇒ Object
get stats #
117 118 119 120 121 122 123 124 125 126 127 128 |
# 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 ⇒ Object
get uptime #
91 92 93 94 95 96 97 98 |
# 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 ⇒ Object
get header #
109 110 111 112 113 114 |
# 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) ⇒ Object
color name #
37 38 39 40 |
# File 'lib/sysresources.rb', line 37 def name(name) name = name.to_s name.white end |
#print_cpu ⇒ Object
prints cpu stat #
131 132 133 134 |
# File 'lib/sysresources.rb', line 131 def print_cpu puts name($CPU) + get_cpu end |
#print_disk ⇒ Object
prints disk stat #
137 138 139 140 |
# File 'lib/sysresources.rb', line 137 def print_disk puts name($DSK) + get_disk.to_s end |
#print_proc ⇒ Object
prints proc stat #
155 156 157 158 |
# File 'lib/sysresources.rb', line 155 def print_proc puts name($PRC) + get_proc.delete("\n") end |
#print_ram ⇒ Object
prints ram stat #
143 144 145 146 |
# File 'lib/sysresources.rb', line 143 def print_ram puts name($RAM) + get_ram end |
#print_upt ⇒ Object
print upt stat #
149 150 151 152 |
# File 'lib/sysresources.rb', line 149 def print_upt puts name($UPT) + get_upt end |
#run_stats ⇒ Object
print stats #
161 162 163 164 165 |
# File 'lib/sysresources.rb', line 161 def run_stats header get_stat exit(0) end |
#stat(stat) ⇒ Object
color stat #
31 32 33 34 |
# File 'lib/sysresources.rb', line 31 def stat(stat) stat = stat.to_s stat.yellow end |