-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack.message
362 lines (327 loc) · 16.6 KB
/
slack.message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{{- define "header"}}
{{- $t := timeFormat .t "02.01.06 15:04:05"}}
{{- $by := ""}}
{{- if .u}}{{- $by = printf "\nby _%s_" .u}}{{end}}
{{- printf "*%s*: %s\n*%s*: %s%s\n" .o .l .c $t $by}}
{{- end}}
{{- define "object"}}
{{- if (index . 0)}}
{{- $o := index . 0}}
{{- $m := ".*"}}
{{- if (index . 1)}}{{$m = index . 1}}{{end}}
{{- range $k, $v := $o}}
{{- if ($k | regexMatch $m)}}
{{- printf "\n*%s* => %s" $k (toString $v)}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- define "k8s-header"}}
{{- $l := (printf "%s / %s" .data.kind .data.location)}}
{{- template "header" (dict "o" (toUpper .data.operation) "l" $l "c" .channel "t" .time "u" .data.user.name)}}
{{- end}}
{{- define "k8s-namespace"}}{{- end}}
{{- define "k8s-node"}}{{- end}}
{{- define "k8s-replicaset"}}{{- end}}
{{- define "k8s-statefulset"}}
{{- printf "\n*Replicas* => %.0f\n*Selector* => %s" .spec.replicas .spec.selector}}
{{- template "k8s-pod" .spec.template}}
{{- end}}
{{- define "k8s-daemonset"}}{{- end}}
{{- define "k8s-secret"}}{{- end}}
{{- define "k8s-ingress"}}{{- end}}
{{- define "k8s-cronjob"}}{{- end}}
{{- define "k8s-job"}}{{- end}}
{{- define "k8s-configmap"}}{{- end}}
{{- define "k8s-role"}}{{- end}}
{{- define "k8s-deployment"}}
{{- printf "\n*Replicas* => %.0f\n*Selector* => %s" .spec.replicas .spec.selector}}
{{- template "k8s-pod" .spec.template}}
{{- end}}
{{- define "k8s-service"}}
{{- printf "\n*%s* => %s" .spec.type .spec.clusterIP}}
{{- range .spec.ports}}
{{- printf "\n*%s* %.0f => %.0f" .protocol .port .targetPort}}
{{- end}}
{{- printf "\n*Selector* => %s" .spec.selector}}
{{- end}}
{{- define "k8s-pod"}}
{{- range .spec.containers}}
{{- printf "\n*%s* => %s" .name .image}}
{{- range .ports}}
{{- printf " [%s: %.0f]" .protocol .containerPort}}
{{- end}}
{{- end}}
{{- end}}
{{- define "kube-event"}}
{{- printf "%s: %s\n" .type .message}}
{{- printf "Reason: %s\n" .reason}}
{{- printf "Source: %s\n" .source}}
{{- printf "Location: %s" .location}}
{{- end}}
{{- define "alertmanager-header"}}
{{- $t := timeFormat .time "02.01.06 15:04:05"}}
{{- printf "*%s*: %s\n*%s*: %s" (toUpper .data.status) .data.labels.alertname .channel $t}}
{{- end}}
{{- define "alertmanager-body"}}
{{- if eq .status "firing"}}{{- printf "\n*StartsAt*: %s" (timeFormat .startsAt "02.01.06 15:04:05") }}{{end}}
{{- if eq .status "resolved"}}{{- printf "\n*endsAt*: %s" (timeFormat .endsAt "02.01.06 15:04:05") }}{{end}}
{{- if .labels}}
{{- printf "\n*Labels*:"}}
{{ range $key, $value := .labels }}
{{- printf "*%s*: %s" $key $value }}
{{ end }}
{{- end}}
{{- if eq .status "firing"}}
{{- printf "\n*GeneratorURL*: %s" .generatorURL }}
{{- end}}
{{- end}}
{{- define "gitlab-header"}}
{{- $t := timeFormat .time "02.01.06 15:04:05"}}
{{- if .data.project}}{{- printf "*%s*: %s / %s@%s\n*%s*: %s\nby _%s_" (toUpper .data.object_kind) .data.project.namespace .data.project.name .data.object_attributes.ref .channel $t .data.user.username}}
{{- else}}{{- printf "*%s*: %s@%s\n*%s*: %s\nby _%s_" (toUpper .data.object_kind) .data.project_name .data.ref .channel $t .data.user.username}}{{end}}
{{- end}}
{{- define "gitlab-commit"}}
{{- if .commit.author}}{{- printf "\n*Commit author* => %s" .commit.author.name}}{{else}}{{- printf "\n*Commit author* => %s" .commit.author_name}}{{end}}
{{- printf "\n*Commit message* => %s" .commit.message}}
{{- if .commit.url}}{{- printf "\n%s" .commit.url}}{{else}}{{- printf "\n%s/-/commit/%s" .repository.homepage .commit.sha}}{{end}}
{{- end}}
{{- define "gitlab-stages"}}
{{- $match := .match}}
{{- range .stages}}
{{- if and (.runner.description | regexMatch $match) (not (empty .finished_at))}}
{{- printf "\n*%s/%s* [%s] => %s" .stage .name .user.username .runner.description}}
{{- end}}
{{- end}}
{{- end}}
{{- define "gitlab-pipeline"}}
{{- $match := getEnv "EVENTS_GITLAB_RUNNERS"}}{{$ok := false}}
{{- range .data.builds}}
{{- if and (.runner.description | regexMatch $match) (not (empty .finished_at))}}{{$ok = true}}{{end}}
{{- end}}
{{- if $ok}}
{{- template "gitlab-header" .}}
{{- template "gitlab-commit" (dict "repository" "" "commit" .data.commit)}}
{{- template "gitlab-stages" (dict "stages" (reverse .data.builds) "match" $match)}}
{{- printf "\n%s/-/pipelines/%.0f" .data.project.web_url .data.object_attributes.id}}
{{- end}}
{{- end}}
{{- define "gitlab-build"}}
{{- $match := getEnv "EVENTS_GITLAB_RUNNERS"}}
{{- if and (.data.runner.description | regexMatch $match) (not (empty .data.build_duration))}}
{{- template "gitlab-header" .}}
{{- template "gitlab-commit" (dict "repository" .data.repository "commit" .data.commit)}}
{{- printf "\n*%s/%s* => %s" .data.build_stage .data.build_name .data.runner.description}}
{{- printf "\n%s/-/jobs/%.0f" .data.repository.homepage .data.build_id}}
{{- end}}
{{- end}}
{{- define "datadog"}}
{{- if not (.data.via)}}
{{- $gProject := ""}}
{{- $slack := ""}}
{{- if (.data.tags | regexMatch ".*sre-anomaly:(quality|availability).*")}}{{ $gProject = env "EVENTS_GITLAB_OUT_ANOMALY_SRE"}}{{end}}
{{- if and (ne $gProject "") (eq .data.alert.transition "Recovered")}}
{{- $gProjectIDRef := index (split "=" $gProject) 1}}
{{- $gProjectID := atoi (index (split "@" $gProjectIDRef) 0)}}
{{- $gURL := getEnv "EVENTS_GITLAB_URL"}}
{{- $gToken := getEnv "EVENTS_GITLAB_TOKEN"}}
{{- $gQuery := printf "DATADOG_TRANSITION=Triggered,DATADOG_ID=%s,SLACK_CHANNEL,SLACK_THREAD" .data.alert.id}}
{{- $vars := gitlabPipelineVars $gURL $gToken $gProjectID $gQuery 10 }}
{{- $sChannel := jsonata $vars "$[(key='SLACK_CHANNEL')].value" }}
{{- $sThread := jsonata $vars "$[(key='SLACK_THREAD')].value" }}
{{- $slack = dict "channel" $sChannel "thread" $sThread }}
{{- else}}
{{- $slack = dict "channel" "" "thread" "" }}
{{- end}}
{{- $image := "https://via.placeholder.com/452x185.png?text=No%20chart%20image"}}
{{- range (list .data.snapshot .data.snapshot .data.snapshot)}}
{{- $png := printf "%s?%s" . (randAlphaNum 6) }}
{{- if urlWait $png 1 2 199 }}
{{- $image = $png}}
{{- break }}
{{- end}}
{{- end}}
{{- logInfo "Datadog image => %s" $image }}
{{- jsonata (dict "datadog" .data "image" $image "slack" $slack) "datadog2slack.jsonata"}}
{{- end}}
{{- end}}
{{- define "site24x7"}}
{{printf "Site24x7 Event\n"}}
{{- printf "%s is %s from %s\n" .data.MONITORNAME .data.STATUS .data.FAILED_LOCATIONS}}
{{- printf "%s" .data.MONITOR_DASHBOARD_LINK}}
{{- end}}
{{- define "cloudflare"}}
{{- printf "Cloudflare Event\n"}}
{{ toJSON .data}}
{{- end}}
{{- define "google"}}
{{- if .data.incident}}
{{ toJSON .data}}
{{- end}}
{{- end}}
{{- define "teamcity"}}
{{- printf "*Build:* %s %s\n" .build_name .build_event}}
{{- printf "*Target:* %s\n" .target}}
{{- printf "*Triggered by:* %s\n" .triggered_by}}
{{- printf "*Status:* <%s|%s>" .build_status_url .build_result}}
{{- end}}
{{- define "aws-ec2"}}
{{- $instanceId := .data.detail.requestParameters.instanceId}}
{{- if .data.detail.requestParameters.tagSet.items}}
{{- if ne (len .data.detail.requestParameters.tagSet.items) 0}}
{{- $m := index .data.detail.requestParameters.tagSet.items 0}}
{{- $instanceId = get $m "value"}}
{{- end}}
{{- end}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source $instanceId)}}
{{- $u := index (split ":" .data.detail.userIdentity.principalId) 0}}
{{- template "header" (dict "o" .data.detail.eventName "l" $l "c" .channel "t" .time "u" $u)}}
{{- template "object" (list .data.detail.requestParameters "(networkInterfaceId)")}}
{{- template "object" (list .data.detail.responseElements "(device|volumeId)")}}
{{- template "object" (list .data.detail.userIdentity "(accessKeyId|arn)")}}
{{- template "object" (list .data.detail "(userAgent)")}}
{{- end}}
{{- define "aws-ecs"}}
{{- $cluster := index (split "/" .data.detail.clusterArn) 1}}
{{- $l := (printf "%s / %s / %s / %s" .data.region .data.source $cluster .data.detail.group)}}
{{- template "header" (dict "o" .data.detail.lastStatus "l" $l "c" .channel "t" .time "u" .data.detail.startedBy)}}
{{- range .data.detail.containers}}
{{- printf "\n*%s* => %s" .name .image}}
{{- end}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(stoppedReason|taskArn|userAgent)")}}
{{- end}}
{{- define "aws-rds"}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source .data.detail.SourceIdentifier)}}
{{- template "header" (dict "o" (index .data "detail-type") "l" $l "c" .channel "t" .time)}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(EventID|Message|userAgent)")}}
{{- end}}
{{- define "aws-elasticloadbalancing"}}
{{- $target := "unknown"}}
{{- if .data.detail.requestParameters.targetGroupArn}}{{$target = index (split "/" .data.detail.requestParameters.targetGroupArn) 1}}{{end}}
{{- if .data.detail.requestParameters.loadBalancerName}}{{$target = .data.detail.requestParameters.loadBalancerName}}{{end}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source $target)}}
{{- template "header" (dict "o" .data.detail.eventName "l" $l "c" .channel "t" .time "u" .data.detail.userIdentity.accessKeyId)}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(userAgent)")}}
{{- end}}
{{- define "aws-route53"}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source .data.detail.requestParameters.hostedZoneId)}}
{{- template "header" (dict "o" .data.detail.eventName "l" $l "c" .channel "t" .time "u" .data.detail.userIdentity.accessKeyId)}}
{{- range .data.detail.requestParameters.changeBatch.changes}}
{{- printf "\n*%s* => %s" .action .resourceRecordSet.name}}
{{- end}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(userAgent)")}}
{{- end}}
{{- define "aws-autoscaling"}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source .data.detail.AutoScalingGroupName)}}
{{- template "header" (dict "o" (index .data "detail-type") "l" $l "c" .channel "t" .time)}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(EC2InstnaceId|Cause|userAgent)")}}
{{- end}}
{{- define "aws-acm"}}
{{- $name := "unknown"}}
{{- if .data.detail.requestParameters.certificateArn}}{{$name = index (split "/" .data.detail.requestParameters.certificateArn) 1}}{{end}}
{{- if .data.detail.CommonName}}{{$name = .data.detail.CommonName}}{{end}}
{{- $l := (printf "%s / %s / %s" .data.region .data.source $name)}}
{{- $o := (index .data "detail-type")}}
{{- if .data.detail.eventName}}{{$o = .data.detail.eventName}}{{end}}
{{- template "header" (dict "o" $o "l" $l "c" .channel "t" .time "u" .data.detail.userIdentity.accessKeyId)}}
{{- template "object" (list .data.detail.userIdentity "(arn)")}}
{{- template "object" (list .data.detail "(DaysToExpiry|userAgent)")}}
{{- end}}
{{- define "nomad-allocation"}}
{{ printf "%s at %s is %s" .Name .NodeName .ClientStatus}}
{{- end}}
{{- define "nomad-evaluation"}}
{{- printf "%s %s\n%s" .JobID .Status .StatusDescription}}
{{- end}}
{{- define "nomad-deployment"}}
{{- printf "%s is %s" .ID .Status}}
{{- end}}
{{- define "nomad-job"}}
{{- printf "%s is %s" .ID .Status}}
{{- end}}
{{- define "nomad-node"}}
{{- printf "%s is %s" .Name .Status}}
{{- end}}
{{- define "zabbix"}}
{{- printf "ZabbixEvent on %s\n" .data.Environment}}
{{- printf "HostName: %s\n" .data.HostName}}
{{- printf "Status: %s\t" .data.Status}}
{{- if eq .data.EventNSeverity "3"}}{{- printf " / average\n" }}{{- end}}
{{- if eq .data.EventNSeverity "4"}}{{- printf " / high\n" }}{{- end}}
{{- if eq .data.EventNSeverity "5"}}{{- printf " / disaster\n" }}{{- end}}
{{- if .data.TriggerDescription}}{{- printf "TriggerDescription: %s\n" .data.TriggerDescription}}{{end -}}
{{- printf "AlertURL: %s\n" .data.AlertURL}}
{{- end}}
{{- define "text"}}
{{- if eq .type "ZabbixEvent"}}{{- if .data}}{{template "zabbix" .}}{{end}}{{- end}}
{{- if eq .type "KubeEvent"}}
{{template "kube-event" .data}}
{{- end}}
{{- if eq .type "K8sEvent"}}
{{- if not (.data.user.name | regexMatch "(system|eks).*")}}
{{- if .data.object}}
{{- if eq .data.kind "Namespace"}}{{template "k8s-header" .}}{{template "k8s-namespace" .data.object}}{{end}}
{{- if eq .data.kind "Node"}}{{template "k8s-header" .}}{{template "k8s-node" .data.object}}{{end}}
{{- if eq .data.kind "ReplicaSet"}}{{template "k8s-header" .}}{{template "k8s-replicaset" .data.object}}{{end}}
{{- if eq .data.kind "StatefulSet"}}{{template "k8s-header" .}}{{template "k8s-statefulset" .data.object}}{{end}}
{{- if eq .data.kind "DaemonSet"}}{{template "k8s-header" .}}{{template "k8s-daemonset" .data.object}}{{end}}
{{- if eq .data.kind "Secret"}}{{template "k8s-header" .}}{{template "k8s-secret" .data.object}}{{end}}
{{- if eq .data.kind "Ingress"}}{{template "k8s-header" .}}{{template "k8s-ingress" .data.object}}{{end}}
{{- if eq .data.kind "CronJob"}}{{template "k8s-header" .}}{{template "k8s-cronjob" .data.object}}{{end}}
{{- if eq .data.kind "Job"}}{{template "k8s-header" .}}{{template "k8s-job" .data.object}}{{end}}
{{- if eq .data.kind "ConfigMap"}}{{template "k8s-header" .}}{{template "k8s-configmap" .data.object}}{{end}}
{{- if eq .data.kind "Role"}}{{template "k8s-header" .}}{{template "k8s-role" .data.object}}{{end}}
{{- if eq .data.kind "Deployment"}}{{template "k8s-header" .}}{{template "k8s-deployment" .data.object}}{{end}}
{{- if eq .data.kind "Service"}}{{template "k8s-header" .}}{{template "k8s-service" .data.object}}{{end}}
{{- if eq .data.kind "Pod"}}{{template "k8s-header" .}}{{template "k8s-pod" .data.object}}{{end}}
{{- else}}{{template "k8s-header" .}}{{end}}
{{- end}}
{{- end}}
{{- if eq .type "AlertmanagerEvent"}}
{{template "alertmanager-header" .}}{{template "alertmanager-body" .data}}
{{- end}}
{{- if eq .type "GitlabEvent"}}
{{- if eq .data.object_kind "pipeline"}}{{template "gitlab-pipeline" .}}{{end}}
{{- if eq .data.object_kind "build"}}{{template "gitlab-build" .}}{{end}}
{{- end}}
{{- if eq .type "DataDogEvent"}}{{- if .data.event}}{{template "datadog" .}}{{end}}{{- end}}
{{- if eq .type "Site24x7Event"}}{{- if .data }}{{template "site24x7" .}}{{end}}{{- end}}
{{- if eq .type "CloudflareEvent"}}{{- if .data}}{{template "cloudflare" .}}{{end}}{{- end}}
{{- if eq .type "GoogleEvent"}}{{- if .data}}{{template "google" .}}{{end}}{{- end}}
{{- if eq .type "AWSEvent"}}
{{- if .data.detail}}
{{- if eq .data.source "aws.acm-pca"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.cloudfront"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.directconnect"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.s3"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.eks"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.msk"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.elasticache"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.dynamodb"}}{{toJSON .data}}{{end}}
{{- if eq .data.source "aws.ec2"}}{{template "aws-ec2" .}}{{end}}
{{- if eq .data.source "aws.ecs"}}{{template "aws-ecs" .}}{{end}}
{{- if eq .data.source "aws.rds"}}{{template "aws-rds" .}}{{end}}
{{- if eq .data.source "aws.elasticloadbalancing"}}{{template "aws-elasticloadbalancing" .}}{{end}}
{{- if eq .data.source "aws.route53"}}{{template "aws-route53" .}}{{end}}
{{- if eq .data.source "aws.autoscaling"}}{{template "aws-autoscaling" .}}{{end}}
{{- if eq .data.source "aws.acm"}}{{template "aws-acm" .}}{{end}}
{{- end}}
{{- end}}
{{- if eq .type "TeamcityEvent"}}{{- if .data}}{{template "teamcity" .data}}{{end}}{{- end}}
{{- if eq .type "NomadEvent"}}
{{- if ne .data.Type "AllocationUpdated" }}
{{- printf "Nomad Event %s\n" .data.Type}}
{{- if eq .data.Topic "Allocation"}}{{template "nomad-allocation" .data.Payload.Allocation}}{{end}}
{{- if eq .data.Topic "Job"}}{{template "nomad-job" .data.Payload.Job}}{{end}}
{{- if eq .data.Topic "Deployment"}}{{template "nomad-deployment" .data.Payload.Deployment}}{{end}}
{{- if eq .data.Topic "Evaluation"}}{{template "nomad-evaluation" .data.Payload.Evaluation}}{{end}}
{{- if eq .data.Topic "Node"}}{{template "nomad-node" .data.Payload.Node}}{{end}}
{{- end}}
{{- end}}
{{- end}}
{{- define "slack-message"}}{{template "text" .}}{{- end}}