class ServState::UsageManager
Constants
- KB_IN_GB
- SECONDS_IN_DAY
- SECONDS_IN_HOUR
Public Class Methods
new()
click to toggle source
# File lib/ServState/usage_manager.rb, line 110 def initialize @old = { traffic: Hash.new, traffic_time: Time.now, } @cpu = CpuUsage.new @traffic = NetworkUsage.new end
Public Instance Methods
cpu_stat()
click to toggle source
# File lib/ServState/usage_manager.rb, line 95 def cpu_stat @cpu.usage end
disk_usage()
click to toggle source
# File lib/ServState/usage_manager.rb, line 42 def disk_usage usage = Hash.new # grep lines with physical devices df_output = `df`.split("\n").grep(/^\/dev/) # Filesystem 1K-blocks Used Available Use% Mounted on # /dev/sda5 36462184 11383116 23226860 33% / df_output.each do |line| splited = line.split device = splited[0] # last of array couse of spaces in mount point mount_point = splited[5..-1].join(' ').chomp total, used, available = splited[1..3].map do |x| (x.to_f / KB_IN_GB).round(2) end in_per = splited[4].chop.to_i # in % usage[device] = [in_per, total, used, available, mount_point] end usage end
hostname()
click to toggle source
# File lib/ServState/usage_manager.rb, line 8 def hostname `hostname`.chomp end
interfaces()
click to toggle source
this function use ifconfig. TODO: rewrite without using ifconfig
# File lib/ServState/usage_manager.rb, line 14 def interfaces ip_output = `ip addr` ip_lines = ip_output.split("\n").grep(/([0-9]:\ )|(inet\ )/) faces = Hash.new ip_lines.each_cons(2) do |lines| if lines[0] =~ /[0-9]:\ / name = lines[0].split[1].chop ip = lines[1].scan(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/).first faces[name] = ip end end faces # { name: ip, name2: ip2, ... } end
network_speed()
click to toggle source
# File lib/ServState/usage_manager.rb, line 99 def network_speed @traffic.speeds end
ram_usage()
click to toggle source
returns hash = [total, used]
hash[mem_type_per] = used in %
# File lib/ServState/usage_manager.rb, line 72 def ram_usage usage = Hash.new free_output = `free`.split("\n") total_ram = (free_output[1].split[1].to_f / KB_IN_GB).round(2) used_ram = (free_output[2].split[2].to_f / KB_IN_GB).round(2) total_swap = (free_output[3].split[1].to_f / KB_IN_GB).round(2) used_swap = (free_output[3].split[2].to_f / KB_IN_GB).round(2) ram_per = (used_ram / total_ram).round(2) swap_per = total_swap > 0 ? (used_swap / total_swap).round(2) : 0 usage[:ram] = [total_ram, used_ram] usage[:swap] = [total_swap, used_swap] usage[:ram_per] = (100 * ram_per).to_i usage[:swap_per] = (100 * swap_per).to_i usage end
update()
click to toggle source
# File lib/ServState/usage_manager.rb, line 103 def update @cpu.update @traffic.update end
uptime()
click to toggle source
# File lib/ServState/usage_manager.rb, line 32 def uptime upt = Hash.new upt[:total_sec] = IO.read('/proc/uptime').split.first.to_i # got seconds upt[:days] = upt[:total_sec] / SECONDS_IN_DAY # seconds in day upt[:hours] = (upt[:total_sec] - upt[:days] * SECONDS_IN_DAY) / SECONDS_IN_HOUR # seconds in hour upt[:minutes] = (upt[:total_sec] - upt[:days] * SECONDS_IN_DAY - upt[:hours] * SECONDS_IN_HOUR) / 60 upt[:seconds] = upt[:total_sec] - upt[:days] * SECONDS_IN_DAY - upt[:hours] * SECONDS_IN_HOUR - upt[:minutes] * 60 upt end