forked from robertkowalski/graylog-golang
/
gelf.go
148 lines (116 loc) · 2.69 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
package gelf
import (
"bytes"
"compress/zlib"
"crypto/rand"
"encoding/binary"
"log"
"math"
"net"
)
const (
defaultGraylogEndpoint = "127.0.0.1:12201"
defaultConnection = "wan"
defaultMaxChunkSizeWan = 1420
defaultMaxChunkSizeLan = 8154
)
type Config struct {
GraylogEndpoint string
Connection string
MaxChunkSizeWan int
MaxChunkSizeLan int
}
type Gelf struct {
Config
}
func New(config Config) *Gelf {
if config.GraylogEndpoint == "" {
config.GraylogEndpoint = defaultGraylogEndpoint
}
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) Write(message []byte) (n int, err error) {
compressed := g.Compress(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)
_, err = g.Send(packet.Bytes())
if err != nil {
return 0, err
}
}
} else {
_, err = g.Send(compressed.Bytes())
if err != nil {
return 0, err
}
}
n = len(message)
return
}
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) Send(b []byte) (n int, err error) {
udpAddr, err := net.ResolveUDPAddr("udp", g.Config.GraylogEndpoint)
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
}
n, err = conn.Write(b)
return
}