Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
22
plugins/inputs/webhooks/mandrill/README.md
Normal file
22
plugins/inputs/webhooks/mandrill/README.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
# mandrill webhook
|
||||
|
||||
You should configure your Mandrill's Webhooks to point at the `webhooks`
|
||||
service. To do this go to [mandrillapp.com](https://mandrillapp.com) and click
|
||||
`Settings > Webhooks`. In the resulting page, click on `Add a Webhook`, select
|
||||
all events, and set the `URL` to `http://<my_ip>:1619/mandrill`, and click on
|
||||
`Create Webhook`.
|
||||
|
||||
## Events
|
||||
|
||||
See the [webhook doc](https://mandrill.zendesk.com/hc/en-us/articles/205583307-Message-Event-Webhook-format).
|
||||
|
||||
All events for logs the original timestamp, the event name and the unique
|
||||
identifier of the message that generated the event.
|
||||
|
||||
**Tags:**
|
||||
|
||||
* 'event' = `event.event` string
|
||||
|
||||
**Fields:**
|
||||
|
||||
* 'id' = `event._id` string
|
67
plugins/inputs/webhooks/mandrill/mandrill_webhooks.go
Normal file
67
plugins/inputs/webhooks/mandrill/mandrill_webhooks.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package mandrill
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/common/auth"
|
||||
)
|
||||
|
||||
type Webhook struct {
|
||||
Path string
|
||||
acc telegraf.Accumulator
|
||||
log telegraf.Logger
|
||||
auth.BasicAuth
|
||||
}
|
||||
|
||||
// Register registers the webhook with the provided router
|
||||
func (md *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
|
||||
router.HandleFunc(md.Path, returnOK).Methods("HEAD")
|
||||
router.HandleFunc(md.Path, md.eventHandler).Methods("POST")
|
||||
|
||||
md.log = log
|
||||
md.log.Infof("Started the webhooks_mandrill on %s", md.Path)
|
||||
md.acc = acc
|
||||
}
|
||||
|
||||
func returnOK(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (md *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
if !md.Verify(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.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)
|
||||
}
|
19
plugins/inputs/webhooks/mandrill/mandrill_webhooks_events.go
Normal file
19
plugins/inputs/webhooks/mandrill/mandrill_webhooks_events.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package mandrill
|
||||
|
||||
type mandrillEvent struct {
|
||||
EventName string `json:"event"`
|
||||
TimeStamp int64 `json:"ts"`
|
||||
ID string `json:"_id"`
|
||||
}
|
||||
|
||||
func (me *mandrillEvent) tags() map[string]string {
|
||||
return map[string]string{
|
||||
"event": me.EventName,
|
||||
}
|
||||
}
|
||||
|
||||
func (me *mandrillEvent) fields() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"id": me.ID,
|
||||
}
|
||||
}
|
97
plugins/inputs/webhooks/mandrill/mandrill_webhooks_test.go
Normal file
97
plugins/inputs/webhooks/mandrill/mandrill_webhooks_test.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package mandrill
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func postWebhooks(t *testing.T, md *Webhook, eventBody string) *httptest.ResponseRecorder {
|
||||
body := url.Values{}
|
||||
body.Set("mandrill_events", eventBody)
|
||||
req, err := http.NewRequest("POST", "/mandrill", strings.NewReader(body.Encode()))
|
||||
require.NoError(t, err)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
md.eventHandler(w, req)
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
func headRequest(t *testing.T) *httptest.ResponseRecorder {
|
||||
req, err := http.NewRequest("HEAD", "/mandrill", strings.NewReader(""))
|
||||
require.NoError(t, err)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
returnOK(w, req)
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
func TestHead(t *testing.T) {
|
||||
resp := headRequest(t)
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Errorf("HEAD returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendEvent(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
md := &Webhook{Path: "/mandrill", acc: &acc}
|
||||
resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+"]")
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"id": "id1",
|
||||
}
|
||||
|
||||
tags := map[string]string{
|
||||
"event": "send",
|
||||
}
|
||||
|
||||
acc.AssertContainsTaggedFields(t, "mandrill_webhooks", fields, tags)
|
||||
}
|
||||
|
||||
func TestMultipleEvents(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
md := &Webhook{Path: "/mandrill", acc: &acc}
|
||||
resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+","+readFile(t, "testdata/hard_bounce_event.json")+"]")
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"id": "id1",
|
||||
}
|
||||
|
||||
tags := map[string]string{
|
||||
"event": "send",
|
||||
}
|
||||
|
||||
acc.AssertContainsTaggedFields(t, "mandrill_webhooks", fields, tags)
|
||||
|
||||
fields = map[string]interface{}{
|
||||
"id": "id2",
|
||||
}
|
||||
|
||||
tags = map[string]string{
|
||||
"event": "hard_bounce",
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "mandrill_webhooks", fields, tags)
|
||||
}
|
||||
|
||||
func readFile(t *testing.T, filePath string) string {
|
||||
data, err := os.ReadFile(filePath)
|
||||
require.NoErrorf(t, err, "could not read from file %s", filePath)
|
||||
|
||||
return string(data)
|
||||
}
|
23
plugins/inputs/webhooks/mandrill/testdata/hard_bounce_event.json
vendored
Normal file
23
plugins/inputs/webhooks/mandrill/testdata/hard_bounce_event.json
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"event": "hard_bounce",
|
||||
"msg": {
|
||||
"ts": 1365109999,
|
||||
"subject": "This an example webhook message",
|
||||
"email": "example.webhook@mandrillapp.com",
|
||||
"sender": "example.sender@mandrillapp.com",
|
||||
"tags": [
|
||||
"webhook-example"
|
||||
],
|
||||
"state": "bounced",
|
||||
"metadata": {
|
||||
"user_id": 111
|
||||
},
|
||||
"_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa2",
|
||||
"_version": "exampleaaaaaaaaaaaaaaa",
|
||||
"bounce_description": "bad_mailbox",
|
||||
"bgtools_code": 10,
|
||||
"diag": "smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
|
||||
},
|
||||
"_id": "id2",
|
||||
"ts": 1384954004
|
||||
}
|
26
plugins/inputs/webhooks/mandrill/testdata/send_event.json
vendored
Normal file
26
plugins/inputs/webhooks/mandrill/testdata/send_event.json
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"event": "send",
|
||||
"msg": {
|
||||
"ts": 1365109999,
|
||||
"subject": "This an example webhook message",
|
||||
"email": "example.webhook@mandrillapp.com",
|
||||
"sender": "example.sender@mandrillapp.com",
|
||||
"tags": [
|
||||
"webhook-example"
|
||||
],
|
||||
"opens": [
|
||||
|
||||
],
|
||||
"clicks": [
|
||||
|
||||
],
|
||||
"state": "sent",
|
||||
"metadata": {
|
||||
"user_id": 111
|
||||
},
|
||||
"_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"_version": "exampleaaaaaaaaaaaaaaa"
|
||||
},
|
||||
"_id": "id1",
|
||||
"ts": 1384954004
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue