<commit_before>#!/usr/bin/env ruby
#
# Check CPU Plugin
#

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'

class CheckCPU < Sensu::Plugin::Check::CLI

  option :warn,
    :short => '-w WARN',
    :proc => proc {|a| a.to_f },
    :default => 80

  option :crit,
    :short => '-c CRIT',
    :proc => proc {|a| a.to_f },
    :default => 10

  option :sleep,
    :long => '--sleep SLEEP',
    :proc => proc {|a| a.to_f },
    :default => 1

  option :process_white_list,
    :short => '-p PROCESS_WHITE_LIST',
    :long => '--process-white-list PROCESS_WHITE_LIST',
    :proc => proc {|a| a.split(',') },
    :default => []  

  [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest].each do |metric|
    option metric,
      :long  => "--#{metric}",
      :description => "Check cpu #{metric} instead of total cpu usage",
      :boolean => true,
      :default => false
  end

  def get_cpu_stats
    File.open("/proc/stat", "r").each_line do |line|
      info = line.split(/\s+/)
      name = info.shift
      return info.map{|i| i.to_f} if name.match(/^cpu$/)
    end
  end

def top_process_on_whitelist?
    config[:process_white_list].map {|cmd| system("ps axo cmd,pcpu k pcpu | tail -n 1 | grep #{cmd} 1>/dev/null") }.any?
end
  
  def run
    metrics = [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest]

    cpu_stats_before = get_cpu_stats
    sleep config[:sleep]
    cpu_stats_after = get_cpu_stats

    cpu_total_diff = 0.to_f
    cpu_stats_diff = []
    metrics.each_index do |i|
      # Some OS's don't have a 'guest' values (RHEL)
      unless cpu_stats_after[i].nil?
        cpu_stats_diff[i] = cpu_stats_after[i] - cpu_stats_before[i]
        cpu_total_diff += cpu_stats_diff[i]
      end
    end

    cpu_stats = []
    metrics.each_index do |i|
      cpu_stats[i] = 100*(cpu_stats_diff[i]/cpu_total_diff)
    end

    cpu_usage = 100*(cpu_total_diff - cpu_stats_diff[3])/cpu_total_diff
    checked_usage = cpu_usage

    self.class.check_name 'CheckCPU TOTAL'
    metrics.each do |metric|
      if config[metric]<commit_msg>sort ps by memory usage and cpu<commit_after>#!/usr/bin/env ruby
#
# Check CPU Plugin
#

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'

class CheckCPU < Sensu::Plugin::Check::CLI

  option :warn,
    :short => '-w WARN',
    :proc => proc {|a| a.to_f },
    :default => 80

  option :crit,
    :short => '-c CRIT',
    :proc => proc {|a| a.to_f },
    :default => 10

  option :sleep,
    :long => '--sleep SLEEP',
    :proc => proc {|a| a.to_f },
    :default => 1

  option :process_white_list,
    :short => '-p PROCESS_WHITE_LIST',
    :long => '--process-white-list PROCESS_WHITE_LIST',
    :proc => proc {|a| a.split(',') },
    :default => []  

  [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest].each do |metric|
    option metric,
      :long  => "--#{metric}",
      :description => "Check cpu #{metric} instead of total cpu usage",
      :boolean => true,
      :default => false
  end

  def get_cpu_stats
    File.open("/proc/stat", "r").each_line do |line|
      info = line.split(/\s+/)
      name = info.shift
      return info.map{|i| i.to_f} if name.match(/^cpu$/)
    end
  end

def top_process_on_whitelist?
    config[:process_white_list].map {|cmd| system("ps axo cmd,pcpu,pmem k pmem,pcpu | tail -n 1 | grep #{cmd} 1>/dev/null") }.any?
end
  
  def run
    metrics = [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest]

    cpu_stats_before = get_cpu_stats
    sleep config[:sleep]
    cpu_stats_after = get_cpu_stats

    cpu_total_diff = 0.to_f
    cpu_stats_diff = []
    metrics.each_index do |i|
      # Some OS's don't have a 'guest' values (RHEL)
      unless cpu_stats_after[i].nil?
        cpu_stats_diff[i] = cpu_stats_after[i] - cpu_stats_before[i]
        cpu_total_diff += cpu_stats_diff[i]
      end
    end

    cpu_stats = []
    metrics.each_index do |i|
      cpu_stats[i] = 100*(cpu_stats_diff[i]/cpu_total_diff)
    end

    cpu_usage = 100*(cpu_total_diff - cpu_stats_diff[3])/cpu_total_diff
    checked_usage = cpu_usage

    self.class.check_name 'CheckCPU TOTAL'
    metrics.each do |metric|
      if config[metric]
{'fraction_non_alphanumeric': 0.1568535091851154, 'fraction_numerical': 0.008478568064060292, 'mean_word_length': 2.914285714285714, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896056', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1593, 'n_words': 375}