2016-05-27 15:27:54 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-09-11 18:51:45 +00:00
|
|
|
"net"
|
2016-05-27 15:27:54 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
2016-06-28 17:26:41 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/filestack"
|
2016-05-27 15:27:54 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
|
2016-07-18 11:41:13 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/mandrill"
|
2017-04-17 20:49:36 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/papertrail"
|
2016-05-27 15:27:54 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Webhook interface {
|
|
|
|
Register(router *mux.Router, acc telegraf.Accumulator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
|
|
|
|
}
|
|
|
|
|
|
|
|
type Webhooks struct {
|
|
|
|
ServiceAddress string
|
|
|
|
|
2017-04-17 20:49:36 +00:00
|
|
|
Github *github.GithubWebhook
|
|
|
|
Filestack *filestack.FilestackWebhook
|
|
|
|
Mandrill *mandrill.MandrillWebhook
|
|
|
|
Rollbar *rollbar.RollbarWebhook
|
|
|
|
Papertrail *papertrail.PapertrailWebhook
|
2017-09-11 18:51:45 +00:00
|
|
|
|
|
|
|
srv *http.Server
|
2016-05-27 15:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebhooks() *Webhooks {
|
|
|
|
return &Webhooks{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) SampleConfig() string {
|
|
|
|
return `
|
|
|
|
## Address and port to host Webhook listener on
|
|
|
|
service_address = ":1619"
|
|
|
|
|
2016-06-28 17:26:41 +00:00
|
|
|
[inputs.webhooks.filestack]
|
|
|
|
path = "/filestack"
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
[inputs.webhooks.github]
|
|
|
|
path = "/github"
|
2017-04-17 18:42:03 +00:00
|
|
|
# secret = ""
|
2016-05-27 15:27:54 +00:00
|
|
|
|
2016-07-18 11:41:13 +00:00
|
|
|
[inputs.webhooks.mandrill]
|
|
|
|
path = "/mandrill"
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
[inputs.webhooks.rollbar]
|
|
|
|
path = "/rollbar"
|
2017-04-17 20:49:36 +00:00
|
|
|
|
|
|
|
[inputs.webhooks.papertrail]
|
|
|
|
path = "/papertrail"
|
2016-05-27 15:27:54 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Description() string {
|
|
|
|
return "A Webhooks Event collector"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Gather(_ telegraf.Accumulator) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks for fields which implement Webhook interface
|
|
|
|
func (wb *Webhooks) AvailableWebhooks() []Webhook {
|
|
|
|
webhooks := make([]Webhook, 0)
|
|
|
|
s := reflect.ValueOf(wb).Elem()
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
|
|
|
|
if !f.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if wbPlugin, ok := f.Interface().(Webhook); ok {
|
|
|
|
if !reflect.ValueOf(wbPlugin).IsNil() {
|
|
|
|
webhooks = append(webhooks, wbPlugin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhooks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
|
2017-09-11 18:51:45 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
for _, webhook := range wb.AvailableWebhooks() {
|
|
|
|
webhook.Register(r, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.srv = &http.Server{Handler: r}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", fmt.Sprintf("%s", wb.ServiceAddress))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("E! Error starting server: %v", err)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := wb.srv.Serve(ln); err != nil {
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
acc.AddError(fmt.Errorf("E! Error listening: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Started the webhooks service on %s\n", wb.ServiceAddress)
|
2017-09-11 18:51:45 +00:00
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *Webhooks) Stop() {
|
2017-09-11 18:51:45 +00:00
|
|
|
rb.srv.Close()
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("I! Stopping the Webhooks service")
|
2016-05-27 15:27:54 +00:00
|
|
|
}
|