Class: Upstreamstatus

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/upstreamstatus.rb,
lib/upstreamstatus/version.rb

Constant Summary collapse

VERSION =
"0.2.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUpstreamstatus

Returns a new instance of Upstreamstatus.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/upstreamstatus.rb', line 26

def initialize
  @conf = OpenStruct.new load_conf

  return unless opts[:notify]

  %w(
    sentry_dsn
    pagerduty_api_key
    pagerduty_rest_api_key
    pagerduty_api_url
    pagerduty_service_id
  ).each do |key|
    fail "Config missing #{key}" unless conf[key]
  end

  Unirest.default_header 'Authorization',
                         "Token token=#{pagerduty_rest_api_key}"
  Unirest.default_header 'Content-type', 'application/json'

  Raven.configure do |config|
    config.dsn = sentry_dsn
    config.logger = logger
  end
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



24
25
26
# File 'lib/upstreamstatus.rb', line 24

def conf
  @conf
end

Instance Method Details

#current_statusObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/upstreamstatus.rb', line 86

def current_status
  return fake_response if opts[:simulate]

  r = Unirest.get status_check_url

  unless (200..299).include?(r.code)
    fail "Error code: #{r.code}\n" \
         "Headers: #{r.headers}" \
         "Body: #{r.body}"
  end

  r.body
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/upstreamstatus.rb', line 51

def run
  down_hosts =
    current_status['servers']['server'].select { |s| s['status'] != 'up' }

  if down_hosts.empty?
    clear_active_alerts!
    exit 0
  end

  puts "Detected down hosts:\n"
  print_hosts down_hosts
  logger.info "Detected down hosts: #{down_hosts.to_json}"

  if opts[:notify]
    notify(
      'One or more API upstream hosts listed as down',
      JSON.pretty_generate(down_hosts)
    )
  end
  exit 1
rescue Interrupt => e
  puts "Received #{e.class}"
  exit 99
rescue SignalException => e
  logger.info "Received: #{e.signm} (#{e.signo})"
  exit 2
rescue SystemExit => e
  exit e.status
rescue Exception => e # Need to rescue "Exception" so that Sentry gets it
  Raven.capture_exception(e) if sentry_dsn
  logger.fatal e.message
  logger.fatal e.backtrace.join("\n")
  raise e
end