Sha256: cb9fc47932ea8c7d1347adb3f8d165bfef39902d150c184c3d4944a70464def9

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

#! /usr/bin/env ruby
#
#   cpu-metrics
#
# DESCRIPTION:
#
# OUTPUT:
#   metric data
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#
# USAGE:
#
# NOTES:
#
# LICENSE:
#   Copyright 2012 Sonian, Inc <chefs@sonian.net>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#
require 'sensu-plugin/metric/cli'
require 'socket'

#
# CPU Graphite
#
class CpuGraphite < Sensu::Plugin::Metric::CLI::Graphite
  option :scheme,
         description: 'Metric naming scheme, text to prepend to metric',
         short: '-s SCHEME',
         long: '--scheme SCHEME',
         default: "#{Socket.gethostname}.cpu"

  option :proc_path,
         long: '--proc-path /proc',
         proc: proc(&:to_s),
         default: '/proc'

  def run
    cpu_metrics = %w[user nice system idle iowait irq softirq steal guest guest_nice]
    other_metrics = %w[ctxt processes procs_running procs_blocked btime intr]
    cpu_count = 0

    File.open("#{config[:proc_path]}/stat", 'r').each_line do |line|
      info = line.split(/\s+/)
      next if info.empty?
      name = info.shift

      if name =~ /cpu([0-9]+|)/
        # #YELLOW
        cpu_count = cpu_count + 1 # rubocop:disable Style/SelfAssignment
        name = 'total' if name == 'cpu'
        info.size.times { |i| output "#{config[:scheme]}.#{name}.#{cpu_metrics[i]}", info[i] }
      end

      output "#{config[:scheme]}.#{name}", info.first if other_metrics.include? name
    end
    if cpu_count.positive?
      # writes the number of cpus, the minus 1 is because /proc/stat/
      # first line is a "cpu" which is stats for total cpus
      output "#{config[:scheme]}.cpu_count", cpu_count - 1
    end

    ok
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sensu-plugins-cpu-checks-4.0.0 bin/metrics-cpu.rb
sensu-plugins-cpu-checks-3.0.0 bin/metrics-cpu.rb
sensu-plugins-cpu-checks-2.1.0 bin/metrics-cpu.rb