Class: GGCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/gg_check.rb

Instance Method Summary collapse

Instance Method Details

#add_status_info(plugin_status, plugin_name) ⇒ Object



109
110
111
112
# 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

#configObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

#optionsObject

Will read the parameters for this application



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#pluginsObject



53
54
55
# File 'lib/gg_check.rb', line 53

def plugins
  @options['<plugin>']
end

#runObject



157
158
159
160
161
162
# 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) ⇒ Object

Will load and run the specified plugin



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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) ⇒ Object

Send via the local client port to sensu



139
140
141
142
143
144
145
146
147
# 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_) ⇒ Boolean

Validate the string to see if it’s correct JSON

Returns:

  • (Boolean)


150
151
152
153
154
155
# File 'lib/gg_check.rb', line 150

def valid_json? json_
  JSON.parse(json_)
  return true
rescue JSON::ParserError
  return false
end