File: rtop

package info (click to toggle)
ruby-vmstat 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 348 kB
  • sloc: ruby: 1,131; ansic: 347; makefile: 3
file content (58 lines) | stat: -rwxr-xr-x 1,887 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby
$:.unshift File.expand_path('../../lib', __FILE__)

require 'vmstat'

last = Vmstat.snapshot

system 'clear'
WIDTH = 60

class String
def black;          "\033[30m#{self}\033[0m" end
def red;            "\033[31m#{self}\033[0m" end
def green;          "\033[32m#{self}\033[0m" end
def brown;          "\033[33m#{self}\033[0m" end
def blue;           "\033[34m#{self}\033[0m" end
def magenta;        "\033[35m#{self}\033[0m" end
def cyan;           "\033[36m#{self}\033[0m" end
def gray;           "\033[37m#{self}\033[0m" end
def bg_black;       "\033[40m#{self}\033[0m" end
def bg_red;         "\033[41m#{self}\033[0m" end
def bg_green;       "\033[42m#{self}\033[0m" end
def bg_brown;       "\033[43m#{self}\033[0m" end
def bg_blue;        "\033[44m#{self}\033[0m" end
def bg_magenta;     "\033[45m#{self}\033[0m" end
def bg_cyan;        "\033[46m#{self}\033[0m" end
def bg_gray;        "\033[47m#{self}\033[0m" end
def bold;           "\033[1m#{self}\033[22m" end
def reverse_color;  "\033[7m#{self}\033[27m" end
end

MB = 1024 ** 2

loop do
  current = Vmstat.snapshot
  memory = current.memory
  free = (memory.free_bytes + memory.inactive_bytes) / MB
  used = (memory.active_bytes + memory.wired_bytes) / MB

  print "\033[0;0H"
  print "Memory Free: #{free.to_s.bg_gray.black} MB"
  print "\tUsed: #{used.to_s.bg_gray.black} MB"
  print "\tWired: #{(memory.wired_bytes / MB).to_s.bg_gray.black} MB"
  puts "\tTotal: #{(memory.total_bytes / MB).to_s.bg_gray.black} MB"
  current.cpus.each_with_index do |cpu, i|
    user = cpu.user - last.cpus[i].user
    system = cpu.system - last.cpus[i].system
    idle = cpu.idle - last.cpus[i].idle

    print "CPU #{i + 1} "
    print " ".bg_red * (system.to_f / 100.0 * WIDTH)
    print " ".bg_green * (user.to_f / 100.0 * WIDTH)
    print " " * (idle.to_f / 100.0 * WIDTH)
    puts
  end
  last = current
  sleep 1
end