class Polars::Ubuntu::Stat

Proc Stat Class

Public Class Methods

new(proc_stat = "/proc/stat") click to toggle source
# File lib/polars/ubuntu/stat.rb, line 25
def initialize(proc_stat = "/proc/stat")
  @hash = get_proc_stat_info(proc_stat)
end

Public Instance Methods

proc_stat() click to toggle source
# File lib/polars/ubuntu/stat.rb, line 29
def proc_stat
  @hash
end

Private Instance Methods

get_proc_stat_info(proc_stat) click to toggle source

Parse the information out of /proc/stat and assign keys and values to a hash that can be accessed via the Forwardable module.

# File lib/polars/ubuntu/stat.rb, line 37
def get_proc_stat_info(proc_stat)
  hash = {}

  File.readlines(proc_stat).each do |line|
    info = line.split

    next if info.empty?

    hash[info.first.to_sym] = if info.first =~ /^cpu/i
      {
        user: info[1].to_i,
        nice: info[2].to_i,
        system: info[3].to_i,
        idle: info[4].to_i,
        iowait: info[5].to_i,
        irq: info[6].to_i,
        softirq: info[7].to_i,
        steal: info[8].to_i,
        guest: info[9].to_i,
        guest_nice: info[10].to_i,
      }
    elsif info.size > 2
      info[1..].map { |e| e.to_i }
    else
      info[1].to_i
    end
  end
  hash
end