-
Notifications
You must be signed in to change notification settings - Fork 21
/
gelf.go
172 lines (134 loc) · 3.18 KB
/
gelf.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gelf
import (
"bytes"
"compress/zlib"
"crypto/rand"
"encoding/binary"
"encoding/json"
"errors"
"log"
"math"
"net"
"strconv"
)
const (
defaultGraylogPort = 12201
defaultGraylogHostname = "127.0.0.1"
defaultConnection = "wan"
defaultMaxChunkSizeWan = 1420
defaultMaxChunkSizeLan = 8154
)
type Config struct {
GraylogPort int
GraylogHostname string
Connection string
MaxChunkSizeWan int
MaxChunkSizeLan int
}
type Gelf struct {
Config
}
func New(config Config) *Gelf {
if config.GraylogPort == 0 {
config.GraylogPort = defaultGraylogPort
}
if config.GraylogHostname == "" {
config.GraylogHostname = defaultGraylogHostname
}
if config.Connection == "" {
config.Connection = defaultConnection
}
if config.MaxChunkSizeWan == 0 {
config.MaxChunkSizeWan = defaultMaxChunkSizeWan
}
if config.MaxChunkSizeLan == 0 {
config.MaxChunkSizeLan = defaultMaxChunkSizeLan
}
g := &Gelf{
Config: config,
}
return g
}
func (g *Gelf) Log(message string) {
msgJson := g.ParseJson(message)
err := g.TestForForbiddenValues(msgJson)
if err != nil {
log.Printf("Uh oh! %s", err)
return
}
compressed := g.Compress([]byte(message))
chunksize := g.Config.MaxChunkSizeWan
length := compressed.Len()
if length > chunksize {
chunkCountInt := int(math.Ceil(float64(length) / float64(chunksize)))
id := make([]byte, 8)
rand.Read(id)
for i, index := 0, 0; i < length; i, index = i+chunksize, index+1 {
packet := g.CreateChunkedMessage(index, chunkCountInt, id, &compressed)
g.Send(packet.Bytes())
}
} else {
g.Send(compressed.Bytes())
}
}
func (g *Gelf) CreateChunkedMessage(index int, chunkCountInt int, id []byte, compressed *bytes.Buffer) bytes.Buffer {
var packet bytes.Buffer
chunksize := g.GetChunksize()
packet.Write(g.IntToBytes(30))
packet.Write(g.IntToBytes(15))
packet.Write(id)
packet.Write(g.IntToBytes(index))
packet.Write(g.IntToBytes(chunkCountInt))
packet.Write(compressed.Next(chunksize))
return packet
}
func (g *Gelf) GetChunksize() int {
if g.Config.Connection == "wan" {
return g.Config.MaxChunkSizeWan
}
if g.Config.Connection == "lan" {
return g.Config.MaxChunkSizeLan
}
return g.Config.MaxChunkSizeWan
}
func (g *Gelf) IntToBytes(i int) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, int8(i))
if err != nil {
log.Printf("Uh oh! %s", err)
}
return buf.Bytes()
}
func (g *Gelf) Compress(b []byte) bytes.Buffer {
var buf bytes.Buffer
comp := zlib.NewWriter(&buf)
comp.Write(b)
comp.Close()
return buf
}
func (g *Gelf) ParseJson(msg string) map[string]interface{} {
var i map[string]interface{}
c := []byte(msg)
json.Unmarshal(c, &i)
return i
}
func (g *Gelf) TestForForbiddenValues(gmap map[string]interface{}) error {
if _, err := gmap["_id"]; err {
return errors.New("Key _id is forbidden")
}
return nil
}
func (g *Gelf) Send(b []byte) {
var addr = g.Config.GraylogHostname + ":" + strconv.Itoa(g.Config.GraylogPort)
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
log.Printf("Uh oh! %s", err)
return
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Printf("Uh oh! %s", err)
return
}
conn.Write(b)
}