module Vidibus::Sysinfo::Cpu

Returns CPU utilization in percent.

Calls `mpstat`

mpstat is part of the “systat” tools for Linux. Installation on Debian:

apt-get install sysstat

Public Class Methods

command() click to toggle source
# File lib/vidibus/sysinfo/cpu.rb, line 41
def command
  'mpstat 1 5'
end
explain(error) click to toggle source
# File lib/vidibus/sysinfo/cpu.rb, line 63
def explain(error)
  if error.match("mpstat: command not found")
    return "mpstat is not installed. On Debian, you can install it with 'apt-get install sysstat'"
  end
end
parse(output) click to toggle source

user system missing

# File lib/vidibus/sysinfo/cpu.rb, line 46
def parse(output)
  lines = output.split(/\r?\n/)
  values = lines[-1].scan(/([\d\.]+)/)
  if values.any?
    values.flatten!
    data = {}
    labels = lines[2].scan(/%([^\s]+)/).flatten
    labels.each_with_index do |label, index|
      key = Result::KEYS[label.to_sym]
      next unless key
      data[key] = values[index].to_f
    end
    data[:used] = round(100.0 - data[:idle])
    Result.new(data)
  end
end