-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.go
More file actions
173 lines (145 loc) · 4.26 KB
/
server.go
File metadata and controls
173 lines (145 loc) · 4.26 KB
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
173
package main
import (
"github.com/golang/glog"
"net"
"strconv"
"sync"
)
// Server holds one (1) UDP connection. Messages are read from the 'incoming'
// channel to be parsed and processed and exporters will read from the
// 'outgoing' channel for exports
type Server struct {
conn *net.UDPConn
connMu *sync.RWMutex
incoming chan *Message
outgoing chan string
exit chan interface{}
}
// NewServer returns a new server instance with an active UDP connection.
func NewServer() *Server {
server := &Server{
conn: nil,
connMu: new(sync.RWMutex),
incoming: make(chan *Message),
outgoing: make(chan string),
exit: make(chan interface{}),
}
parseOptions() // parse program options
server.Listen()
for server.getConn() == nil {
} // wait for connection
return server
}
// returns a concurrent-safe UDP connection.
func (server *Server) getConn() *net.UDPConn {
server.connMu.RLock()
defer server.connMu.RUnlock()
return server.conn
}
// sets the server UDP connection.
func (server *Server) setConn(conn *net.UDPConn) {
server.connMu.Lock()
defer server.connMu.Unlock()
server.conn = conn
}
// Listen does wait for incoming UDP messages
func (server *Server) Listen() {
service := globalServerOptions.address + ":" + strconv.Itoa(globalServerOptions.port)
udpAddr, _ := net.ResolveUDPAddr("udp", service)
go func() {
glog.Infoln("UDP server up and listening on port", string(service))
glog.Infoln("It can take up to 1 minute for messages to start " +
"coming in: waiting for IPFIX template sync.")
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
panic(err)
}
defer conn.Close()
err = conn.SetReadBuffer(globalServerOptions.rcvbuf)
if err != nil {
glog.Errorln(err)
}
err = conn.SetWriteBuffer(globalServerOptions.sndbuf)
if err != nil {
glog.Errorln(err)
}
server.setConn(conn)
for {
select {
case in := <-server.incoming:
server.Parse(in)
case out := <-server.outgoing:
server.Export(out)
case <-server.exit:
return
}
}
}()
}
// read UDP messages and process their IPFIX payload given an IPFIX context
func (server *Server) Read(ipfixContext *IpfixContext, exit chan interface{}) {
err := error(nil)
var errCount uint // error count for retry mechanism
for err == nil && errCount < maxRetries {
buf := make([]byte, 65507) // maximum UDP payload length
n, addr, err := server.getConn().ReadFrom(buf)
if err != nil {
incErrorCountAndSleep(err, &errCount)
// error will be logged when exiting after 3 errors.
continue
}
glog.V(3).Infoln("Incoming message from UDP client @ ", addr)
glog.V(3).Infoln("Number of bytes: ", n)
server.incoming <- NewMessage(ipfixContext, buf, n)
}
glog.Errorln("Listener failed 3 times. Killing it!", err)
exit <- struct{}{}
}
// Parse parses 'msg' and sends JSON representation to 'outgoing' channel.
func (server *Server) Parse(msg *Message) {
// parse, pre-process and generate a JSON representation.
go func() {
server.outgoing <- msg.Parse()
}()
}
// Start launches one or multiple goroutine listeners.
func (server *Server) Start() {
glog.Infof("Will be using %d CPU(s).", globalServerOptions.numCPU)
for cpu := 0; cpu < globalServerOptions.numCPU; cpu++ {
// use closures with goroutines to ensure we have one (1) IPFIX
// session and interpreter instances per goroutine
ipfixContext := initIpfixContext()
go func(cpu int) {
glog.Infof("Starting worker #%d ", cpu)
server.Read(ipfixContext, server.exit)
}(cpu)
}
}
// Export sends JSON processed messages to enabled export destinations
func (server *Server) Export(msg string) {
// syslog export
if len(msg) > 0 && isSyslogExportEnabled() {
go exportSyslog(msg)
}
// other exports can can here
}
// Message holds an IPFIX context, a buffer containing IPFIX binary payload and
// the size of the buffer.
type Message struct {
ipfixContext *IpfixContext
buf []byte
n int
}
// NewMessage returns a *Message
func NewMessage(ipfixContext *IpfixContext, buf []byte, n int) *Message {
message := &Message{
ipfixContext: ipfixContext,
buf: buf,
n: n,
}
return message
}
// Parse returns a message in JSON format
func (message *Message) Parse() string {
return parseIpfix(message.buf, message.n, message.ipfixContext)
}