Sha256: fc28fb1eb0771e6b1baa98fabc009410625ff56e4d537a55fc5b739e53ce8a1f

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

#! /usr/bin/env ruby
#  encoding: UTF-8
#
#   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.last if other_metrics.include? name
    end
    if cpu_count > 0
      # 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

1 entries across 1 versions & 1 rubygems

Version Path
sensu-plugins-cpu-checks-1.1.2 bin/metrics-cpu.rb