forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
1960 lines (1785 loc) · 55.8 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 (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/pkg/errors"
)
// 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 . }}"
// AlertNode struct wraps the default AlertNodeData
// tick:wraps:AlertNodeData
type AlertNode struct{ *AlertNodeData }
// 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, Telegram 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.
// * tcp -- Send data to a specified address via raw TCP.
// * 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.
// * SNMPTraps -- Trigger SNMP traps.
// * OpsGenie -- Send alert to OpsGenie.
// * VictorOps -- Send alert to VictorOps.
// * PagerDuty -- Send alert to PagerDuty.
// * Pushover -- Send alert to Pushover.
// * Talk -- Post alert message to Talk client.
// * Telegram -- Post alert message to Telegram client.
// * MQTT -- Post alert message to MQTT.
//
// 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")
// .tcp("exampleendpoint.com:5678")
// .email('[email protected]')
//
//
// Each expression maintains its own state.
// The order of execution for the expressions is not considered to be deterministic.
// For each point an expression may or may not be evaluated.
// If no expression is true then the alert is considered to be in the OK state.
//
// Kapacitor supports alert reset expressions.
// This way when an alert enters a state, it can only be lowered in severity if its reset expression evaluates to true.
//
// Example:
// stream
// |from()
// .measurement('cpu')
// .where(lambda: "host" == 'serverA')
// .groupBy('host')
// |alert()
// .info(lambda: "value" > 60)
// .infoReset(lambda: "value" < 50)
// .warn(lambda: "value" > 70)
// .warnReset(lambda: "value" < 60)
// .crit(lambda: "value" > 80)
// .critReset(lambda: "value" < 70)
//
// For example given the following values:
// 61 73 64 85 62 56 47
// The corresponding alert states are:
// INFO WARNING WARNING CRITICAL INFO INFO OK
//
// 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 AlertNodeData struct {
chainnode
// Category places this alert in a named category.
// Categories are used to inhibit alerts.
Category string `json:"category"`
// Topic specifies the name of an alert topic to which,
// alerts will be published.
// Alert handlers can be configured per topic, see the API documentation.
Topic string `json:"topic"`
// 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.
// * ServerInfo -- Information about the running server. Available nested fields are:
// Hostname, ClusterID and ServerID.
//
// 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 `json:"alertId"`
// 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.
// * Duration -- The duration of the alert.
//
// 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 `json:"message"`
// 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 `json:"details"`
// Filter expression for the INFO alert level.
// An empty value indicates the level is invalid and is skipped.
Info *ast.LambdaNode `json:"info"`
// Filter expression for the WARNING alert level.
// An empty value indicates the level is invalid and is skipped.
Warn *ast.LambdaNode `json:"warn"`
// Filter expression for the CRITICAL alert level.
// An empty value indicates the level is invalid and is skipped.
Crit *ast.LambdaNode `json:"crit"`
// Filter expression for reseting the INFO alert level to lower level.
InfoReset *ast.LambdaNode `json:"infoReset"`
// Filter expression for reseting the WARNING alert level to lower level.
WarnReset *ast.LambdaNode `json:"warnReset"`
// Filter expression for reseting the CRITICAL alert level to lower level.
CritReset *ast.LambdaNode `json:"critReset"`
//tick:ignore
UseFlapping bool `tick:"Flapping" json:"useFlapping"`
//tick:ignore
FlapLow float64 `json:"flapLow"`
//tick:ignore
FlapHigh float64 `json:"flapHigh"`
// 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 `json:"history"`
// Optional tag key to use when tagging the data with the alert level.
LevelTag string `json:"levelTag"`
// Optional field key to add to the data, containing the alert level as a string.
LevelField string `json:"levelField"`
// Optional field key to add to the data, containing the alert message.
MessageField string `json:"messageField"`
// Optional field key to add the alert duration to the data.
// The duration is always in units of nanoseconds.
DurationField string `json:"durationField"`
// Optional tag key to use when tagging the data with the alert ID.
IdTag string `json:"idTag"`
// Optional field key to add to the data, containing the alert ID as a string.
IdField string `json:"idField"`
// Indicates an alert should trigger only if all points in a batch match the criteria
// tick:ignore
AllFlag bool `tick:"All" json:"all"`
// Do not send recovery events.
// tick:ignore
NoRecoveriesFlag bool `tick:"NoRecoveries" json:"noRecoveries"`
// Send alerts only on state changes.
// tick:ignore
IsStateChangesOnly bool `tick:"StateChangesOnly" json:"stateChangesOnly"`
// Maximum interval to ignore non state changed events
// tick:ignore
StateChangesOnlyDuration time.Duration `json:"stateChangesOnlyDuration"`
// Inhibitors
// tick:ignore
Inhibitors []Inhibitor `tick:"Inhibit" json:"inhibitors"`
// Post the JSON alert data to the specified URL.
// tick:ignore
HTTPPostHandlers []*AlertHTTPPostHandler `tick:"Post" json:"post"`
// Send the JSON alert data to the specified endpoint via TCP.
// tick:ignore
TcpHandlers []*TcpHandler `tick:"Tcp" json:"tcp"`
// Email handlers
// tick:ignore
EmailHandlers []*EmailHandler `tick:"Email" json:"email"`
// A commands to run when an alert triggers
// tick:ignore
ExecHandlers []*ExecHandler `tick:"Exec" json:"exec"`
// Log JSON alert data to file. One event per line.
// tick:ignore
LogHandlers []*LogHandler `tick:"Log" json:"log"`
// Send alert to VictorOps.
// tick:ignore
VictorOpsHandlers []*VictorOpsHandler `tick:"VictorOps" json:"victorOps"`
// Send alert to PagerDuty.
// tick:ignore
PagerDutyHandlers []*PagerDutyHandler `tick:"PagerDuty" json:"pagerDuty"`
// Send alert to PagerDuty API v2.
// tick:ignore
PagerDuty2Handlers []*PagerDuty2Handler `tick:"PagerDuty2" json:"pagerDuty2"`
// Send alert to Pushover.
// tick:ignore
PushoverHandlers []*PushoverHandler `tick:"Pushover" json:"pushover"`
// Send alert to Sensu.
// tick:ignore
SensuHandlers []*SensuHandler `tick:"Sensu" json:"sensu"`
// Send alert to Slack.
// tick:ignore
SlackHandlers []*SlackHandler `tick:"Slack" json:"slack"`
// Send alert to Telegram.
// tick:ignore
TelegramHandlers []*TelegramHandler `tick:"Telegram" json:"telegram"`
// Send alert to HipChat.
// tick:ignore
HipChatHandlers []*HipChatHandler `tick:"HipChat" json:"hipChat"`
// Send alert to Alerta.
// tick:ignore
AlertaHandlers []*AlertaHandler `tick:"Alerta" json:"alerta"`
// Send alert to OpsGenie
// tick:ignore
OpsGenieHandlers []*OpsGenieHandler `tick:"OpsGenie" json:"opsGenie"`
// Send alert to OpsGenie using v2 API
// tick:ignore
OpsGenie2Handlers []*OpsGenie2Handler `tick:"OpsGenie2" json:"opsGenie2"`
// Send alert to Talk.
// tick:ignore
TalkHandlers []*TalkHandler `tick:"Talk" json:"talk"`
// Send alert to MQTT
// tick:ignore
MQTTHandlers []*MQTTHandler `tick:"Mqtt" json:"mqtt"`
// Send alert using SNMPtraps.
// tick:ignore
SNMPTrapHandlers []*SNMPTrapHandler `tick:"SnmpTrap" json:"snmpTrap"`
// Send alert to Kafka topic
// tick:ignore
KafkaHandlers []*KafkaHandler `tick:"Kafka" json:"kafka"`
}
func newAlertNode(wants EdgeType) *AlertNode {
a := &AlertNode{
AlertNodeData: &AlertNodeData{
chainnode: newBasicChainNode("alert", wants, wants),
History: defaultFlapHistory,
Id: defaultIDTmpl,
Message: defaultMessageTmpl,
Details: defaultDetailsTmpl,
},
}
return a
}
// MarshalJSON converts AlertNode to JSON
// tick:ignore
func (n *AlertNode) MarshalJSON() ([]byte, error) {
type Alias AlertNodeData
var raw = &struct {
TypeOf
*Alias
}{
TypeOf: TypeOf{
Type: "alert",
ID: n.ID(),
},
Alias: (*Alias)(n.AlertNodeData),
}
return json.Marshal(raw)
}
// UnmarshalJSON converts JSON to an AlertNode
// tick:ignore
func (n *AlertNode) UnmarshalJSON(data []byte) error {
type Alias AlertNode
var raw = &struct {
TypeOf
*Alias
}{
Alias: (*Alias)(n),
}
err := json.Unmarshal(data, raw)
if err != nil {
return err
}
if raw.Type != "alert" {
return fmt.Errorf("error unmarshaling node %d of type %s as AlertNode", raw.ID, raw.Type)
}
n.setID(raw.ID)
return nil
}
//tick:ignore
func (n *AlertNodeData) ChainMethods() map[string]reflect.Value {
return map[string]reflect.Value{
"Log": reflect.ValueOf(n.chainnode.Log),
}
}
func (n *AlertNodeData) validate() error {
for _, snmp := range n.SNMPTrapHandlers {
if err := snmp.validate(); err != nil {
return errors.Wrapf(err, "invalid SNMP trap %q", snmp.TrapOid)
}
}
for _, post := range n.HTTPPostHandlers {
if err := post.validate(); err != nil {
return errors.Wrap(err, "invalid post")
}
}
return nil
}
// 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 *AlertNodeData) All() *AlertNodeData {
n.AllFlag = true
return n
}
// Do not send recovery alerts.
// tick:property
func (n *AlertNodeData) NoRecoveries() *AlertNodeData {
n.NoRecoveriesFlag = 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 above usage will only trigger alerts to slack on state changes or at least every 10 minutes.
//
// tick:property
func (n *AlertNodeData) StateChangesOnly(maxInterval ...time.Duration) *AlertNodeData {
n.IsStateChangesOnly = true
if len(maxInterval) == 1 {
n.StateChangesOnlyDuration = maxInterval[0]
}
return n
}
// 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 (n *AlertNodeData) Flapping(low, high float64) *AlertNodeData {
n.UseFlapping = true
n.FlapLow = low
n.FlapHigh = high
return n
}
// Inhibit other alerts in a category.
// The equal tags provides a list of tags that must be equal in order for an alert event to be inhibited.
//
// The following two TICKscripts demonstrate how to use the inhibit feature.
//
// Example:
// //cpu_alert.tick
// stream
// |from()
// .measurement('cpu')
// .groupBy('host')
// |alert()
// .category('system_alerts')
// .crit(lambda: "usage_idle" < 10.0)
//
// //host_alert.tick
// stream
// |from()
// .measurement('uptime')
// .groupBy('host')
// |deadman(0.0, 1m)
// .inhibit('system_alerts', 'host')
//
// The deadman is a kind of alert node and so can be used to inhibit all alerts in the `system_alerts` category when it triggers.
// The 'host` argument to the inhibit function says that the host tag must be equal between the cpu alert and the host alert in order for it to be inhibited.
// This has the effect of the deadman alerts only inhibits cpu alerts for hosts are are currently dead.
//
// tick:property
func (n *AlertNodeData) Inhibit(category string, equalTags ...string) *AlertNodeData {
n.Inhibitors = append(n.Inhibitors, Inhibitor{
Category: category,
EqualTags: equalTags,
})
return n
}
// Inhibitor represents a single alert inhibitor
// tick:ignore
type Inhibitor struct {
Category string `json:"category"`
EqualTags []string `json:"equalTags"`
}
// HTTP POST JSON alert data to a specified URL.
//
// Example:
// stream
// |alert()
// .post()
// .endpoint('example')
//
// Example:
// stream
// |alert()
// .post('http://example.com')
//
// tick:property
func (n *AlertNodeData) Post(urls ...string) *AlertHTTPPostHandler {
post := &AlertHTTPPostHandler{
AlertNodeData: n,
}
n.HTTPPostHandlers = append(n.HTTPPostHandlers, post)
if len(urls) == 0 {
return post
}
post.URL = urls[0]
return post
}
// tick:embedded:AlertNode.Post
type AlertHTTPPostHandler struct {
*AlertNodeData `json:"-"`
// The POST URL.
// tick:ignore
URL string `json:"url"`
// Name of the endpoint to be used, as is defined in the configuration file
Endpoint string `json:"endpoint"`
// tick:ignore
Headers map[string]string `tick:"Header" json:"headers"`
// tick:ignore
CaptureResponseFlag bool `tick:"CaptureResponse" json:"captureResponse"`
// Timeout for HTTP Post
Timeout time.Duration `json:"timeout"`
// tick:ignore
SkipSSLVerificationFlag bool `tick:"SkipSSLVerification" json:"skipSSLVerification"`
}
// Set a header key and value on the post request.
// Setting the Authenticate header is not allowed from within TICKscript,
// please use the configuration file to specify sensitive headers.
//
// Example:
// stream
// |alert()
// .post()
// .endpoint('example')
// .header('a','b')
// tick:property
func (a *AlertHTTPPostHandler) Header(k, v string) *AlertHTTPPostHandler {
if a.Headers == nil {
a.Headers = map[string]string{}
}
a.Headers[k] = v
return a
}
// CaptureResponse indicates that the HTTP response should be read and logged if
// the status code was not an 2xx code.
// tick:property
func (a *AlertHTTPPostHandler) CaptureResponse() *AlertHTTPPostHandler {
a.CaptureResponseFlag = true
return a
}
// SkipSSLVerification disables ssl verification for the POST request
// Example:
// stream
// |alert()
// .post()
// .endpoint('https'://user@pw/example/resource')
// .skipSSLVerification()
// tick:property
func (a *AlertHTTPPostHandler) SkipSSLVerification() *AlertHTTPPostHandler {
a.SkipSSLVerificationFlag = true
return a
}
func (a *AlertHTTPPostHandler) validate() error {
for k := range a.Headers {
if strings.ToUpper(k) == "AUTHENTICATE" {
return errors.New("cannot set 'authenticate' header")
}
}
return nil
}
// Send JSON alert data to a specified address over TCP.
// tick:property
func (n *AlertNodeData) Tcp(address string) *TcpHandler {
tcp := &TcpHandler{
AlertNodeData: n,
Address: address,
}
n.TcpHandlers = append(n.TcpHandlers, tcp)
return tcp
}
// tick:embedded:AlertNode.Tcp
type TcpHandler struct {
*AlertNodeData `json:"-"`
// The endpoint address.
Address string `json:"address"`
}
// 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
// .message('{{ .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 = "[email protected]"
// to = ["[email protected]"]
// # Set global to true so all alert trigger emails.
// global = true
// state-changes-only = true
//
// Example:
// stream
// |alert()
//
// Send email to '[email protected]' from '[email protected]'
//
// tick:property
func (n *AlertNodeData) Email(to ...string) *EmailHandler {
em := &EmailHandler{
AlertNodeData: n,
ToList: to,
}
n.EmailHandlers = append(n.EmailHandlers, em)
return em
}
// Email AlertHandler
// tick:embedded:AlertNode.Email
type EmailHandler struct {
*AlertNodeData `json:"-"`
// List of email recipients.
// tick:ignore
ToList []string `tick:"To" json:"to"`
}
// Define the To addresses for the email alert.
// Multiple calls append to the existing list of addresses.
// If empty uses the addresses from the configuration.
//
// Example:
// |alert()
// .id('{{ .Name }}')
// // Email subject
// .message('{{ .ID }}:{{ .Level }}')
// //Email body as HTML
// .details('''
//<h1>{{ .ID }}</h1>
//<b>{{ .Message }}</b>
//Value: {{ index .Fields "value" }}
//''')
// .email('[email protected]')
// .to('[email protected]')
// .to('[email protected]')
//
// All three email addresses will receive the alert message.
//
// Passing addresses to the `email` property directly or using the `email.to` property is the same.
// tick:property
func (h *EmailHandler) To(to ...string) *EmailHandler {
h.ToList = append(h.ToList, to...)
return h
}
// Execute a command whenever an alert is triggered and pass the alert data over STDIN in JSON format.
// tick:property
func (n *AlertNodeData) Exec(executable string, args ...string) *ExecHandler {
exec := &ExecHandler{
AlertNodeData: n,
Command: append([]string{executable}, args...),
}
n.ExecHandlers = append(n.ExecHandlers, exec)
return exec
}
// tick:embedded:AlertNode.Exec
type ExecHandler struct {
*AlertNodeData `json:"-"`
// The command to execute
// tick:ignore
Command []string `json:"command"`
}
// 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 (n *AlertNodeData) Log(filepath string) *LogHandler {
log := &LogHandler{
AlertNodeData: n,
FilePath: filepath,
}
n.LogHandlers = append(n.LogHandlers, log)
return log
}
// tick:embedded:AlertNode.Log
type LogHandler struct {
*AlertNodeData `json:"-"`
// Absolute path the the log file.
// It will be created if it does not exist.
// tick:ignore
FilePath string `json:"filePath"`
// File's mode and permissions, default is 0600
// NOTE: The leading 0 is required to interpret the value as an octal integer.
Mode int64 `json:"mode"`
}
// 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 (n *AlertNodeData) VictorOps() *VictorOpsHandler {
vo := &VictorOpsHandler{
AlertNodeData: n,
}
n.VictorOpsHandlers = append(n.VictorOpsHandlers, vo)
return vo
}
// tick:embedded:AlertNode.VictorOps
type VictorOpsHandler struct {
*AlertNodeData `json:"-"`
// The routing key to use for the alert.
// Defaults to the value in the configuration if empty.
RoutingKey string `json:"routingKey"`
}
// 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 (n *AlertNodeData) PagerDuty() *PagerDutyHandler {
pd := &PagerDutyHandler{
AlertNodeData: n,
}
n.PagerDutyHandlers = append(n.PagerDutyHandlers, pd)
return pd
}
// tick:embedded:AlertNode.PagerDuty
type PagerDutyHandler struct {
*AlertNodeData `json:"-"`
// The service key to use for the alert.
// Defaults to the value in the configuration if empty.
ServiceKey string `json:"serviceKey"`
}
// Send the alert to PagerDuty API v2.
// To use PagerDuty alerting you must first follow the steps to enable a new 'Generic API' service.
// NOTE: the API v2 endpoint is different and requires a new configuration in order to process/handle alerts
//
// 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 "Integration key", which is needed to access the API
//
// Place the 'integration key' into the 'pagerduty' section of the Kapacitor configuration as the option 'routing-key'.
//
// Example:
// [pagerduty2]
// enabled = true
// routing-key = "xxxxxxxxx"
//
// With the correct configuration you can now use PagerDuty in TICKscripts.