huginn_pagerduty_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa095d93a5d6200329d552185456398db77ffaabce6cbf1793f863de4ca66a11
4
+ data.tar.gz: 271d7604e4720bbf8446bc354f210956b138fb41da89aa4ba27dda045629768b
5
+ SHA512:
6
+ metadata.gz: dd97c96427c54b98f026e2f66ef97f3ed8b215fff30e095ef007c7510741b692995389e7f693ba0cb7234d83fef57df0eca9168a754417185d51ece6843eeb14
7
+ data.tar.gz: 7e57b86e1be763e7e0e3236f32023eb2665cbbdc17bf3b930027b898aa26d2852e2f265bf4d4fda7961cc1676ad3acb8ca382af4f05f07d153d0f75a9aec54d6
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2025 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,294 @@
1
+ module Agents
2
+ class PagerdutyAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_5m'
7
+
8
+ description do
9
+ <<-MD
10
+ The PagerDuty Agent creates events / checks incidents with the PG api..
11
+
12
+ The `type` can be like check_incidents or create_incident for example.
13
+
14
+ `token` is needed for the authentication.
15
+
16
+ `data` is the payload (alert / system / foo / bar).
17
+
18
+ `debug` is used for verbose mode.
19
+
20
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
21
+ that you anticipate passing without this Agent receiving an incoming Event.
22
+ MD
23
+ end
24
+
25
+ event_description <<-MD
26
+ Events look like this:
27
+
28
+ {
29
+ "incident": {
30
+ "incident_number": 3,
31
+ "title": "test",
32
+ "description": "test",
33
+ "created_at": "2021-07-31T16:34:10Z",
34
+ "status": "triggered",
35
+ "incident_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
36
+ "service": {
37
+ "id": "XXXXXX",
38
+ "type": "service_reference",
39
+ "summary": "test",
40
+ "self": "https://api.pagerduty.com/services/XXXXXX",
41
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/service-directory/XXXXXX"
42
+ },
43
+ "assignments": [
44
+ {
45
+ "at": "2021-07-31T16:34:11Z",
46
+ "assignee": {
47
+ "id": "XXXXXX",
48
+ "type": "user_reference",
49
+ "summary": "XXXXXXXXXXXXXXX",
50
+ "self": "https://api.pagerduty.com/users/XXXXXX",
51
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/users/XXXXXX"
52
+ }
53
+ }
54
+ ],
55
+ "assigned_via": "escalation_policy",
56
+ "last_status_change_at": "2021-07-31T16:34:10Z",
57
+ "first_trigger_log_entry": {
58
+ "id": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
59
+ "type": "trigger_log_entry_reference",
60
+ "summary": "Triggered through the website",
61
+ "self": "https://api.pagerduty.com/log_entries/XXXXXXXXXXXXXXXXXXXXXXXXXX",
62
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/incidents/XXXXXX/log_entries/XXXXXXXXXXXXXXXXXXXXXXXXXX"
63
+ },
64
+ "alert_counts": {
65
+ "all": 0,
66
+ "triggered": 0,
67
+ "resolved": 0
68
+ },
69
+ "is_mergeable": true,
70
+ "escalation_policy": {
71
+ "id": "XXXXXX",
72
+ "type": "escalation_policy_reference",
73
+ "summary": "test-ep",
74
+ "self": "https://api.pagerduty.com/escalation_policies/XXXXXX",
75
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/escalation_policies/XXXXXX"
76
+ },
77
+ "teams": [],
78
+ "impacted_services": [
79
+ {
80
+ "id": "XXXXXX",
81
+ "type": "service_reference",
82
+ "summary": "test",
83
+ "self": "https://api.pagerduty.com/services/XXXXXX",
84
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/service-directory/XXXXXX"
85
+ }
86
+ ],
87
+ "pending_actions": [],
88
+ "acknowledgements": [],
89
+ "basic_alert_grouping": null,
90
+ "alert_grouping": null,
91
+ "last_status_change_by": {
92
+ "id": "XXXXXX",
93
+ "type": "service_reference",
94
+ "summary": "test",
95
+ "self": "https://api.pagerduty.com/services/XXXXXX",
96
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/service-directory/XXXXXX"
97
+ },
98
+ "incidents_responders": [],
99
+ "responder_requests": [],
100
+ "subscriber_requests": [],
101
+ "urgency": "high",
102
+ "id": "XXXXXX",
103
+ "type": "incident",
104
+ "summary": "[#3] test",
105
+ "self": "https://api.pagerduty.com/incidents/XXXXXX",
106
+ "html_url": "https://XXXXXXXXXXXXXX.pagerduty.com/incidents/XXXXXX"
107
+ }
108
+ }
109
+ MD
110
+
111
+ def default_options
112
+ {
113
+ 'email' => '',
114
+ 'team_id' => '',
115
+ 'data' => '',
116
+ 'debug' => 'false',
117
+ 'emit_events' => 'true',
118
+ 'expected_receive_period_in_days' => '7',
119
+ 'token' => ''
120
+ }
121
+ end
122
+
123
+ form_configurable :team_id, type: :string
124
+ form_configurable :debug, type: :boolean
125
+ form_configurable :emit_events, type: :boolean
126
+ form_configurable :token, type: :string
127
+ form_configurable :expected_receive_period_in_days, type: :string
128
+ form_configurable :email, type: :string
129
+ form_configurable :type, type: :array, values: ['check_incidents', 'create_incident']
130
+ form_configurable :data, type: :string
131
+
132
+ def validate_options
133
+ errors.add(:base, "type has invalid value: should be 'check_incidents', 'create_incident'") if interpolated['type'].present? && !%w(check_incidents create_incident).include?(interpolated['type'])
134
+
135
+ unless options['team_id'].present? || !['check_incidents'].include?(options['type'])
136
+ errors.add(:base, "team_id is a required field")
137
+ end
138
+
139
+ unless options['data'].present? || !['create_incident'].include?(options['type'])
140
+ errors.add(:base, "data is a required field")
141
+ end
142
+
143
+ if options.has_key?('debug') && boolify(options['debug']).nil?
144
+ errors.add(:base, "if provided, debug must be true or false")
145
+ end
146
+
147
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
148
+ errors.add(:base, "if provided, emit_events must be true or false")
149
+ end
150
+
151
+ unless options['token'].present?
152
+ errors.add(:base, "token is a required field")
153
+ end
154
+
155
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
156
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
157
+ end
158
+ end
159
+
160
+ def working?
161
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
162
+ end
163
+
164
+ def receive(incoming_events)
165
+ incoming_events.each do |event|
166
+ interpolate_with(event) do
167
+ log event
168
+ trigger_action
169
+ end
170
+ end
171
+ end
172
+
173
+ def check
174
+ trigger_action
175
+ end
176
+
177
+ private
178
+
179
+ def log_curl_output(code,body)
180
+
181
+ log "request status : #{code}"
182
+
183
+ if interpolated['debug'] == 'true'
184
+ log "body"
185
+ log body
186
+ end
187
+
188
+ end
189
+
190
+ def create_incident
191
+
192
+ if interpolated['debug'] == 'true'
193
+ log "data : #{interpolated['data']}"
194
+ end
195
+
196
+ uri = URI.parse("https://api.pagerduty.com/incidents")
197
+ request = Net::HTTP::Post.new(uri)
198
+ request.content_type = "application/json"
199
+ request["From"] = "#{interpolated['email']}"
200
+ request["Accept"] = "application/vnd.pagerduty+json;version=2"
201
+ request["Authorization"] = "Token token=#{interpolated['token']}"
202
+ request.body = interpolated['data']
203
+
204
+ req_options = {
205
+ use_ssl: uri.scheme == "https",
206
+ }
207
+
208
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
209
+ http.request(request)
210
+ end
211
+
212
+ log_curl_output(response.code,response.body)
213
+
214
+ if interpolated['emit_events'] == 'true'
215
+ create_event :payload => response.body
216
+ end
217
+
218
+ end
219
+
220
+ def check_incidents
221
+
222
+ uri = URI.parse("https://api.pagerduty.com/incidents")
223
+ request = Net::HTTP::Get.new(uri)
224
+ request.content_type = "application/json"
225
+ request["Accept"] = "application/json"
226
+ request["Authorization"] = "Token token=#{interpolated['token']}"
227
+ request.body = JSON.dump({
228
+ "team_ids" => [
229
+ "#{interpolated['team_id']}"
230
+ ],
231
+ "sort_by" => "created_at:DESC"
232
+ })
233
+
234
+ req_options = {
235
+ use_ssl: uri.scheme == "https",
236
+ }
237
+
238
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
239
+ http.request(request)
240
+ end
241
+
242
+ log_curl_output(response.code,response.body)
243
+
244
+ payload = JSON.parse(response.body)
245
+ if payload != memory['last_status']
246
+ payload['incidents'].each do |incident|
247
+ found = false
248
+ if interpolated['debug'] == 'true'
249
+ log "incident"
250
+ log incident
251
+ end
252
+ if !memory['last_status'].nil? and memory['last_status'].present?
253
+ if interpolated['debug'] == 'true'
254
+ log "memory"
255
+ log memory['last_status']
256
+ end
257
+ last_status = memory['last_status']
258
+ last_status['incidents'].each do |incidentbis|
259
+ if incident == incidentbis
260
+ found = true
261
+ end
262
+ if interpolated['debug'] == 'true'
263
+ log "incidentbis"
264
+ log incidentbis
265
+ log "found is #{found}!"
266
+ end
267
+ end
268
+ end
269
+ if found == false
270
+ create_event payload: incident
271
+ end
272
+ end
273
+ else
274
+ if interpolated['debug'] == 'true'
275
+ log "nothing to compare"
276
+ end
277
+ end
278
+ memory['last_status'] = payload
279
+
280
+ end
281
+
282
+ def trigger_action
283
+
284
+ case interpolated['type']
285
+ when "check_incidents"
286
+ check_incidents()
287
+ when "create_incident"
288
+ create_incident()
289
+ else
290
+ log "Error: type has an invalid value (#{interpolated['type']})"
291
+ end
292
+ end
293
+ end
294
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_pagerduty_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_pagerduty_agent/pagerduty_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::PagerdutyAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::PagerdutyAgent.new.default_options
7
+ @checker = Agents::PagerdutyAgent.new(:name => "PagerdutyAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_pagerduty_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_pagerduty_agent.rb
64
+ - lib/huginn_pagerduty_agent/pagerduty_agent.rb
65
+ - spec/pagerduty_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_pagerduty_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/pagerduty_agent_spec.rb