Skip to content

Commit ada996f

Browse files
authored
Alternate endpoint (#56)
* Add support for alternate endpoints * Add support to send events to a different endpoint than the PagerDuty events API * Update to the latest sensu-plugin-skd Signed-off-by: Francis Guimond [email protected]
1 parent 2742bb2 commit ada996f

File tree

7 files changed

+452
-177
lines changed

7 files changed

+452
-177
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
99

10+
## 2.5.0 - 2023-05-03
11+
12+
### Added
13+
- Add `--alternate-endpoint` option to be able to send events to a different endpoint than the PagerDuty API
14+
1015
## 2.4.0 - 2023-04-04
1116

12-
###
17+
### Added
1318
- Add `--details-format` option to be able to specify if the event details should be sent as a string or a JSON document
1419

1520
## 2.3.0 – 2022-05-06

README.md

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
# Sensu PagerDuty Handler
66

77
## Table of Contents
8+
89
- [Overview](#overview)
910
- [Usage examples](#usage-examples)
10-
- [Help output](#help-output)
11-
- [Deduplication key](#deduplication-key)
12-
- [PagerDuty severity mapping](#pagerduty-severity-mapping)
11+
- [Help output](#help-output)
12+
- [Deduplication key](#deduplication-key)
13+
- [PagerDuty severity mapping](#pagerduty-severity-mapping)
1314
- [Configuration](#configuration)
14-
- [Asset registration](#asset-registration)
15-
- [Handler definition](#handler-definition)
16-
- [Environment variables](#environment-variables)
17-
- [Templates](#templates)
18-
- [Argument annotations](#argument-annotations)
19-
- [Pager teams](#pager-teams)
20-
- [Contact routing](#contact-routing)
21-
- [Proxy support](#proxy-support)
15+
- [Asset registration](#asset-registration)
16+
- [Handler definition](#handler-definition)
17+
- [Environment variables](#environment-variables)
18+
- [Templates](#templates)
19+
- [Argument annotations](#argument-annotations)
20+
- [Pager teams](#pager-teams)
21+
- [Contact routing](#contact-routing)
22+
- [Proxy support](#proxy-support)
2223
- [Installation from source](#installation-from-source)
2324
- [Contributing](#contributing)
2425

@@ -31,6 +32,7 @@ The Sensu PagerDuty Handler is a [Sensu Event Handler][3] which manages
3132
## Usage examples
3233

3334
### Help output
35+
3436
```
3537
The Sensu Go PagerDuty handler for incident management
3638
@@ -39,10 +41,12 @@ Usage:
3941
sensu-pagerduty-handler [command]
4042
4143
Available Commands:
44+
completion Generate the autocompletion script for the specified shell
4245
help Help about any command
4346
version Print the version number of this plugin
4447
4548
Flags:
49+
-e, --alternate-endpoint string The endpoint to use to send the PagerDuty events, can be set with PAGERDUTY_ALTERNATE_ENDPOINT
4650
--contact-routing Enable contact routing
4751
-k, --dedup-key-template string The PagerDuty V2 API deduplication key template, can be set with PAGERDUTY_DEDUP_KEY_TEMPLATE (default "{{.Entity.Name}}-{{.Check.Name}}")
4852
--details-format string The format of the details output ('string' or 'json'), can be set with PAGERDUTY_DETAILS_FORMAT (default "string")
@@ -52,14 +56,15 @@ Flags:
5256
-S, --summary-template string The template for the alert summary, can be set with PAGERDUTY_SUMMARY_TEMPLATE (default "{{.Entity.Name}}/{{.Check.Name}} : {{.Check.Output}}")
5357
--team string Envvar name for pager team(alphanumeric and underscores) holding PagerDuty V2 API authentication token, can be set with PAGERDUTY_TEAM
5458
--team-suffix string Pager team suffix string to append if missing from team name, can be set with PAGERDUTY_TEAM_SUFFIX (default "_pagerduty_token")
59+
--timeout uint The maximum amount of time in seconds to wait for the event to be created, can be set with PAGERDUTY_TIMEOUT (default 30)
5560
-t, --token string The PagerDuty V2 API authentication token, can be set with PAGERDUTY_TOKEN
5661
5762
Use "sensu-pagerduty-handler [command] --help" for more information about a command.
5863
```
5964

6065
### Deduplication key
6166

62-
The deduplication key is determined via the `--dedup-key-template` argument. It
67+
The deduplication key is determined via the `--dedup-key-template` argument. It
6368
is a Golang template containing the event values and defaults to
6469
`{{.Entity.Name}}-{{.Check.Name}}`.
6570

@@ -68,41 +73,42 @@ is a Golang template containing the event values and defaults to
6873
Optionally you can provide mapping information between the Sensu check status
6974
and the PagerDuty incident severity. To provide the mapping you need to use the
7075
`--status-map` command line option or the `PAGERDUTY_STATUS_MAP` environment
71-
variable. The option accepts a JSON document containing the mapping
76+
variable. The option accepts a JSON document containing the mapping
7277
information. Here's an example of the JSON document:
7378

7479
```json
7580
{
76-
"info": [
77-
0,
78-
1
79-
],
80-
"warning": [
81-
2
82-
],
83-
"critical:": [
84-
3
85-
],
86-
"error": [
87-
4,
88-
5,
89-
6,
90-
7,
91-
8,
92-
9,
93-
10
94-
]
81+
"info": [
82+
0,
83+
1
84+
],
85+
"warning": [
86+
2
87+
],
88+
"critical:": [
89+
3
90+
],
91+
"error": [
92+
4,
93+
5,
94+
6,
95+
7,
96+
8,
97+
9,
98+
10
99+
]
95100
}
96101
```
97102

98103
The valid [PagerDuty alert severity levels][5] are the following:
104+
99105
* `info`
100106
* `warning`
101107
* `critical`
102108
* `error`
103109

104-
105110
## Configuration
111+
106112
### Asset registration
107113

108114
[Sensu Assets][6] are the best way to make use of this plugin. If you're not
@@ -136,36 +142,38 @@ spec:
136142
--details-format string
137143
timeout: 10
138144
runtime_assets:
139-
- sensu/sensu-pagerduty-handler
145+
- sensu/sensu-pagerduty-handler
140146
filters:
141-
- is_incident
147+
- is_incident
142148
secrets:
143-
- name: PAGERDUTY_TOKEN
144-
secret: pagerduty_authtoken
149+
- name: PAGERDUTY_TOKEN
150+
secret: pagerduty_authtoken
145151
```
146152
147153
### Environment variables
148154
149155
Most arguments for this handler are available to be set via environment
150-
variables. However, any arguments specified directly on the command line
156+
variables. However, any arguments specified directly on the command line
151157
override the corresponding environment variable.
152158
153159
| Argument | Environment Variable |
154160
|----------------------|------------------------------|
161+
| --alternate-endpoint | PAGERDUTY_ALTERNATE_ENDPOINT |
155162
| --dedup-key-template | PAGERDUTY_DEDUP_KEY_TEMPLATE |
156163
| --details-template | PAGERDUTY_DETAILS_TEMPLATE |
157164
| --details-format | PAGERDUTY_DETAILS_FORMAT |
158165
| --status-map | PAGERDUTY_STATUS_MAP |
159166
| --summary-template | PAGERDUTY_SUMMARY_TEMPLATE |
160167
| --team | PAGERDUTY_TEAM |
161168
| --team-suffix | PAGERDUTY_TEAM_SUFFIX |
169+
| --timeout | PAGERDUTY_TIMEOUT |
162170
| --token | PAGERDUTY_TOKEN |
163171
164172
**Security Note:** Care should be taken to not expose the auth token for this
165173
handler by specifying it on the command line or by directly setting the
166-
environment variable in the handler definition. It is suggested to make use of
167-
[secrets management][8] to surface it as an environment variable. The handler
168-
definition above references it as a secret. Below is an example secrets
174+
environment variable in the handler definition. It is suggested to make use of
175+
[secrets management][8] to surface it as an environment variable. The handler
176+
definition above references it as a secret. Below is an example secrets
169177
definition that make use of the built-in [env secrets provider][9].
170178
171179
```yml
@@ -188,13 +196,13 @@ template syntax and format can be found in [the documentation][12].
188196
### Argument annotations
189197
190198
All arguments for this handler are tunable on a per entity or check basis based
191-
on annotations. The annotations keyspace for this handler is
199+
on annotations. The annotations keyspace for this handler is
192200
`sensu.io/plugins/sensu-pagerduty-handler/config`.
193201

194202
**NOTE**: Due to [check token substituion][10], supplying a template value such
195203
as for `details-template` as a check annotation requires that you place the
196204
desired template as a [golang string literal][11] (enlcosed in backticks)
197-
within another template definition. This does not apply to entity annotations.
205+
within another template definition. This does not apply to entity annotations.
198206

199207
##### Examples
200208

@@ -208,7 +216,7 @@ api_version: core/v2
208216
metadata:
209217
annotations:
210218
sensu.io/plugins/sensu-pagerduty-handler/config/details-template: "{{`{{.Check.Output}}`}}"
211-
[...]
219+
[ ... ]
212220
```
213221

214222
To change the `--token` argument for a particular check, for that checks's metadata
@@ -220,26 +228,31 @@ api_version: core/v2
220228
metadata:
221229
annotations:
222230
sensu.io/plugins/sensu-pagerduty-handler/config/token: abcde12345fabcd67890efabc12345de
223-
[...]
231+
[ ... ]
224232
```
225233

226234
### Pager teams
227235

228-
Instead of specifying the authentication token directly in the check or agent annotations, you can instead reference a pager team name, which will then be used to lookup the corresponding token from the handler environment.
236+
Instead of specifying the authentication token directly in the check or agent annotations, you can instead reference a
237+
pager team name, which will then be used to lookup the corresponding token from the handler environment.
229238
Corresponding pager team token environment variables can be populated in the handler environment in 3 different ways
239+
230240
1. Explicitly set in the handler definition
231241
2. Kept as Sensu [secrets][13] and referenced in the handler definition
232242
3. Defined in the [backend service environment file][14], read in at backend service start.
233243

234244
Pager team names will be automatically suffixed with configured --team-suffix (default: `_pagerduty_suffix`)
235-
Note: Pager team name strings should be alphameric and underscores only. Groups of illegal characters will be mapped into a single underscore character. Ex: `example-_-team` will be converted to `example_team`
245+
Note: Pager team name strings should be alphameric and underscores only. Groups of illegal characters will be mapped
246+
into a single underscore character. Ex: `example-_-team` will be converted to `example_team`
236247

237248
If the team token lookup fails, the explicitly provided token will be used as a fallback if available.
238249

239250
##### Example of Check Using Pager Team and Handler Environment Variables:
240251

241252
First set the team annotation in the check or agent resource.
253+
242254
###### Check Snippet:
255+
243256
```
244257
---
245258
type: CheckConfig
@@ -253,6 +266,7 @@ metadata:
253266
And define the corresponding evironment variable for the pager team's token in the handler's environment.
254267

255268
###### Handler Snippet:
269+
256270
```
257271
---
258272
type: Handler
@@ -283,13 +297,15 @@ spec:
283297

284298
### Contact Routing
285299

286-
The Sensu Pagerduty Handler provides support for generating one Pagerduty event per "contact" via the `--contact-routing` flag.
300+
The Sensu Pagerduty Handler provides support for generating one Pagerduty event per "contact" via
301+
the `--contact-routing` flag.
287302

288303
With `--contact-routing` enabled, the Sensu Pagerduty Handler will do the following:
289304

290305
* Check for and merge the entity, check, and/or event `contacts` annotation.
291306

292-
The `contacts` annotation supports a comma-separated list of contact names containing alpha-numeric characters and underscore (`_`) characters only.
307+
The `contacts` annotation supports a comma-separated list of contact names containing alpha-numeric characters and
308+
underscore (`_`) characters only.
293309

294310
Example:
295311

@@ -298,19 +314,26 @@ With `--contact-routing` enabled, the Sensu Pagerduty Handler will do the follow
298314
contacts: "team_a,team_b"
299315
```
300316

301-
_NOTE: when `--contact-routing` is enabled, the handler will log a message like `Pagerduty contact routing is enabled (contacts: team_a, team_b)`.
302-
If `--contact-routing` is enabled and no `contacts` annotations are found, the handler will log an error like `contact routing enabled but no contacts were found`._
317+
_NOTE: when `--contact-routing` is enabled, the handler will log a message
318+
like `Pagerduty contact routing is enabled (contacts: team_a, team_b)`.
319+
If `--contact-routing` is enabled and no `contacts` annotations are found, the handler will log an error
320+
like `contact routing enabled but no contacts were found`._
303321

304322
* Lookup contact-specific environment variables for Pagerduty API Authentication
305323

306-
When `--contact-routing` is enabled, the Sensu Pagerduty Handler will attempt to create or update an event per configured "contact".
307-
For each configured "contact", the Sensu Pagerduty Handler will look for a matching environment variable containing a Pagerduty token.
308-
Pagerduty token environment variables should be prefixed with `PAGERDUTY_TOKEN_` and match the contact name in all-uppercase (e.g. the contact "team_a" needs a corresponding `PAGERDUTY_TOKEN_TEAM_A` environment variable).
324+
When `--contact-routing` is enabled, the Sensu Pagerduty Handler will attempt to create or update an event per
325+
configured "contact".
326+
For each configured "contact", the Sensu Pagerduty Handler will look for a matching environment variable containing a
327+
Pagerduty token.
328+
Pagerduty token environment variables should be prefixed with `PAGERDUTY_TOKEN_` and match the contact name in
329+
all-uppercase (e.g. the contact "team_a" needs a corresponding `PAGERDUTY_TOKEN_TEAM_A` environment variable).
309330

310-
_NOTE: contact names are converted to environment variables via `fmt.Sprintf("PAGERDUTY_TOKEN_%s",strings.ToUpper(contact))`._
331+
_NOTE: contact names are converted to environment variables
332+
via `fmt.Sprintf("PAGERDUTY_TOKEN_%s",strings.ToUpper(contact))`._
311333

312334
If a matching contact environment variable is found, the event will be processed.
313-
If the contact environment variable is not found, the handler will log a warning (e.g. `WARNING: skipping contact: "team-a" (no environment variable found for "PAGERDUTY_TOKEN_TEAM_A")`\n).
335+
If the contact environment variable is not found, the handler will log a warning (
336+
e.g. `WARNING: skipping contact: "team-a" (no environment variable found for "PAGERDUTY_TOKEN_TEAM_A")`\n).
314337

315338
#### Contact Routing Example
316339

@@ -330,23 +353,24 @@ spec:
330353
--summary-template "[{{.Entity.Namespace}}] {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}"
331354
--details-template "{{ .Check.Name }} is {{ .Check.State }} on {{ .Entity.Name }} (namespace: {{ .Entity.Namespace }})"
332355
timeout: 10
333-
filters: []
334-
runtime_assets: []
335-
env_vars: []
356+
filters: [ ]
357+
runtime_assets: [ ]
358+
env_vars: [ ]
336359
secrets:
337360
- name: PAGERDUTY_TOKEN_TEAM_A
338361
secret: pagerduty_token_team_a
339362
- name: PAGERDUTY_TOKEN_TEAM_B
340363
secret: pagerduty_token_team_b
341364
```
342365
343-
_NOTE: contact routing is compatible with Sensu Secrets or environment variables set via Handler `env_vars`, but given the sensitive nature of a Pagerduty API token, using secrets management is strongly encouraged._
366+
_NOTE: contact routing is compatible with Sensu Secrets or environment variables set via Handler `env_vars`, but given
367+
the sensitive nature of a Pagerduty API token, using secrets management is strongly encouraged._
344368

345369
### Proxy support
346370

347371
This handler supports the use of the environment variables HTTP_PROXY,
348372
HTTPS_PROXY, and NO_PROXY (or the lowercase versions thereof). HTTPS_PROXY takes
349-
precedence over HTTP_PROXY for https requests. The environment values may be
373+
precedence over HTTP_PROXY for https requests. The environment values may be
350374
either a complete URL or a "host[:port]", in which case the "http" scheme is
351375
assumed.
352376

@@ -356,6 +380,7 @@ Download the latest version of the sensu-pagerduty-handler from [releases][4],
356380
or create an executable from this source.
357381

358382
From the local path of the sensu-pagerduty-handler repository:
383+
359384
```
360385
go build -o /usr/local/bin/sensu-pagerduty-handler
361386
```
@@ -365,16 +390,29 @@ go build -o /usr/local/bin/sensu-pagerduty-handler
365390
See https://github.com/sensu/sensu-go/blob/master/CONTRIBUTING.md
366391

367392
[1]: https://github.com/sensu/sensu-go
393+
368394
[2]: https://www.pagerduty.com/
395+
369396
[3]: https://docs.sensu.io/sensu-go/5.0/reference/handlers/#how-do-sensu-handlers-work
397+
370398
[4]: https://github.com/sensu/sensu-pagerduty-handler/releases
399+
371400
[5]: https://support.pagerduty.com/docs/dynamic-notifications#section-eventalert-severity-levels
401+
372402
[6]: https://docs.sensu.io/sensu-go/latest/reference/assets/
403+
373404
[7]: https://bonsai.sensu.io/sensu/sensu-pagerduty-handler
405+
374406
[8]: https://docs.sensu.io/sensu-go/latest/guides/secrets-management/
407+
375408
[9]: https://docs.sensu.io/sensu-go/latest/guides/secrets-management/#use-env-for-secrets-management
409+
376410
[10]: https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-schedule/checks/#check-token-substitution
411+
377412
[11]: https://golang.org/ref/spec#String_literals
413+
378414
[12]: https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-process/handler-templates/
415+
379416
[13]: https://docs.sensu.io/sensu-go/latest/operations/manage-secrets/secrets/
417+
380418
[14]: https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-schedule/backend/#use-environment-variables-with-the-sensu-backend

0 commit comments

Comments
 (0)