Class: ServerMetrics::Cpu::CpuStats
- Inherits:
-
Object
- Object
- ServerMetrics::Cpu::CpuStats
- Defined in:
- lib/server_metrics/collectors/cpu.rb
Overview
Helper class
Instance Attribute Summary collapse
-
#idle ⇒ Object
Returns the value of attribute idle.
-
#interrupts ⇒ Object
Returns the value of attribute interrupts.
-
#iowait ⇒ Object
Returns the value of attribute iowait.
-
#procs_blocked ⇒ Object
Returns the value of attribute procs_blocked.
-
#procs_running ⇒ Object
Returns the value of attribute procs_running.
-
#steal ⇒ Object
Returns the value of attribute steal.
-
#system ⇒ Object
Returns the value of attribute system.
-
#time ⇒ Object
Returns the value of attribute time.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #diff(other) ⇒ Object
-
#initialize ⇒ CpuStats
constructor
A new instance of CpuStats.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ CpuStats
Returns a new instance of CpuStats.
88 89 90 |
# File 'lib/server_metrics/collectors/cpu.rb', line 88 def initialize self.time = Time.now end |
Instance Attribute Details
#idle ⇒ Object
Returns the value of attribute idle.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def idle @idle end |
#interrupts ⇒ Object
Returns the value of attribute interrupts.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def interrupts @interrupts end |
#iowait ⇒ Object
Returns the value of attribute iowait.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def iowait @iowait end |
#procs_blocked ⇒ Object
Returns the value of attribute procs_blocked.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def procs_blocked @procs_blocked end |
#procs_running ⇒ Object
Returns the value of attribute procs_running.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def procs_running @procs_running end |
#steal ⇒ Object
Returns the value of attribute steal.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def steal @steal end |
#system ⇒ Object
Returns the value of attribute system.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def system @system end |
#time ⇒ Object
Returns the value of attribute time.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def time @time end |
#user ⇒ Object
Returns the value of attribute user.
38 39 40 |
# File 'lib/server_metrics/collectors/cpu.rb', line 38 def user @user end |
Class Method Details
.fetch ⇒ Object
40 41 42 43 44 45 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 |
# File 'lib/server_metrics/collectors/cpu.rb', line 40 def self.fetch output = `cat /proc/stat 2>&1` if $? and !$?.success? raise ProcStatError, output end data = output.split(/\n/).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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/server_metrics/collectors/cpu.rb', line 73 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
92 93 94 95 96 97 98 99 100 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 |
# File 'lib/server_metrics/collectors/cpu.rb', line 92 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_h ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/server_metrics/collectors/cpu.rb', line 129 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 |