Module: Vidibus::Sysinfo::Cpu

Extended by:
Base
Defined in:
lib/vidibus/sysinfo/cpu.rb

Overview

Returns CPU utilization in percent.

Calls ‘mpstat`

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

apt-get install sysstat

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Methods included from Base

call, explain

Methods included from Helper

#round

Class Method Details

.commandObject



41
42
43
# File 'lib/vidibus/sysinfo/cpu.rb', line 41

def command
  'mpstat 1 5'
end

.explain(error) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object

user system missing



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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