Skip to content

Commit

Permalink
Allowing sensu handler to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
davidloutsch authored and henry-megarry-tr committed Apr 27, 2017
1 parent e23b6a4 commit b6f27d5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ kapacitor define-handler system aggregate_by_1m.yaml
- [#1286](https://github.com/influxdata/kapacitor/issues/1286): Default HipChat URL should be blank
- [#507](https://github.com/influxdata/kapacitor/issues/507): Add API endpoint for performing Kapacitor database backups.
- [#1132](https://github.com/influxdata/kapacitor/issues/1132): Adding source for sensu alert as parameter
- [#1299](https://github.com/influxdata/kapacitor/pull/1299): Allowing sensu handler to be specified

### Bugfixes

Expand Down
3 changes: 2 additions & 1 deletion alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, l *log.Logger) (an *

for _, s := range n.SensuHandlers {
c := sensu.HandlerConfig{
Source: s.Source,
Source: s.Source,
Handlers: s.HandlersList,
}
h, err := et.tm.SensuService.Handler(c, l)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pipeline/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ func (a *AlertaHandler) Services(service ...string) *AlertaHandler {
// enabled = true
// url = "http://sensu:3030"
// source = "Kapacitor"
// handlers = ["sns","slack"]
//
// Example:
// stream
Expand All @@ -957,6 +958,14 @@ func (a *AlertaHandler) Services(service ...string) *AlertaHandler {
//
// Send alerts to Sensu client.
//
// Example:
// stream
// |alert()
// .sensu()
// .handlers('sns','slack')
//
// Send alerts to Sensu specifying the handlers
//
// tick:property
func (a *AlertNode) Sensu() *SensuHandler {
sensu := &SensuHandler{
Expand All @@ -973,6 +982,19 @@ type SensuHandler struct {
// Sensu source in which to post messages.
// If empty uses the Source from the configuration.
Source string

// Sensu handler list
// If empty uses the handler list from the configuration
// tick:ignore
HandlersList []string `tick:"Handlers"`
}

// List of effected services.
// If not specified defaults to the Name of the stream.
// tick:property
func (s *SensuHandler) Handlers(handlers ...string) *SensuHandler {
s.HandlersList = handlers
return s
}

// Send the alert to Pushover.
Expand Down
46 changes: 26 additions & 20 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5978,19 +5978,21 @@ func TestServer_UpdateConfig(t *testing.T) {
Elements: []client.ConfigElement{{
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/config/sensu/"},
Options: map[string]interface{}{
"addr": "sensu.example.com:3000",
"enabled": false,
"source": "Kapacitor",
"addr": "sensu.example.com:3000",
"enabled": false,
"source": "Kapacitor",
"handlers": nil,
},
Redacted: nil,
}},
},
expDefaultElement: client.ConfigElement{
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/config/sensu/"},
Options: map[string]interface{}{
"addr": "sensu.example.com:3000",
"enabled": false,
"source": "Kapacitor",
"addr": "sensu.example.com:3000",
"enabled": false,
"source": "Kapacitor",
"handlers": nil,
},
Redacted: nil,
},
Expand All @@ -6008,19 +6010,21 @@ func TestServer_UpdateConfig(t *testing.T) {
Elements: []client.ConfigElement{{
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/config/sensu/"},
Options: map[string]interface{}{
"addr": "sensu.local:3000",
"enabled": true,
"source": "Kapacitor",
"addr": "sensu.local:3000",
"enabled": true,
"source": "Kapacitor",
"handlers": nil,
},
Redacted: nil,
}},
},
expElement: client.ConfigElement{
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/config/sensu/"},
Options: map[string]interface{}{
"addr": "sensu.local:3000",
"enabled": true,
"source": "Kapacitor",
"addr": "sensu.local:3000",
"enabled": true,
"source": "Kapacitor",
"handlers": nil,
},
Redacted: nil,
},
Expand Down Expand Up @@ -6630,10 +6634,11 @@ func TestServer_ListServiceTests(t *testing.T) {
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/service-tests/sensu"},
Name: "sensu",
Options: client.ServiceTestOptions{
"name": "testName",
"output": "testOutput",
"source": "Kapacitor",
"level": "CRITICAL",
"name": "testName",
"output": "testOutput",
"source": "Kapacitor",
"handlers": []interface{}{},
"level": "CRITICAL",
},
},
{
Expand Down Expand Up @@ -6732,10 +6737,11 @@ func TestServer_ListServiceTests_WithPattern(t *testing.T) {
Link: client.Link{Relation: client.Self, Href: "/kapacitor/v1/service-tests/sensu"},
Name: "sensu",
Options: client.ServiceTestOptions{
"name": "testName",
"output": "testOutput",
"source": "Kapacitor",
"level": "CRITICAL",
"name": "testName",
"output": "testOutput",
"source": "Kapacitor",
"handlers": []interface{}{},
"level": "CRITICAL",
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions services/sensu/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Config struct {
Addr string `toml:"addr" override:"addr"`
// The JIT sensu source name of the alert.
Source string `toml:"source" override:"source"`
// The sensu handler to use
Handlers []string `toml:"handlers" override:"handlers"`
}

func NewConfig() Config {
Expand Down
34 changes: 23 additions & 11 deletions services/sensu/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ func (s *Service) Update(newConfig []interface{}) error {
}

type testOptions struct {
Name string `json:"name"`
Source string `json:"source"`
Output string `json:"output"`
Level alert.Level `json:"level"`
Name string `json:"name"`
Source string `json:"source"`
Output string `json:"output"`
Handlers []string `json:"handlers"`
Level alert.Level `json:"level"`
}

func (s *Service) TestOptions() interface{} {
return &testOptions{
Name: "testName",
Source: "Kapacitor",
Output: "testOutput",
Level: alert.Critical,
Name: "testName",
Source: "Kapacitor",
Output: "testOutput",
Handlers: []string{},
Level: alert.Critical,
}
}

Expand All @@ -79,16 +81,17 @@ func (s *Service) Test(options interface{}) error {
o.Name,
o.Source,
o.Output,
o.Handlers,
o.Level,
)
}

func (s *Service) Alert(name, source, output string, level alert.Level) error {
func (s *Service) Alert(name, source, output string, handlers []string, level alert.Level) error {
if !validNamePattern.MatchString(name) {
return fmt.Errorf("invalid name %q for sensu alert. Must match %v", name, validNamePattern)
}

addr, postData, err := s.prepareData(name, source, output, level)
addr, postData, err := s.prepareData(name, source, output, handlers, level)
if err != nil {
return err
}
Expand All @@ -114,7 +117,7 @@ func (s *Service) Alert(name, source, output string, level alert.Level) error {
return nil
}

func (s *Service) prepareData(name, source, output string, level alert.Level) (*net.TCPAddr, map[string]interface{}, error) {
func (s *Service) prepareData(name, source, output string, handlers []string, level alert.Level) (*net.TCPAddr, map[string]interface{}, error) {

c := s.config()

Expand Down Expand Up @@ -144,6 +147,10 @@ func (s *Service) prepareData(name, source, output string, level alert.Level) (*
postData["source"] = source
postData["output"] = output
postData["status"] = status
if len(handlers) == 0 {
handlers = c.Handlers
}
postData["handlers"] = handlers

addr, err := net.ResolveTCPAddr("tcp", c.Addr)
if err != nil {
Expand All @@ -157,6 +164,10 @@ type HandlerConfig struct {
// Sensu source for which to post messages.
// If empty uses the source from the configuration.
Source string `mapstructure:"source"`

// Sensu handler list
// If empty uses the handler list from the configuration
Handlers []string `mapstructure:"handlers"`
}

type handler struct {
Expand Down Expand Up @@ -194,6 +205,7 @@ func (h *handler) Handle(event alert.Event) {
event.State.ID,
sourceStr,
event.State.Message,
h.c.Handlers,
event.State.Level,
); err != nil {
h.logger.Println("E! failed to send event to Sensu", err)
Expand Down

0 comments on commit b6f27d5

Please sign in to comment.