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.
85 86 87 |
# File 'lib/server_metrics/collectors/cpu.rb', line 85 def initialize self.time = Time.now end |
Instance Attribute Details
#idle ⇒ Object
Returns the value of attribute idle.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def idle @idle end |
#interrupts ⇒ Object
Returns the value of attribute interrupts.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def interrupts @interrupts end |
#iowait ⇒ Object
Returns the value of attribute iowait.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def iowait @iowait end |
#procs_blocked ⇒ Object
Returns the value of attribute procs_blocked.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def procs_blocked @procs_blocked end |
#procs_running ⇒ Object
Returns the value of attribute procs_running.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def procs_running @procs_running end |
#steal ⇒ Object
Returns the value of attribute steal.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def steal @steal end |
#system ⇒ Object
Returns the value of attribute system.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def system @system end |
#time ⇒ Object
Returns the value of attribute time.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def time @time end |
#user ⇒ Object
Returns the value of attribute user.
35 36 37 |
# File 'lib/server_metrics/collectors/cpu.rb', line 35 def user @user end |
Class Method Details
.fetch ⇒ Object
37 38 39 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 |
# File 'lib/server_metrics/collectors/cpu.rb', line 37 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/server_metrics/collectors/cpu.rb', line 70 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
89 90 91 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 |
# File 'lib/server_metrics/collectors/cpu.rb', line 89 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
126 127 128 129 130 131 132 133 |
# File 'lib/server_metrics/collectors/cpu.rb', line 126 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 |