forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
1043 lines (958 loc) · 29.2 KB
/
alert.go
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pipeline
import (
"reflect"
"time"
"github.com/influxdata/kapacitor/tick"
)
// Number of previous states to remember when computing flapping percentage.
const defaultFlapHistory = 21
// Default template for constructing an ID
const defaultIDTmpl = "{{ .Name }}:{{ .Group }}"
// Default template for constructing a message.
const defaultMessageTmpl = "{{ .ID }} is {{ .Level }}"
// Default template for constructing a details message.
const defaultDetailsTmpl = "{{ json . }}"
// Default log mode for file
const defaultLogFileMode = 0600
// An AlertNode can trigger an event of varying severity levels,
// and pass the event to alert handlers. The criteria for triggering
// an alert is specified via a [lambda expression](/kapacitor/latest/tick/expr/).
// See AlertNode.Info, AlertNode.Warn, and AlertNode.Crit below.
//
// Different event handlers can be configured for each AlertNode.
// Some handlers like Email, HipChat, Sensu, Slack, OpsGenie, VictorOps, PagerDuty and Talk have a configuration
// option 'global' that indicates that all alerts implicitly use the handler.
//
// Available event handlers:
//
// * log -- log alert data to file.
// * post -- HTTP POST data to a specified URL.
// * email -- Send and email with alert data.
// * exec -- Execute a command passing alert data over STDIN.
// * HipChat -- Post alert message to HipChat room.
// * Alerta -- Post alert message to Alerta.
// * Sensu -- Post alert message to Sensu client.
// * Slack -- Post alert message to Slack channel.
// * OpsGenie -- Send alert to OpsGenie.
// * VictorOps -- Send alert to VictorOps.
// * PagerDuty -- Send alert to PagerDuty.
// * Talk -- Post alert message to Talk client.
//
// See below for more details on configuring each handler.
//
// Each event that gets sent to a handler contains the following alert data:
//
// * ID -- the ID of the alert, user defined.
// * Message -- the alert message, user defined.
// * Details -- the alert details, user defined HTML content.
// * Time -- the time the alert occurred.
// * Duration -- the duration of the alert in nanoseconds.
// * Level -- one of OK, INFO, WARNING or CRITICAL.
// * Data -- influxql.Result containing the data that triggered the alert.
//
// Events are sent to handlers if the alert is in a state other than 'OK'
// or the alert just changed to the 'OK' state from a non 'OK' state (a.k.a. the alert recovered).
// Using the AlertNode.StateChangesOnly property events will only be sent to handlers
// if the alert changed state.
//
// It is valid to configure multiple alert handlers, even with the same type.
//
// Example:
// stream
// .groupBy('service')
// |alert()
// .id('kapacitor/{{ index .Tags "service" }}')
// .message('{{ .ID }} is {{ .Level }} value:{{ index .Fields "value" }}')
// .info(lambda: "value" > 10)
// .warn(lambda: "value" > 20)
// .crit(lambda: "value" > 30)
// .post("http://example.com/api/alert")
// .post("http://another.example.com/api/alert")
// .email().to('oncall@example.com')
//
//
// It is assumed that each successive level filters a subset
// of the previous level. As a result, the filter will only be applied if
// a data point passed the previous level.
// In the above example, if value = 15 then the INFO and
// WARNING expressions would be evaluated, but not the
// CRITICAL expression.
// Each expression maintains its own state.
//
// Available Statistics:
//
// * alerts_triggered -- Total number of alerts triggered
// * oks_triggered -- Number of OK alerts triggered
// * infos_triggered -- Number of Info alerts triggered
// * warns_triggered -- Number of Warn alerts triggered
// * crits_triggered -- Number of Crit alerts triggered
//
type AlertNode struct {
chainnode
// Template for constructing a unique ID for a given alert.
//
// Available template data:
//
// * Name -- Measurement name.
// * TaskName -- The name of the task
// * Group -- Concatenation of all group-by tags of the form [key=value,]+.
// If no groupBy is performed equal to literal 'nil'.
// * Tags -- Map of tags. Use '{{ index .Tags "key" }}' to get a specific tag value.
//
// Example:
// stream
// |from()
// .measurement('cpu')
// .groupBy('cpu')
// |alert()
// .id('kapacitor/{{ .Name }}/{{ .Group }}')
//
// ID: kapacitor/cpu/cpu=cpu0,
//
// Example:
// stream
// |from()
// .measurement('cpu')
// .groupBy('service')
// |alert()
// .id('kapacitor/{{ index .Tags "service" }}')
//
// ID: kapacitor/authentication
//
// Example:
// stream
// |from()
// .measurement('cpu')
// .groupBy('service', 'host')
// |alert()
// .id('kapacitor/{{ index .Tags "service" }}/{{ index .Tags "host" }}')
//
// ID: kapacitor/authentication/auth001.example.com
//
// Default: {{ .Name }}:{{ .Group }}
Id string
// Template for constructing a meaningful message for the alert.
//
// Available template data:
//
// * ID -- The ID of the alert.
// * Name -- Measurement name.
// * TaskName -- The name of the task
// * Group -- Concatenation of all group-by tags of the form [key=value,]+.
// If no groupBy is performed equal to literal 'nil'.
// * Tags -- Map of tags. Use '{{ index .Tags "key" }}' to get a specific tag value.
// * Level -- Alert Level, one of: INFO, WARNING, CRITICAL.
// * Fields -- Map of fields. Use '{{ index .Fields "key" }}' to get a specific field value.
// * Time -- The time of the point that triggered the event.
//
// Example:
// stream
// |from()
// .measurement('cpu')
// .groupBy('service', 'host')
// |alert()
// .id('{{ index .Tags "service" }}/{{ index .Tags "host" }}')
// .message('{{ .ID }} is {{ .Level}} value: {{ index .Fields "value" }}')
//
// Message: authentication/auth001.example.com is CRITICAL value:42
//
// Default: {{ .ID }} is {{ .Level }}
Message string
// Template for constructing a detailed HTML message for the alert.
// The same template data is available as the AlertNode.Message property,
// in addition to a Message field that contains the rendered Message value.
//
// The intent is that the Message property be a single line summary while the
// Details property is a more detailed message possibly spanning multiple lines,
// and containing HTML formatting.
//
// This template is rendered using the html/template package in Go so that
// safe and valid HTML can be generated.
//
// The `json` method is available within the template to convert any variable to a valid
// JSON string.
//
// Example:
// |alert()
// .id('{{ .Name }}')
// .details('''
//<h1>{{ .ID }}</h1>
//<b>{{ .Message }}</b>
//Value: {{ index .Fields "value" }}
//''')
// .email()
//
// Default: {{ json . }}
Details string
// Filter expression for the INFO alert level.
// An empty value indicates the level is invalid and is skipped.
Info tick.Node
// Filter expression for the WARNING alert level.
// An empty value indicates the level is invalid and is skipped.
Warn tick.Node
// Filter expression for the CRITICAL alert level.
// An empty value indicates the level is invalid and is skipped.
Crit tick.Node
//tick:ignore
UseFlapping bool `tick:"Flapping"`
//tick:ignore
FlapLow float64
//tick:ignore
FlapHigh float64
// Number of previous states to remember when computing flapping levels and
// checking for state changes.
// Minimum value is 2 in order to keep track of current and previous states.
//
// Default: 21
History int64
// Optional tag key to use when tagging the data with the alert level.
LevelTag string
// Optional field key to add to the data, containing the alert level as a string.
LevelField string
// Optional field key to add the alert duration to the data.
// The duration is always in units of nanoseconds.
DurationField string
// Optional tag key to use when tagging the data with the alert ID.
IdTag string
// Optional field key to add to the data, containing the alert ID as a string.
IdField string
// Indicates an alert should trigger only if all points in a batch match the criteria
// tick:ignore
AllFlag bool `tick:"All"`
// Send alerts only on state changes.
// tick:ignore
IsStateChangesOnly bool `tick:"StateChangesOnly"`
// Maximum interval to ignore non state changed events
// tick:ignore
StateChangesOnlyDuration time.Duration
// Post the JSON alert data to the specified URL.
// tick:ignore
PostHandlers []*PostHandler `tick:"Post"`
// Email handlers
// tick:ignore
EmailHandlers []*EmailHandler `tick:"Email"`
// A commands to run when an alert triggers
// tick:ignore
ExecHandlers []*ExecHandler `tick:"Exec"`
// Log JSON alert data to file. One event per line.
// tick:ignore
LogHandlers []*LogHandler `tick:"Log"`
// Send alert to VictorOps.
// tick:ignore
VictorOpsHandlers []*VictorOpsHandler `tick:"VictorOps"`
// Send alert to PagerDuty.
// tick:ignore
PagerDutyHandlers []*PagerDutyHandler `tick:"PagerDuty"`
// Send alert to Sensu.
// tick:ignore
SensuHandlers []*SensuHandler `tick:"Sensu"`
// Send alert to Slack.
// tick:ignore
SlackHandlers []*SlackHandler `tick:"Slack"`
// Send alert to HipChat.
// tick:ignore
HipChatHandlers []*HipChatHandler `tick:"HipChat"`
// Send alert to Alerta.
// tick:ignore
AlertaHandlers []*AlertaHandler `tick:"Alerta"`
// Send alert to OpsGenie
// tick:ignore
OpsGenieHandlers []*OpsGenieHandler `tick:"OpsGenie"`
// Send alert to Talk.
// tick:ignore
TalkHandlers []*TalkHandler `tick:"Talk"`
}
func newAlertNode(wants EdgeType) *AlertNode {
a := &AlertNode{
chainnode: newBasicChainNode("alert", wants, wants),
History: defaultFlapHistory,
Id: defaultIDTmpl,
Message: defaultMessageTmpl,
Details: defaultDetailsTmpl,
}
return a
}
//tick:ignore
func (n *AlertNode) ChainMethods() map[string]reflect.Value {
return map[string]reflect.Value{
"Log": reflect.ValueOf(n.chainnode.Log),
}
}
// Indicates an alert should trigger only if all points in a batch match the criteria
// Does not apply to stream alerts.
// tick:property
func (n *AlertNode) All() *AlertNode {
n.AllFlag = true
return n
}
// Only sends events where the state changed.
// Each different alert level OK, INFO, WARNING, and CRITICAL
// are considered different states.
//
// Example:
// stream
// |from()
// .measurement('cpu')
// |window()
// .period(10s)
// .every(10s)
// |alert()
// .crit(lambda: "value" > 10)
// .stateChangesOnly()
// .slack()
//
// If the "value" is greater than 10 for a total of 60s, then
// only two events will be sent. First, when the value crosses
// the threshold, and second, when it falls back into an OK state.
// Without stateChangesOnly, the alert would have triggered 7 times:
// 6 times for each 10s period where the condition was met and once more
// for the recovery.
//
// An optional maximum interval duration can be provided.
// An event will not be ignore (aka trigger an alert) if more than the maximum interval has elapsed
// since the last alert.
//
// Example:
// stream
// |from()
// .measurement('cpu')
// |window()
// .period(10s)
// .every(10s)
// |alert()
// .crit(lambda: "value" > 10)
// .stateChangesOnly(10m)
// .slack()
//
// The abvove usage will only trigger alerts to slack on state changes or at least every 10 minutes.
//
// tick:property
func (a *AlertNode) StateChangesOnly(maxInterval ...time.Duration) *AlertNode {
a.IsStateChangesOnly = true
if len(maxInterval) == 1 {
a.StateChangesOnlyDuration = maxInterval[0]
}
return a
}
// Perform flap detection on the alerts.
// The method used is similar method to Nagios:
// https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/flapping.html
//
// Each different alerting level is considered a different state.
// The low and high thresholds are inverted thresholds of a percentage of state changes.
// Meaning that if the percentage of state changes goes above the `high`
// threshold, the alert enters a flapping state. The alert remains in the flapping state
// until the percentage of state changes goes below the `low` threshold.
// Typical values are low: 0.25 and high: 0.5. The percentage values represent the number state changes
// over the total possible number of state changes. A percentage change of 0.5 means that the alert changed
// state in half of the recorded history, and remained the same in the other half of the history.
// tick:property
func (a *AlertNode) Flapping(low, high float64) *AlertNode {
a.UseFlapping = true
a.FlapLow = low
a.FlapHigh = high
return a
}
// HTTP POST JSON alert data to a specified URL.
// tick:property
func (a *AlertNode) Post(url string) *PostHandler {
post := &PostHandler{
AlertNode: a,
URL: url,
}
a.PostHandlers = append(a.PostHandlers, post)
return post
}
// tick:embedded:AlertNode.Email
type PostHandler struct {
*AlertNode
// The POST URL.
// tick:ignore
URL string
}
// Email the alert data.
//
// If the To list is empty, the To addresses from the configuration are used.
// The email subject is the AlertNode.Message property.
// The email body is the AlertNode.Details property.
// The emails are sent as HTML emails and so the body can contain html markup.
//
// If the 'smtp' section in the configuration has the option: global = true
// then all alerts are sent via email without the need to explicitly state it
// in the TICKscript.
//
// Example:
// |alert()
// .id('{{ .Name }}')
// // Email subject
// .meassage('{{ .ID }}:{{ .Level }}')
// //Email body as HTML
// .details('''
//<h1>{{ .ID }}</h1>
//<b>{{ .Message }}</b>
//Value: {{ index .Fields "value" }}
//''')
// .email()
//
// Send an email with custom subject and body.
//
// Example:
// [smtp]
// enabled = true
// host = "localhost"
// port = 25
// username = ""
// password = ""
// from = "kapacitor@example.com"
// to = ["oncall@example.com"]
// # Set global to true so all alert trigger emails.
// global = true
// state-changes-only = true
//
// Example:
// stream
// |alert()
//
// Send email to 'oncall@example.com' from 'kapacitor@example.com'
//
// tick:property
func (a *AlertNode) Email(to ...string) *EmailHandler {
em := &EmailHandler{
AlertNode: a,
ToList: to,
}
a.EmailHandlers = append(a.EmailHandlers, em)
return em
}
// Email AlertHandler
// tick:embedded:AlertNode.Email
type EmailHandler struct {
*AlertNode
// List of email recipients.
// tick:ignore
ToList []string
}
// Execute a command whenever an alert is triggered and pass the alert data over STDIN in JSON format.
// tick:property
func (a *AlertNode) Exec(executable string, args ...string) *ExecHandler {
exec := &ExecHandler{
AlertNode: a,
Command: append([]string{executable}, args...),
}
a.ExecHandlers = append(a.ExecHandlers, exec)
return exec
}
// tick:embedded:AlertNode.Exec
type ExecHandler struct {
*AlertNode
// The command to execute
// tick:ignore
Command []string
}
// Log JSON alert data to file. One event per line.
// Must specify the absolute path to the log file.
// It will be created if it does not exist.
// Example:
// stream
// |alert()
// .log('/tmp/alert')
//
// Example:
// stream
// |alert()
// .log('/tmp/alert')
// .mode(0644)
// tick:property
func (a *AlertNode) Log(filepath string) *LogHandler {
log := &LogHandler{
AlertNode: a,
FilePath: filepath,
Mode: defaultLogFileMode,
}
a.LogHandlers = append(a.LogHandlers, log)
return log
}
// tick:embedded:AlertNode.Log
type LogHandler struct {
*AlertNode
// Absolute path the the log file.
// It will be created if it does not exist.
// tick:ignore
FilePath string
// File's mode and permissions, default is 0600
Mode int64
}
// Send alert to VictorOps.
// To use VictorOps alerting you must first enable the 'Alert Ingestion API'
// in the 'Integrations' section of VictorOps.
// Then place the API key from the URL into the 'victorops' section of the Kapacitor configuration.
//
// Example:
// [victorops]
// enabled = true
// api-key = "xxxxx"
// routing-key = "everyone"
//
// With the correct configuration you can now use VictorOps in TICKscripts.
//
// Example:
// stream
// |alert()
// .victorOps()
//
// Send alerts to VictorOps using the routing key in the configuration file.
//
// Example:
// stream
// |alert()
// .victorOps()
// .routingKey('team_rocket')
//
// Send alerts to VictorOps with routing key 'team_rocket'
//
// If the 'victorops' section in the configuration has the option: global = true
// then all alerts are sent to VictorOps without the need to explicitly state it
// in the TICKscript.
//
// Example:
// [victorops]
// enabled = true
// api-key = "xxxxx"
// routing-key = "everyone"
// global = true
//
// Example:
// stream
// |alert()
//
// Send alert to VictorOps using the default routing key, found in the configuration.
// tick:property
func (a *AlertNode) VictorOps() *VictorOpsHandler {
vo := &VictorOpsHandler{
AlertNode: a,
}
a.VictorOpsHandlers = append(a.VictorOpsHandlers, vo)
return vo
}
// tick:embedded:AlertNode.VictorOps
type VictorOpsHandler struct {
*AlertNode
// The routing key to use for the alert.
// Defaults to the value in the configuration if empty.
RoutingKey string
}
// Send the alert to PagerDuty.
// To use PagerDuty alerting you must first follow the steps to enable a new 'Generic API' service.
//
// From https://developer.pagerduty.com/documentation/integration/events
//
// 1. In your account, under the Services tab, click "Add New Service".
// 2. Enter a name for the service and select an escalation policy. Then, select "Generic API" for the Service Type.
// 3. Click the "Add Service" button.
// 4. Once the service is created, you'll be taken to the service page. On this page, you'll see the "Service key", which is needed to access the API
//
// Place the 'service key' into the 'pagerduty' section of the Kapacitor configuration as the option 'service-key'.
//
// Example:
// [pagerduty]
// enabled = true
// service-key = "xxxxxxxxx"
//
// With the correct configuration you can now use PagerDuty in TICKscripts.
//
// Example:
// stream
// |alert()
// .pagerDuty()
//
// If the 'pagerduty' section in the configuration has the option: global = true
// then all alerts are sent to PagerDuty without the need to explicitly state it
// in the TICKscript.
//
// Example:
// [pagerduty]
// enabled = true
// service-key = "xxxxxxxxx"
// global = true
//
// Example:
// stream
// |alert()
//
// Send alert to PagerDuty.
// tick:property
func (a *AlertNode) PagerDuty() *PagerDutyHandler {
pd := &PagerDutyHandler{
AlertNode: a,
}
a.PagerDutyHandlers = append(a.PagerDutyHandlers, pd)
return pd
}
// tick:embedded:AlertNode.PagerDuty
type PagerDutyHandler struct {
*AlertNode
}
// Send the alert to HipChat.
// To allow Kapacitor to post to HipChat,
// go to the URL https://www.hipchat.com/docs/apiv2 for
// information on how to get your room id and tokens.
//
// Example:
// [hipchat]
// enabled = true
// url = "https://orgname.hipchat.com/v2/room"
// room = "4189212"
// token = "9hiWoDOZ9IbmHsOTeST123ABciWTIqXQVFDo63h9"
//
// In order to not post a message every alert interval
// use AlertNode.StateChangesOnly so that only events
// where the alert changed state are posted to the room.
//
// Example:
// stream
// |alert()
// .hipChat()
//
// Send alerts to HipChat room in the configuration file.
//
// Example:
// stream
// |alert()
// .hipChat()
// .room('Kapacitor')
//
// Send alerts to HipChat room 'Kapacitor'
//
// If the 'hipchat' section in the configuration has the option: global = true
// then all alerts are sent to HipChat without the need to explicitly state it
// in the TICKscript.
//
// Example:
// [hipchat]
// enabled = true
// url = "https://orgname.hipchat.com/v2/room"
// room = "Test Room"
// token = "9hiWoDOZ9IbmHsOTeST123ABciWTIqXQVFDo63h9"
// global = true
// state-changes-only = true
//
// Example:
// stream
// |alert()
//
// Send alert to HipChat using default room 'Test Room'.
// tick:property
func (a *AlertNode) HipChat() *HipChatHandler {
hipchat := &HipChatHandler{
AlertNode: a,
}
a.HipChatHandlers = append(a.HipChatHandlers, hipchat)
return hipchat
}
// tick:embedded:AlertNode.HipChat
type HipChatHandler struct {
*AlertNode
// HipChat room in which to post messages.
// If empty uses the channel from the configuration.
Room string
// HipChat authentication token.
// If empty uses the token from the configuration.
Token string
}
// Send the alert to Alerta.
//
// Example:
// [alerta]
// enabled = true
// url = "https://alerta.yourdomain"
// token = "9hiWoDOZ9IbmHsOTeST123ABciWTIqXQVFDo63h9"
// environment = "Production"
// origin = "Kapacitor"
//
// In order to not post a message every alert interval
// use AlertNode.StateChangesOnly so that only events
// where the alert changed state are sent to Alerta.
//
// Send alerts to Alerta. The resource and event properties are required.
//
// Example:
// stream
// |alert()
// .alerta()
// .resource('Hostname or service')
// .event('Something went wrong')
//
// Alerta also accepts optional alert information.
//
// Example:
// stream
// |alert()
// .alerta()
// .resource('Hostname or service')
// .event('Something went wrong')
// .environment('Development')
// .group('Dev. Servers')
//
// NOTE: Alerta cannot be configured globally because of its required properties.
// tick:property
func (a *AlertNode) Alerta() *AlertaHandler {
alerta := &AlertaHandler{
AlertNode: a,
Resource: defaultAlertaResource,
Event: defaultAlertaEvent,
Group: defaultAlertaGroup,
}
a.AlertaHandlers = append(a.AlertaHandlers, alerta)
return alerta
}
const defaultAlertaResource = "{{ .Name }}"
const defaultAlertaEvent = "{{ .ID }}"
const defaultAlertaGroup = "{{ .Group }}"
// tick:embedded:AlertNode.Alerta
type AlertaHandler struct {
*AlertNode
// Alerta authentication token.
// If empty uses the token from the configuration.
Token string
// Alerta resource.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default: {{ .Name }}
Resource string
// Alerta event.
// Can be a template and has access to the same data as the idInfo property.
// Default: {{ .ID }}
Event string
// Alerta environment.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Defaut is set from the configuration.
Environment string
// Alerta group.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default: {{ .Group }}
Group string
// Alerta value.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Default is an empty string.
Value string
// Alerta origin.
// If empty uses the origin from the configuration.
Origin string
// List of effected Services
// tick:ignore
Service []string `tick:"Services"`
}
// List of effected services.
// If not specified defaults to the Name of the stream.
func (a *AlertaHandler) Services(service ...string) *AlertaHandler {
a.Service = service
return a
}
// Send the alert to Sensu.
//
// Example:
// [sensu]
// enabled = true
// url = "http://sensu:3030"
// source = "Kapacitor"
//
// Example:
// stream
// |alert()
// .sensu()
//
// Send alerts to Sensu client.
//
// tick:property
func (a *AlertNode) Sensu() *SensuHandler {
sensu := &SensuHandler{
AlertNode: a,
}
a.SensuHandlers = append(a.SensuHandlers, sensu)
return sensu
}
// tick:embedded:AlertNode.Sensu
type SensuHandler struct {
*AlertNode
}
// Send the alert to Slack.
// To allow Kapacitor to post to Slack,
// go to the URL https://slack.com/services/new/incoming-webhook
// and create a new incoming webhook and place the generated URL
// in the 'slack' configuration section.
//
// Example:
// [slack]
// enabled = true
// url = "https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx"
// channel = "#general"
//
// In order to not post a message every alert interval
// use AlertNode.StateChangesOnly so that only events
// where the alert changed state are posted to the channel.
//
// Example:
// stream
// |alert()
// .slack()
//
// Send alerts to Slack channel in the configuration file.
//
// Example:
// stream
// |alert()
// .slack()
// .channel('#alerts')
//
// Send alerts to Slack channel '#alerts'
//
// Example:
// stream
// |alert()
// .slack()
// .channel('@jsmith')
//
// Send alert to user '@jsmith'
//
// If the 'slack' section in the configuration has the option: global = true
// then all alerts are sent to Slack without the need to explicitly state it
// in the TICKscript.
//
// Example:
// [slack]
// enabled = true
// url = "https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx"
// channel = "#general"
// global = true
// state-changes-only = true
//
// Example:
// stream
// |alert()
//
// Send alert to Slack using default channel '#general'.
// tick:property
func (a *AlertNode) Slack() *SlackHandler {
slack := &SlackHandler{
AlertNode: a,
}
a.SlackHandlers = append(a.SlackHandlers, slack)
return slack
}
// tick:embedded:AlertNode.Slack
type SlackHandler struct {
*AlertNode
// Slack channel in which to post messages.
// If empty uses the channel from the configuration.
Channel string
}
// Send alert to OpsGenie.
// To use OpsGenie alerting you must first enable the 'Alert Ingestion API'
// in the 'Integrations' section of OpsGenie.
// Then place the API key from the URL into the 'opsgenie' section of the Kapacitor configuration.
//
// Example:
// [opsgenie]
// enabled = true
// api-key = "xxxxx"
// teams = ["everyone"]
// recipients = ["jim", "bob"]
//
// With the correct configuration you can now use OpsGenie in TICKscripts.
//
// Example:
// stream
// |alert()
// .opsGenie()
//
// Send alerts to OpsGenie using the teams and recipients in the configuration file.
//
// Example:
// stream
// |alert()
// .opsGenie()
// .teams('team_rocket','team_test')
//
// Send alerts to OpsGenie with team set to 'team_rocket' and 'team_test'
//
// If the 'opsgenie' section in the configuration has the option: global = true
// then all alerts are sent to OpsGenie without the need to explicitly state it
// in the TICKscript.
//
// Example:
// [opsgenie]
// enabled = true
// api-key = "xxxxx"
// recipients = ["johndoe"]
// global = true
//
// Example:
// stream
// |alert()
//
// Send alert to OpsGenie using the default recipients, found in the configuration.
// tick:property
func (a *AlertNode) OpsGenie() *OpsGenieHandler {
og := &OpsGenieHandler{
AlertNode: a,
}
a.OpsGenieHandlers = append(a.OpsGenieHandlers, og)
return og
}
// tick:embedded:AlertNode.OpsGenie
type OpsGenieHandler struct {
*AlertNode
// OpsGenie Teams.
// tick:ignore
TeamsList []string `tick:"Teams"`
// OpsGenie Recipients.
// tick:ignore
RecipientsList []string `tick:"Recipients"`
}
// The list of teams to be alerted. If empty defaults to the teams from the configuration.
// tick:property
func (og *OpsGenieHandler) Teams(teams ...string) *OpsGenieHandler {
og.TeamsList = teams
return og
}
// The list of recipients to be alerted. If empty defaults to the recipients from the configuration.