Class: Remon::Metrics::System

Inherits:
Object
  • Object
show all
Defined in:
lib/remon/metrics/system.rb

Defined Under Namespace

Classes: CpuStat

Instance Method Summary collapse

Instance Method Details

#cpu_statObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/remon/metrics/system.rb', line 35

def cpu_stat
  stat = File.open('/proc/stat', 'r') { |f| f.readline }
  @stat_regex ||= /^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
  mdata = stat.match(@stat_regex)
  if mdata
    user, nice, system = [mdata[1], mdata[2], mdata[3]].map { |i| i.to_i }
    cpu = CpuStat.new
    cpu.used = user + nice + system
    cpu.idle = mdata[4].to_i
    cpu.iowait = mdata[5].to_i
    return cpu
  else
    return nil
  end
end

#cpu_usage(old_cpu, new_cpu) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/remon/metrics/system.rb', line 51

def cpu_usage(old_cpu, new_cpu)
  used = new_cpu.used - old_cpu.used
  idle = new_cpu.idle - old_cpu.idle
  iowait = new_cpu.iowait - old_cpu.iowait
  total = used + idle + iowait
  used_frac = (used.to_f / total).round 4
  iowait_frac = (iowait.to_f / total).round 4
  [used_frac, iowait_frac]
end

#loadavgObject



23
24
25
# File 'lib/remon/metrics/system.rb', line 23

def loadavg
  File.open("/proc/loadavg") { |f| f.gets(" ").to_f }
end

#loadavg_normalizedObject



27
28
29
# File 'lib/remon/metrics/system.rb', line 27

def loadavg_normalized
  (loadavg / Sysinfo.normalized_cores).round 2
end

#memoryObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/remon/metrics/system.rb', line 10

def memory
  m = {}
  IO.foreach("/proc/meminfo").first(6).each do |line|
    split = line.split(":")
    key = split[0].to_sym
    value = split[1].split[0]
    m[key] = value
  end
  free = m[:MemAvailable].to_i || (m[:MemFree].to_i + m[:Buffers].to_i + m[:Cached].to_i)
  total = m[:MemTotal].to_i
  used = (1 - (free.to_f / total)).round(4)
end

#uptimeObject



31
32
33
# File 'lib/remon/metrics/system.rb', line 31

def uptime
  File.open('/proc/uptime') { |f| f.gets(" ").to_f }
end