2016-05-27 15:27:54 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"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"
|
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
|
|
|
|
|
2016-06-28 17:26:41 +00:00
|
|
|
Github *github.GithubWebhook
|
|
|
|
Filestack *filestack.FilestackWebhook
|
|
|
|
Mandrill *mandrill.MandrillWebhook
|
|
|
|
Rollbar *rollbar.RollbarWebhook
|
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:38:12 +00:00
|
|
|
[inputs.webhooks.filestack]
|
|
|
|
path = "/filestack"
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
[inputs.webhooks.github]
|
|
|
|
path = "/github"
|
|
|
|
|
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"
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Description() string {
|
|
|
|
return "A Webhooks Event collector"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Gather(_ telegraf.Accumulator) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Listen(acc telegraf.Accumulator) {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
for _, webhook := range wb.AvailableWebhooks() {
|
|
|
|
webhook.Register(r, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error starting server: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
go wb.Listen(acc)
|
|
|
|
log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *Webhooks) Stop() {
|
|
|
|
log.Println("Stopping the Webhooks service")
|
|
|
|
}
|