|
1 | 1 | package processor |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
4 | 6 | "encoding/json" |
| 7 | + "fmt" |
5 | 8 | "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
6 | 11 |
|
| 12 | + "github.com/threatwinds/logger" |
7 | 13 | "github.com/utmstack/UTMStack/office365/configuration" |
8 | 14 | "github.com/utmstack/UTMStack/office365/utils" |
9 | 15 | ) |
10 | 16 |
|
11 | | -func SendToCorrelation(data []TransformedLog) error { |
12 | | - utils.Logger.Info("uploading %d logs...", len(data)) |
| 17 | +var transport = &http.Transport{ |
| 18 | + MaxIdleConns: 100, |
| 19 | + IdleConnTimeout: 2 * time.Second, |
| 20 | + ResponseHeaderTimeout: 2 * time.Second, |
| 21 | + ForceAttemptHTTP2: true, |
| 22 | + TLSClientConfig: &tls.Config{ |
| 23 | + InsecureSkipVerify: true, |
| 24 | + }, |
| 25 | +} |
| 26 | + |
| 27 | +var client = &http.Client{Transport: transport, Timeout: 2 * time.Second} |
13 | 28 |
|
| 29 | +func SendToLogstash(data []TransformedLog) *logger.Error { |
| 30 | + var logStrings []string |
14 | 31 | for _, log := range data { |
15 | 32 | body, err := json.Marshal(log) |
16 | 33 | if err != nil { |
17 | 34 | utils.Logger.ErrorF("error encoding log to JSON: %v", err) |
18 | 35 | continue |
19 | 36 | } |
| 37 | + logStrings = append(logStrings, string(body)) |
| 38 | + } |
20 | 39 |
|
21 | | - _, status, e := utils.DoReq[map[string]interface{}](configuration.CORRELATIONURL, body, http.MethodPost, map[string]string{}) |
22 | | - if e != nil { |
23 | | - utils.Logger.ErrorF("error sending log to correlation engine: %v", e) |
24 | | - continue |
25 | | - } else if status != http.StatusOK && status != http.StatusCreated { |
26 | | - utils.Logger.ErrorF("error sending log to correlation engine: status code %d", status) |
27 | | - continue |
| 40 | + if len(logStrings) == 0 { |
| 41 | + return nil |
| 42 | + } |
| 43 | + |
| 44 | + var logs string |
| 45 | + for _, str := range logStrings { |
| 46 | + logs += str + configuration.UTMLogSeparator |
| 47 | + } |
| 48 | + |
| 49 | + url := fmt.Sprintf(configuration.LogstashEndpoint, configuration.GetLogstashHost(), configuration.GetLogstashPort()) |
| 50 | + |
| 51 | + req, err := http.NewRequest("POST", url, bytes.NewBufferString(logs)) |
| 52 | + if err != nil { |
| 53 | + return utils.Logger.ErrorF("error creating request: %v", err.Error()) |
| 54 | + } |
| 55 | + |
| 56 | + resp, err := client.Do(req) |
| 57 | + if err != nil { |
| 58 | + if !strings.Contains(err.Error(), "Client.Timeout exceeded while awaiting headers") { |
| 59 | + utils.Logger.ErrorF("error sending logs with error: %v", err.Error()) |
28 | 60 | } |
| 61 | + return utils.Logger.ErrorF("error sending logs: %v", err.Error()) |
| 62 | + } |
| 63 | + defer resp.Body.Close() |
29 | 64 |
|
30 | | - utils.Logger.Info("log successfully sent to correlation engine") |
| 65 | + if resp.StatusCode != http.StatusOK { |
| 66 | + return utils.Logger.ErrorF("error sending logs with http code %d", resp.StatusCode) |
31 | 67 | } |
32 | 68 |
|
33 | | - utils.Logger.Info("all logs were sent to correlation") |
| 69 | + utils.Logger.Info("successfully sent %d logs to Logstash", len(logStrings)) |
34 | 70 | return nil |
35 | 71 | } |
0 commit comments