forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
papertrail_webhooks.go
79 lines (67 loc) · 1.81 KB
/
papertrail_webhooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package papertrail
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
)
type PapertrailWebhook struct {
Path string
acc telegraf.Accumulator
}
func (pt *PapertrailWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
router.HandleFunc(pt.Path, pt.eventHandler).Methods("POST")
log.Printf("I! Started the papertrail_webhook on %s", pt.Path)
pt.acc = acc
}
func (pt *PapertrailWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
return
}
data := r.PostFormValue("payload")
if data == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
var payload Payload
err := json.Unmarshal([]byte(data), &payload)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if payload.Events != nil {
// Handle event-based payload
for _, e := range payload.Events {
// Warning: Duplicate event timestamps will overwrite each other
tags := map[string]string{
"host": e.Hostname,
"event": payload.SavedSearch.Name,
}
fields := map[string]interface{}{
"count": uint64(1),
}
pt.acc.AddFields("papertrail", fields, tags, e.ReceivedAt)
}
} else if payload.Counts != nil {
// Handle count-based payload
for _, c := range payload.Counts {
for ts, count := range *c.TimeSeries {
tags := map[string]string{
"host": c.SourceName,
"event": payload.SavedSearch.Name,
}
fields := map[string]interface{}{
"count": count,
}
pt.acc.AddFields("papertrail", fields, tags, time.Unix(ts, 0))
}
}
} else {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}