2016-05-27 15:27:54 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-06-01 17:35:08 +00:00
|
|
|
"reflect"
|
2016-05-27 15:27:54 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-06-01 17:35:08 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
|
2016-05-27 15:27:54 +00:00
|
|
|
)
|
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
type Webhook interface {
|
|
|
|
Register(router *mux.Router, acc telegraf.Accumulator)
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
func init() {
|
|
|
|
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
|
|
|
|
}
|
|
|
|
|
|
|
|
type Webhooks struct {
|
|
|
|
ServiceAddress string
|
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
Github *github.GithubWebhook
|
|
|
|
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-05-27 15:57:05 +00:00
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
[inputs.webhooks.github]
|
2016-05-27 15:57:05 +00:00
|
|
|
path = "/github"
|
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
[inputs.webhooks.rollbar]
|
2016-06-02 13:27:15 +00:00
|
|
|
path = "/rollbar"
|
2016-06-01 17:35:08 +00:00
|
|
|
`
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wb *Webhooks) Listen(acc telegraf.Accumulator) {
|
|
|
|
r := mux.NewRouter()
|
2016-06-01 17:35:08 +00:00
|
|
|
|
|
|
|
for _, webhook := range wb.AvailableWebhooks() {
|
|
|
|
webhook.Register(r, acc)
|
2016-05-27 15:27:54 +00:00
|
|
|
}
|
2016-06-01 17:35:08 +00:00
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error starting server: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
// 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)
|
|
|
|
|
2016-06-02 19:39:10 +00:00
|
|
|
if !f.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:35:08 +00:00
|
|
|
if wbPlugin, ok := f.Interface().(Webhook); ok {
|
2016-06-02 19:38:22 +00:00
|
|
|
if !reflect.ValueOf(wbPlugin).IsNil() {
|
|
|
|
webhooks = append(webhooks, wbPlugin)
|
|
|
|
}
|
2016-06-01 17:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhooks
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
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")
|
|
|
|
}
|