Sha256: ee3fd96d401d59cb4564e4bdf5f732f643575d1fcbaef2adef5005ccb30b1964

Contents?: true

Size: 1.15 KB

Versions: 33

Compression:

Stored size: 1.15 KB

Contents

class Riemann::Babbler::Cpu < Riemann::Babbler

  def desc
    "usage\n\n#{shell('ps -eo pcpu,pid,cmd | sort -nrb -k1 | head -10').chomp}"
  end

  def init
    @old_cpu = Hash.new
  end

  def collect
    array = Array.new
    description = desc
    File.read('/proc/stat').split("\n").each do |cpu_line|

      # проверяем есть строчка /cpu\d+/ или /cpu / и сграбливаем это в переменную
      cpu_number = cpu_line.scan(/cpu(\d+|\s)\s+/)
      next if cpu_number.empty?
      cpu_number = cpu_number[0][0] == " " ? "_total" : cpu_number[0][0] 

      # работаем со строкой
      cpu_line[/cpu(\d+|\s)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/]
      _, u2, n2, s2, i2 = [$1, $2, $3, $4, $5].map { |e| e.to_i }

      unless @old_cpu[cpu_number].nil?
        u1, n1, s1, i1 = @old_cpu[cpu_number]
        used = (u2+n2+s2) - (u1+n1+s1)
        total = used + i2-i1
        fraction = used.to_f / total
      end

      @old_cpu[cpu_number] = [u2, n2, s2, i2]
      array << { :service => plugin.service + " cpu#{cpu_number}", :metric => fraction, :description => description } if fraction
    end
    array
  end

end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
riemann-babbler-0.8.2 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.8.1 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.8.0 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.9 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.8 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.7 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.6 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.5 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.4 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.3 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.2 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.1 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.7.0 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.9 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.8 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.7 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.6 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.5 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.4 lib/riemann/babbler/plugins/cpu.rb
riemann-babbler-0.6.3 lib/riemann/babbler/plugins/cpu.rb