-
Notifications
You must be signed in to change notification settings - Fork 29
/
splunk_event.go
147 lines (127 loc) · 3.74 KB
/
splunk_event.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package eventwriter
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"code.cloudfoundry.org/cfhttp"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry-community/splunk-firehose-nozzle/utils"
)
var keepAliveTimer = time.Now()
type SplunkConfig struct {
Host string
Token string
Index string
Fields map[string]string
SkipSSL bool
Debug bool
Version string
RefreshSplunkConnection bool
KeepAliveTimer time.Duration
Logger lager.Logger
}
type SplunkEvent struct {
httpClient *http.Client
config *SplunkConfig
BodyBufferSize utils.Counter
SentEventCount utils.Counter
}
func NewSplunkEvent(config *SplunkConfig) Writer {
httpClient := cfhttp.NewClient()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.SkipSSL, MinVersion: tls.VersionTLS12},
}
httpClient.Transport = tr
return &SplunkEvent{
httpClient: httpClient,
config: config,
BodyBufferSize: &utils.NopCounter{},
SentEventCount: &utils.NopCounter{},
}
}
func (s *SplunkEvent) Write(events []map[string]interface{}) (error, uint64) {
bodyBuffer := new(bytes.Buffer)
count := uint64(len(events))
for i, event := range events {
s.parseEvent(&event)
eventJson, err := json.Marshal(event)
if err == nil {
bodyBuffer.Write(eventJson)
if i < len(events)-1 {
bodyBuffer.Write([]byte("\n\n"))
}
} else {
s.config.Logger.Error("Error marshalling event", err,
lager.Data{
"event": fmt.Sprintf("%+v", event),
},
)
}
}
if s.config.Debug {
bodyString := bodyBuffer.String()
return s.dump(bodyString), count
} else {
bodyBytes := bodyBuffer.Bytes()
s.SentEventCount.Add(count)
return s.send(&bodyBytes), count
}
}
func (s *SplunkEvent) parseEvent(event *map[string]interface{}) error {
if _, ok := (*event)["index"]; !ok {
if (*event)["event"].(map[string]interface{})["info_splunk_index"] != nil {
(*event)["index"] = (*event)["event"].(map[string]interface{})["info_splunk_index"]
} else if s.config.Index != "" {
(*event)["index"] = s.config.Index
}
}
if len(s.config.Fields) > 0 {
(*event)["fields"] = s.config.Fields
}
return nil
}
func (s *SplunkEvent) send(postBody *[]byte) error {
endpoint := fmt.Sprintf("%s/services/collector", s.config.Host)
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(*postBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Authorization", fmt.Sprintf("Splunk %s", s.config.Token))
//Add app headers for HEC telemetry
req.Header.Set("__splunk_app_name", "Splunk Firehose Nozzle")
req.Header.Set("__splunk_app_version", s.config.Version)
resp, err := s.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
responseBody, _ := io.ReadAll(resp.Body)
return errors.New(fmt.Sprintf("Non-ok response code [%d] from splunk: %s", resp.StatusCode, responseBody))
} else {
if s.config.RefreshSplunkConnection && time.Now().After(keepAliveTimer) {
if s.config.KeepAliveTimer > 0 {
keepAliveTimer = time.Now().Add(s.config.KeepAliveTimer)
}
} else {
//Draining the response buffer, so that the same connection can be reused the next time
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
s.config.Logger.Error("Error discarding response body", err)
}
}
}
s.BodyBufferSize.Add(uint64(len(*postBody)))
return nil
}
// To dump the event on stdout instead of Splunk, in case of 'debug' mode
func (s *SplunkEvent) dump(eventString string) error {
fmt.Println(string(eventString))
return nil
}