...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package auditor
18
19 import (
20 "bytes"
21 "encoding/json"
22 "fmt"
23 "net/http"
24 "strings"
25 "time"
26
27 "github.com/sassoftware/relic/lib/audit"
28 )
29
30 func logGraylog(info *audit.Info, rowid int64) error {
31 if auditConfig.GraylogURL == "" {
32 return nil
33 }
34 msg := map[string]interface{}{
35 "version": "1.1",
36 "host": info.Attributes["sig.hostname"],
37 "short_message": fmtRow(info, rowid),
38 "level": 6,
39 }
40 if timestamp, err := time.Parse(time.RFC3339Nano, info.Attributes["sig.timestamp"].(string)); err == nil {
41 msg["timestamp"] = timestamp.Unix()
42 }
43 for k, v := range info.Attributes {
44 if v == nil {
45 continue
46 }
47
48
49
50 k = strings.Replace(k, ".", "_", -1)
51 msg["_"+k] = v
52 }
53 blob, err := json.Marshal(msg)
54 if err != nil {
55 return err
56 }
57 resp, err := http.Post(auditConfig.GraylogURL, "application/json", bytes.NewReader(blob))
58 if err != nil {
59 return err
60 } else if resp.StatusCode >= 300 {
61 return fmt.Errorf("error posting to graylog: %s", resp.Status)
62 }
63 resp.Body.Close()
64 return nil
65 }
66
View as plain text