Class: Upstreamstatus

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

Constant Summary collapse

VERSION =
"0.4.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUpstreamstatus



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

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'
  Unirest.default_header 'Accept', 'application/vnd.pagerduty+json;version=2'

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

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



26
27
28
# File 'lib/upstreamstatus.rb', line 26

def conf
  @conf
end

Instance Method Details

#current_statusObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/upstreamstatus.rb', line 91

def current_status
  @current_status ||= begin
    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
end

#down_hostsObject



54
55
56
# File 'lib/upstreamstatus.rb', line 54

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

#runObject



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
85
86
87
88
89
# File 'lib/upstreamstatus.rb', line 58

def run
  clear_active_alerts

  exit 0 if down_hosts.empty?

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

  if opts[:notify]
    down_hosts.each do |host|
      notify(
        "Upstream host #{host['upstream']} listed as down",
        host
      )
    end
  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