upstreamstatus 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77db314deab89fc679fc47c383192fafafab930e
4
- data.tar.gz: dc2b44690c28f80881fda94f7a0959dc60198821
3
+ metadata.gz: 46e8a906db4b57704842d9182ed382e74e6b9604
4
+ data.tar.gz: 57315acb232238d2932b2860b9627796c6fac02b
5
5
  SHA512:
6
- metadata.gz: aebccadf9e8857262c831dafd055ecb3f240719a4f6af0b84ade880d5c3a5c4db2e1c2e856f9bcf2a20c2714400132686611f54831e2441080f4a6fae265afcf
7
- data.tar.gz: e374ff2d622b4f5ea34a74419d9e5cd0a0b28205b4aeb398d8836c6321eba3b23b3ba9476d5609d722624680a569c7641554c534a0db5d6c6bef836d227487c3
6
+ metadata.gz: cf7475dacb3d805f689ea558dcedc13c3b430ecf95a824b042e0ac1c129414d74a2886241bbf5d2dc8e52ff0a5b5f62cbf5783b131d35d35a17734cf5a4b4040
7
+ data.tar.gz: d17cc84a32ee42ac467b1db5f2f3658a1851dd23992d07b75c0120a3d0eda76b35787a888db258914ec9306128e70232c95c75bd6fed89e1144f2109f53b3963
@@ -1,3 +1,3 @@
1
1
  class Upstreamstatus
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'upstreamstatus/version'
2
+ require 'active_support/time'
2
3
  require 'unirest'
3
4
  require 'yaml'
4
5
  require 'ostruct'
@@ -6,6 +7,8 @@ require 'trollop'
6
7
  require 'forwardable'
7
8
  require 'json'
8
9
  require 'sentry-raven'
10
+ require 'pagerduty'
11
+ require 'time'
9
12
 
10
13
  class Upstreamstatus
11
14
  extend Forwardable
@@ -13,14 +16,32 @@ class Upstreamstatus
13
16
  def_delegators :@conf,
14
17
  :status_check_url,
15
18
  :sentry_dsn,
16
- :pagerduty_api_key
19
+ :pagerduty_api_url,
20
+ :pagerduty_service_id,
21
+ :pagerduty_api_key,
22
+ :pagerduty_rest_api_key
17
23
 
18
24
  attr_reader :conf
19
25
 
20
26
  def initialize
21
27
  @conf = OpenStruct.new load_conf
22
28
 
23
- return unless opts[:notify] && sentry_dsn
29
+ return unless opts[:notify]
30
+
31
+ %w(
32
+ sentry_dsn
33
+ pagerduty_api_key
34
+ pagerduty_rest_api_key
35
+ pagerduty_api_url
36
+ pagerduty_service_id
37
+ ).each do |key|
38
+ fail "Config missing #{key}" unless conf[key]
39
+ end
40
+
41
+ Unirest.default_header 'Authorization',
42
+ "Token token=#{pagerduty_rest_api_key}"
43
+ Unirest.default_header 'Content-type', 'application/json'
44
+
24
45
  Raven.configure do |config|
25
46
  config.dsn = sentry_dsn
26
47
  config.logger = logger
@@ -36,21 +57,34 @@ class Upstreamstatus
36
57
  exit 0
37
58
  end
38
59
 
60
+ puts "Detected down hosts:\n"
39
61
  print_hosts down_hosts
40
- logger.notice "Detected down hosts: #{down_hosts.to_json}"
62
+ logger.info "Detected down hosts: #{down_hosts.to_json}"
41
63
 
42
64
  if opts[:notify]
43
- puts 'Sending notifications'
44
65
  notify(
45
66
  'One or more API upstream hosts listed as down',
46
67
  JSON.pretty_generate(down_hosts)
47
68
  )
48
69
  end
49
70
  exit 1
71
+ rescue Interrupt => e
72
+ puts "Received #{e.class}"
73
+ exit 99
74
+ rescue SignalException => e
75
+ logger.info "Received: #{e.signm} (#{e.signo})"
76
+ exit 2
77
+ rescue SystemExit => e
78
+ exit e.status
79
+ rescue Exception => e # Need to rescue "Exception" so that Sentry gets it
80
+ Raven.capture_exception(e) if sentry_dsn
81
+ logger.fatal e.message
82
+ logger.fatal e.backtrace.join("\n")
83
+ raise e
50
84
  end
51
85
 
52
86
  def current_status
53
- return fake_servers if opts[:simulate]
87
+ return fake_response if opts[:simulate]
54
88
 
55
89
  r = Unirest.get status_check_url
56
90
 
@@ -65,34 +99,58 @@ class Upstreamstatus
65
99
 
66
100
  private
67
101
 
68
- def fake_servers
69
- [
70
- {
71
- 'index' => 0,
72
- 'upstream' => 'testupstream',
73
- 'name' => '10.0.0.1 =>8080',
74
- 'status' => 'up',
75
- 'rise' => 10_459,
76
- 'fall' => 0,
77
- 'type' => 'http',
78
- 'port' => 0
79
- },
80
- {
81
- 'index' => 1,
82
- 'upstream' => 'testupstream',
83
- 'name' => '10.0.0.2 =>8080',
84
- 'status' => 'down',
85
- 'rise' => 10_029,
86
- 'fall' => 0,
87
- 'type' => 'http',
88
- 'port' => 0
102
+ def pd_incidents
103
+ @pd_incidents ||= begin
104
+ r = Unirest.get(
105
+ "#{pagerduty_api_url}/incidents",
106
+ parameters: { service: pagerduty_service_id }
107
+ )
108
+ fail "Result: #{r.inspect}" unless (200..299).include?(r.code)
109
+ r.body['incidents']
110
+ end
111
+ end
112
+
113
+ def fake_response
114
+ {
115
+ 'servers' => {
116
+ 'total' => 2,
117
+ 'generation' => 99,
118
+ 'server' => [
119
+ {
120
+ 'index' => 0,
121
+ 'upstream' => 'testupstream',
122
+ 'name' => '10.0.0.1 =>8080',
123
+ 'status' => 'up',
124
+ 'rise' => 10_459,
125
+ 'fall' => 0,
126
+ 'type' => 'http',
127
+ 'port' => 0
128
+ },
129
+ {
130
+ 'index' => 1,
131
+ 'upstream' => 'testupstream',
132
+ 'name' => '10.0.0.2 =>8080',
133
+ 'status' => 'down',
134
+ 'rise' => 10_029,
135
+ 'fall' => 0,
136
+ 'type' => 'http',
137
+ 'port' => 0
138
+ }
139
+ ]
89
140
  }
90
- ]
141
+ }
91
142
  end
92
143
 
93
144
  def clear_active_alerts!
94
- return unless File.exist? '/var/run/active_upstream_alert'
95
- File.delete('/var/run/active_upstream_alert')
145
+ return unless opts[:notify]
146
+ pd_incidents.each do |i|
147
+ resolve_incident i['incident_key'] unless i['status'] == 'resolved'
148
+ end
149
+ end
150
+
151
+ def resolve_incident(incident_key)
152
+ puts "Resolving incident: #{incident_key}"
153
+ pagerduty.get_incident(incident_key).resolve
96
154
  end
97
155
 
98
156
  def logger
@@ -104,7 +162,10 @@ class Upstreamstatus
104
162
  end
105
163
 
106
164
  def notify(msg, details)
107
- unless active_alert?(msg)
165
+ if active_alert?(msg)
166
+ puts "Already an active alert. Not sending anything (message: #{msg})"
167
+ else
168
+ puts "Notifying PagerDuty (message: #{msg})"
108
169
  pd = pagerduty.trigger(
109
170
  msg,
110
171
  client: ENV['hostname'],
@@ -124,14 +185,13 @@ class Upstreamstatus
124
185
  }.to_json
125
186
  )
126
187
  end
127
-
128
- Raven.capture_exception(e) if sentry_dsn
129
188
  end
130
189
 
131
190
  def active_alert?(msg)
132
- File.exist?('/var/run/active_upstream_alert') &&
133
- (Time.now - File.ctime('/var/run/active_upstream_alert')) < 3600 &&
134
- JSON.parse(File.read('/var/run/active_upstream_alert'))['msg'] == msg
191
+ pd_incidents.find do |incident|
192
+ incident['status'] != 'resolved' &&
193
+ incident['trigger_summary_data']['description'] == msg
194
+ end
135
195
  end
136
196
 
137
197
  def opts
@@ -30,6 +30,8 @@ Gem::Specification.new do |spec|
30
30
  spec.add_dependency 'unirest', '~> 1.1'
31
31
  spec.add_dependency 'trollop', '~> 2.1'
32
32
  spec.add_dependency 'sentry-raven'
33
+ spec.add_dependency 'pagerduty'
34
+ spec.add_dependency 'activesupport'
33
35
 
34
36
  spec.add_development_dependency 'bundler', '~> 1.10'
35
37
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upstreamstatus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Herot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-06 00:00:00.000000000 Z
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unirest
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pagerduty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -156,4 +184,3 @@ signing_key:
156
184
  specification_version: 4
157
185
  summary: Parse the output of the Nginx Upstream Check plugin
158
186
  test_files: []
159
- has_rdoc: