bin/check-cpu.rb in sensu-plugins-cpu-checks-1.0.0 vs bin/check-cpu.rb in sensu-plugins-cpu-checks-1.1.1

- old
+ new

@@ -24,17 +24,25 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-plugin/check/cli' +require 'json' # # Check CPU # class CheckCPU < Sensu::Plugin::Check::CLI CPU_METRICS = [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest, :guest_nice].freeze + option :less_than, + description: 'Change whether value is less than check', + short: '-l', + long: '--less_than', + default: false, + boolean: true + option :warn, short: '-w WARN', proc: proc(&:to_f), default: 80 @@ -46,13 +54,17 @@ option :sleep, long: '--sleep SLEEP', proc: proc(&:to_f), default: 1 + option :cache_file, + long: '--cache-file CACHEFILE', + default: nil + option :proc_path, long: '--proc-path /proc', - proc: proc(&:to_f), + proc: proc(&:to_s), default: '/proc' option :idle_metrics, long: '--idle-metrics METRICS', description: 'Treat the specified metrics as idle. Defaults to idle,iowait,steal,guest,guest_nice', @@ -73,22 +85,51 @@ name = info.shift return info.map(&:to_f) if name =~ /^cpu$/ end end + def acquire_stats_with_sleeping(sec) + before = acquire_cpu_stats + sleep sec + after = acquire_cpu_stats + + [before, after] + end + + def acquire_stats_with_cache_file + before = JSON.parse(File.read(config[:cache_file])) + now = acquire_cpu_stats + + [before, now] + end + + def write_stats_to_cache_file(data) + File.write(config[:cache_file], data) + end + + def acquire_stats(sec) + if config[:cache_file] && File.exist?(config[:cache_file]) + (before, now) = acquire_stats_with_cache_file + else + (before, now) = acquire_stats_with_sleeping(sec) + end + + write_stats_to_cache_file(now) if config[:cache_file] + + [before, now] + end + def run - cpu_stats_before = acquire_cpu_stats - sleep config[:sleep] - cpu_stats_after = acquire_cpu_stats + (cpu_stats_before, cpu_stats_now) = acquire_stats(config[:sleep]) # Some kernels don't have 'guest' and 'guest_nice' values - metrics = CPU_METRICS.slice(0, cpu_stats_after.length) + metrics = CPU_METRICS.slice(0, cpu_stats_now.length) cpu_total_diff = 0.to_f cpu_stats_diff = [] metrics.each_index do |i| - cpu_stats_diff[i] = cpu_stats_after[i] - cpu_stats_before[i] + cpu_stats_diff[i] = cpu_stats_now[i] - cpu_stats_before[i] cpu_total_diff += cpu_stats_diff[i] end cpu_stats = [] metrics.each_index do |i| @@ -111,10 +152,15 @@ msg = "total=#{(cpu_usage * 100).round / 100.0}" cpu_stats.each_index { |i| msg += " #{metrics[i]}=#{(cpu_stats[i] * 100).round / 100.0}" } message msg - critical if checked_usage >= config[:crit] - warning if checked_usage >= config[:warn] + if config[:less_than] + critical if checked_usage <= config[:crit] + warning if checked_usage <= config[:warn] + else + critical if checked_usage >= config[:crit] + warning if checked_usage >= config[:warn] + end ok end end