2016-06-28 17:26:41 +00:00
|
|
|
package filestack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilestackWebhook struct {
|
|
|
|
Path string
|
|
|
|
acc telegraf.Accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
|
|
|
router.HandleFunc(fs.Path, fs.eventHandler).Methods("POST")
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Started the webhooks_filestack on %s\n", fs.Path)
|
2016-06-28 17:26:41 +00:00
|
|
|
fs.acc = acc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilestackWebhook) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
event := &FilestackEvent{}
|
|
|
|
err = json.Unmarshal(body, event)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.acc.AddFields("filestack_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0))
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|