class GGCheck

Public Instance Methods

add_status_info(plugin_status, plugin_name) click to toggle source
# File lib/gg_check.rb, line 109
def add_status_info(plugin_status, plugin_name)
  plugin_status['message_values'] = [plugin_name] if plugin_status['message_key'] == 'event.not_running'
  plugin_status
end
config() click to toggle source
# File lib/gg_check.rb, line 57
def config
  config_file_name = Dir.glob(DEFAULT_CONF_FILE)[0]
  if File.exists?(config_file_name)
    begin
      conf_file = File.open(config_file_name)
      config = JSON.parse(conf_file.read)
    rescue Exception => e
      raise "Could not read from file #{config_file_name}"
    end
  else
    raise "Could find file #{config_file_name}"
  end
  config
end
options() click to toggle source

Will read the parameters for this application

# File lib/gg_check.rb, line 27
  def options
    doc = <<DOCOPT
    GaddyGaddy check. The result of the check is sent via sensu

    Usage:
      #{File.basename(__FILE__)[0..-4]} plugin <plugin>...  [--function=<function_id>] [--extra=<extra>] [--verbose] [--time_out=<time_out>]
      #{File.basename(__FILE__)[0..-4]} -h | --help
      #{File.basename(__FILE__)[0..-4]} --version

    Options:
      -h --help     Show this screen.
      --version     Show version.
      -v --verbose  Be more verbose
      --function=<function_id>  Function id to send with check result
      --extra=<extra> Extra data

DOCOPT

    begin
      @options = Docopt::docopt(doc)
    rescue Docopt::Exit => e
      puts e.message
      exit -1
    end
  end
plugins() click to toggle source
# File lib/gg_check.rb, line 53
def plugins
  @options['<plugin>']
end
run() click to toggle source
# File lib/gg_check.rb, line 157
def run
  options
  exit_status = run_plugin(plugins) if @options["plugin"]
  puts "Failed with exit status #{exit_status}" unless exit_status == 0
  exit exit_status
end
run_plugin(run_plugins) click to toggle source

Will load and run the specified plugin

# File lib/gg_check.rb, line 75
def run_plugin(run_plugins)
  exit_status = 0
  run_plugins.each do |plugin_name|
    begin
      load File.expand_path(File.dirname(__FILE__) + "/gg_check/plugins/#{plugin_name}.rb")
      puts "Will run #{plugin_name.camel_case}Check.new('#{@options['--extra']}')" if @options['--verbose']
      plugin = eval("#{plugin_name.camel_case}Check.new('#{@options['--extra']}')")
      status_opt = {}
      status_opt[:function_id] = @options['--function'] if @options['--function']
      time_out = @options['--time_out'].to_i
      end_time = Time.new + time_out if time_out
      plugin_status = {}
      loop do
        begin
          plugin_status = plugin.status(status_opt)
          break if plugin_status['status'].to_i == 0
          break if end_time.nil? || Time.new > end_time
        rescue Exception => e
          puts "Got error #{e.message}, will try again"
        end
        sleep 2
      end
      plugin_status = add_status_info plugin_status, plugin_name
      send_status_to_sensu plugin_status, plugin_name
      exit_status = plugin_status['status'] unless plugin_status['status'] == 0
    rescue Exception => e
      puts "Got error #{e.message} and \n#{e.backtrace}" if @options['--verbose']
      send_to_sensu "Could not run check. Error is #{e.message} for #{plugin_name}"
      exit_status = 3
    end
  end
  exit_status
end
send_status_to_sensu(plugin_status, plugin_name) click to toggle source
# File lib/gg_check.rb, line 114
def send_status_to_sensu(plugin_status, plugin_name)
  plugin_status[:function_id] = @options['--function'] if @options['--function']
  status = {:name => "gapp_check_#{plugin_name}"}
  output = {}
  plugin_status.each_pair do |k,v|
    output[k] = v unless ['status','handlers'].index(k)
  end

  status[:status] = plugin_status['status'] ? plugin_status['status'] : 0
  output[:type] = ['ok','warning','critical'][status[:status]]
  output[:message_key] = 'event.ok' if (status[:status] == 0) && (output[:message_key].nil?)
  status[:handlers] = plugin_status["handlers"] if plugin_status["handlers"]
  status[:handlers] = [] unless status[:handlers]
  status[:handlers] << 'function_check'
  if status[:handlers].index('restart_service')
    conf = config
    output[:user_id_salt] =   conf['user_id_salt']
    output[:token] = conf['token']
  end
  status[:output] = output.to_json
  puts "Result is " + status.to_s if @options['--verbose']
  send_to_sensu JSON.generate(status)
end
send_to_sensu(message) click to toggle source

Send via the local client port to sensu

# File lib/gg_check.rb, line 139
def send_to_sensu(message)
  begin
    s = TCPSocket.open(SENSU_HOST, SENSU_PORT)
    s.print message
    s.close
  rescue Exception => e
    puts e.message
  end
end
valid_json?(json_) click to toggle source

Validate the string to see if it’s correct JSON

# File lib/gg_check.rb, line 150
def valid_json? json_
  JSON.parse(json_)
  return true
rescue JSON::ParserError
  return false
end