-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataSocket.go
217 lines (183 loc) · 5.89 KB
/
dataSocket.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2021, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* dataSocket implements a UNIX socket server to receive data from nfcapd
* Up to now the exporter implements flows/packets/bytes counters per
* protocol(tcp/udp/icmp/other and the source identifier from the collector
*
*/
package main
/*
#include <stdint.h>
typedef struct metric_record_s {
// Ident
uint64_t exporterID; // 32bit: exporter_id:16 engineType:8 engineID:*
// flow stat
uint64_t numflows_tcp;
uint64_t numflows_udp;
uint64_t numflows_icmp;
uint64_t numflows_other;
// bytes stat
uint64_t numbytes_tcp;
uint64_t numbytes_udp;
uint64_t numbytes_icmp;
uint64_t numbytes_other;
// packet stat
uint64_t numpackets_tcp;
uint64_t numpackets_udp;
uint64_t numpackets_icmp;
uint64_t numpackets_other;
} metric_record_t;
const int record_size = sizeof(metric_record_t);
*/
import "C"
import (
"encoding/binary"
"fmt"
"os"
"log"
"net"
"unsafe"
)
const packetPrefix byte = '@'
var metricSize int = int(C.record_size)
type nfsenMetric struct {
// exporter ID
exporterID uint64
// flow stat
numFlows_tcp uint64
numFlows_udp uint64
numFlows_icmp uint64
numFlows_other uint64
// bytes stat
numBytes_tcp uint64
numBytes_udp uint64
numBytes_icmp uint64
numBytes_other uint64
// packet stat
numPackets_tcp uint64
numPackets_udp uint64
numPackets_icmp uint64
numPackets_other uint64
}
var metricList map[string]map[uint64]nfsenMetric
type socketConf struct {
socketPath string
listener net.Listener
}
func New(socketPath string) *socketConf {
conf := new(socketConf)
conf.socketPath = socketPath
metricList = make(map[string]map[uint64]nfsenMetric)
return conf
}
func (socket *socketConf) Open() error {
if err := os.RemoveAll(socket.socketPath); err != nil {
return err
}
listener, err := net.Listen("unix", socket.socketPath)
if err != nil {
return err
}
socket.listener = listener
return nil
} // End of Open
func (socket *socketConf) Close() error {
return socket.listener.Close()
} // End of Close
func processStat(conn net.Conn) {
defer conn.Close()
// storage for reading from socket.
readBuf := make([]byte, 65536)
dataLen, err := conn.Read(readBuf)
if err != nil || dataLen == 0{
fmt.Printf("Socket read error: %v\n", err)
return
}
if readBuf[0] != packetPrefix {
fmt.Printf("Message prefix error - got %u\n", readBuf[0])
return
}
// version := readBuf[1]
// payloadSize := int(binary.LittleEndian.Uint16(readBuf[2:4]))
numMetrics := int(binary.LittleEndian.Uint16(readBuf[4:6]))
// collectorID := int(binary.LittleEndian.Uint64(readBuf[8:16]))
// uptime := int(binary.LittleEndian.Uint64(readBuf[16:24]))
ilen := 0
for i := 0; readBuf[24+i] != 0; i++ {
ilen++
}
ident := string(readBuf[24:24+ilen])
if _, ok := metricList[ident]; ok == false {
metricList[ident] = make(map[uint64]nfsenMetric)
}
/*
fmt.Printf("Message size: %d, payload size: %d version: %d, numMetrics: %d\n",
dataLen, payloadSize, version, numMetrics);
fmt.Printf("Collector: %d, uptime: %d, ident: %s\n",
collectorID, uptime, ident)
*/
var metric nfsenMetric
offset := 152
for num := 0; num < numMetrics; num++ {
var s *C.metric_record_t = (*C.metric_record_t)(unsafe.Pointer(&readBuf[offset]))
metric.exporterID = uint64(s.exporterID)
metric.numFlows_tcp = uint64(s.numflows_tcp)
metric.numFlows_udp = uint64(s.numflows_udp)
metric.numFlows_icmp = uint64(s.numflows_icmp)
metric.numFlows_other = uint64(s.numflows_other)
metric.numBytes_tcp = uint64(s.numbytes_tcp)
metric.numBytes_udp = uint64(s.numbytes_udp)
metric.numBytes_icmp = uint64(s.numbytes_icmp)
metric.numBytes_other = uint64(s.numbytes_other)
metric.numPackets_tcp = uint64(s.numpackets_tcp)
metric.numPackets_udp = uint64(s.numpackets_udp)
metric.numPackets_icmp = uint64(s.numpackets_icmp)
metric.numPackets_other = uint64(s.numpackets_other)
mutex.Lock()
metricList[ident][metric.exporterID] = metric
mutex.Unlock()
offset += metricSize
}
} // end of processStat
func (socket *socketConf) Run() {
go func() {
for {
// Accept new connections from nfcapd collectors and
// dispatching them to goroutine processStat
conn, err := socket.listener.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
// fmt.Printf("New connection\n")
go processStat(conn)
}
}()
} // End of Run