Skip to content

Instantly share code, notes, and snippets.

@nstielau
Last active December 26, 2015 05:39
Show Gist options
  • Save nstielau/7102719 to your computer and use it in GitHub Desktop.
Save nstielau/7102719 to your computer and use it in GitHub Desktop.
check_load.rb with debugging output
#!/usr/bin/env ruby
#
# Check Linux system load
# ===
#
# Copyright 2012 Sonian, Inc <[email protected]>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
# rubocop:disable HandleExceptions
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'
class LoadAverage
def initialize(per_core = false)
puts "Getting load average (per_core=#{per_core})"
@cores = per_core ? cpu_count : 1
puts "Using #{@cores} cores"
begin
la = File.read('/proc/loadavg').split.take(3)
@avg = la.map {|a| (a.to_f / @cores).round(2) }
rescue Exception => e
puts "Error getting LA: #{e}"
@avg = nil
end
end
def cpu_count
begin
return `grep -sc ^processor /proc/cpuinfo`.to_i
rescue Exception => e
puts "Error getting CPU Count"
end
return 0
end
def failed?
puts "Avg is nil" if @avg.nil?
puts "No cores found" if @cores.zero?
@avg.nil? || @cores.zero?
end
def exceed?(thresholds)
@avg.zip(thresholds).any? {|a, t| a > t }
end
def to_s
@avg.join(', ')
end
end
class CheckLoad < Sensu::Plugin::Check::CLI
option :warn,
:short => '-w L1,L5,L15',
:long => '--warn L1,L5,L15',
:description => 'Load WARNING threshold, 1/5/15 min average',
:proc => proc {|a| a.split(',').map {|t| t.to_f } },
:default => [10, 20, 30]
option :crit,
:short => '-c L1,L5,L15',
:long => '--crit L1,L5,L15',
:description => 'Load CRITICAL threshold, 1/5/15 min average',
:proc => proc {|a| a.split(',').map {|t| t.to_f } },
:default => [25, 50, 75]
option :per_core,
:short => '-p',
:long => '--per-core',
:description => 'Divide load average results by cpu/core count',
:boolean => 'true',
:default => false
def run
avg = LoadAverage.new(config[:per_core])
warning "Could not read load average from /proc" if avg.failed?
message "Load average: #{avg}"
critical if avg.exceed?(config[:crit])
warning if avg.exceed?(config[:warn])
ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment