Class: ServerMetrics::Cpu::CpuStats

Inherits:
Object
  • Object
show all
Defined in:
lib/server_metrics/collectors/cpu.rb

Overview

Helper class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCpuStats

Returns a new instance of CpuStats.



97
98
99
# File 'lib/server_metrics/collectors/cpu.rb', line 97

def initialize
  self.time = Time.now
end

Instance Attribute Details

#idleObject

Returns the value of attribute idle.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def idle
  @idle
end

#interruptsObject

Returns the value of attribute interrupts.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def interrupts
  @interrupts
end

#iowaitObject

Returns the value of attribute iowait.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def iowait
  @iowait
end

#procs_blockedObject

Returns the value of attribute procs_blocked.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def procs_blocked
  @procs_blocked
end

#procs_runningObject

Returns the value of attribute procs_running.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def procs_running
  @procs_running
end

#stealObject

Returns the value of attribute steal.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def steal
  @steal
end

#systemObject

Returns the value of attribute system.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def system
  @system
end

#timeObject

Returns the value of attribute time.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def time
  @time
end

#userObject

Returns the value of attribute user.



44
45
46
# File 'lib/server_metrics/collectors/cpu.rb', line 44

def user
  @user
end

Class Method Details

.fetchObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/server_metrics/collectors/cpu.rb', line 46

def self.fetch
  output = nil
  begin
    output = File.read("#{ServerMetrics::SystemInfo.proc_dir}/stat")
  rescue Errno::ENOENT
    # No such file or directory - /proc/stat
    # /proc/stat doesn't exist on this system.
    raise ProcStatError
  end

  data = output.lines.collect { |line| line.split }

  cpu_stats = CpuStats.new

  if cpu = data.detect { |line| line[0] == 'cpu' }
    cpu_stats.user, nice, cpu_stats.system, cpu_stats.idle, cpu_stats.iowait,
        hardirq, softirq, cpu_stats.steal = *cpu[1..-1].collect { |c| c.to_i }
    cpu_stats.user += nice
    cpu_stats.system += hardirq + softirq
  end

  if interrupts = data.detect { |line| line[0] == 'intr' }
    cpu_stats.interrupts, _ = *interrupts[1..-1].collect { |c| c.to_i }
  end

  if procs_running = data.detect { |line| line[0] == 'procs_running' }
    cpu_stats.procs_running, _ = *procs_running[1..-1].collect { |c| c.to_i }
  end

  if procs_blocked = data.detect { |line| line[0] == 'procs_blocked' }
    cpu_stats.procs_blocked, _ = *procs_blocked[1..-1].collect { |c| c.to_i }
  end

  cpu_stats
end

.from_hash(h) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/server_metrics/collectors/cpu.rb', line 82

def self.from_hash(h)
  cpu_stats= CpuStats.new
  hash = {}
  h.each { |k, v| hash[k.to_sym] = v }

  if time = hash.delete(:time)
    cpu_stats.time = Time.parse(time) rescue time
  end

  hash.each do |k, v|
    cpu_stats.send("#{k}=", v) if cpu_stats.respond_to?("#{k}=")
  end
  cpu_stats
end

Instance Method Details

#diff(other) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/server_metrics/collectors/cpu.rb', line 101

def diff(other)
  diff_user = user - other.user
  diff_system = system - other.system
  diff_idle = idle - other.idle
  diff_iowait = iowait - other.iowait

  div = diff_user + diff_system + diff_idle + diff_iowait

  if steal && other.steal && steal > 0
    diff_steal = steal - other.steal
    div += diff_steal
  end

  divo2 = div / 2

  results = {
      :user => (100.0 * diff_user + divo2) / div,
      :system => (100.0 * diff_system + divo2) / div,
      :idle => (100.0 * diff_idle + divo2) / div,
      :io_wait => (100.0 * diff_iowait + divo2) / div,
      :procs_running => self.procs_running,
      :procs_blocked => self.procs_blocked
  }

  if diff_steal && steal > 0
    results[:steal] = (100.0 * diff_steal + divo2) / div
  end

  if self.time && other.time
    diff_in_seconds = self.time.to_f - other.time.to_f

    results[:interrupts] = (self.interrupts.to_f - other.interrupts.to_f) / diff_in_seconds
  end

  results
end

#to_hObject



138
139
140
141
142
143
144
145
# File 'lib/server_metrics/collectors/cpu.rb', line 138

def to_h
  {
      :user => user, :system => system, :idle => idle, :iowait => iowait,
      :interrupts => interrupts, :procs_running => procs_running,
      :procs_blocked => procs_blocked, :time => Time.now.to_s,
      :steal => steal
  }
end