2016-07-18 11:41:13 +00:00
|
|
|
package mandrill
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-07-18 11:41:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MandrillWebhook struct {
|
|
|
|
Path string
|
2016-12-23 15:18:27 +00:00
|
|
|
acc plugins.Accumulator
|
2016-07-18 11:41:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
func (md *MandrillWebhook) Register(router *mux.Router, acc plugins.Accumulator) {
|
2016-07-18 11:41:13 +00:00
|
|
|
router.HandleFunc(md.Path, md.returnOK).Methods("HEAD")
|
|
|
|
router.HandleFunc(md.Path, md.eventHandler).Methods("POST")
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Started the webhooks_mandrill on %s\n", md.Path)
|
2016-07-18 11:41:13 +00:00
|
|
|
md.acc = acc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (md *MandrillWebhook) returnOK(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (md *MandrillWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := url.ParseQuery(string(body))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var events []MandrillEvent
|
|
|
|
err = json.Unmarshal([]byte(data.Get("mandrill_events")), &events)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
md.acc.AddFields("mandrill_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|