-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
120 lines (104 loc) · 4.19 KB
/
exporter.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
// Copyright © 2023 Peter Haag [email protected]
// All rights reserved.
//
// Use of this source code is governed by the license that can be
// found in the LICENSE file.
// Package nfdump provides an API for nfdump files
package nfdump
import (
"encoding/binary"
"fmt"
"net"
"syscall"
"unsafe"
)
type Sampler struct {
Id int64
Algorithm uint16
PacketInterval uint32
SpaceInterval uint32
}
type Exporter struct {
IP net.IP // IP address
isV4 bool
isV6 bool
SysId uint16 // internal assigned ID
Version uint16 // netflow version
Id uint32 // exporter ID/Domain ID/Observation Domain ID assigned by the device
Packets uint64 // number of packets sent by this exporter
Flows uint64 // number of flow records sent by this exporter
SequenceFailures uint32 // number of sequence failures
SamplerList []Sampler
}
const MaxExporters = 256
// Extract next flow record from []byte stream
func (nfFile *NfFile) addExporterInfo(record []byte) {
exporterInfo := (*ExporterInfoRecord)(unsafe.Pointer(&record[0]))
var exporter Exporter
exporter.Id = exporterInfo.Id
exporter.SysId = exporterInfo.Sysid
exporter.Version = uint16(exporterInfo.Version)
if int(exporter.SysId) >= MaxExporters {
fmt.Printf("Exporter SysID: %d out of range\n", exporter.SysId)
}
for int(exporter.SysId) >= len(nfFile.ExporterList) {
newSlice := make([]Exporter, 8)
nfFile.ExporterList = append(nfFile.ExporterList, newSlice...)
}
if exporterInfo.Family == syscall.AF_INET {
exporter.isV4 = true
exOffset := 16
exporter.IP = net.IPv4(record[exOffset+3], record[exOffset+2], record[exOffset+1], record[exOffset])
}
if exporterInfo.Family == syscall.AF_INET6 {
exporter.isV6 = true
exOffset := 8
exporter.IP = net.IP{record[exOffset+7], record[exOffset+6], record[exOffset+5], record[exOffset+4], record[exOffset+3], record[exOffset+2], record[exOffset+1], record[exOffset+0], record[exOffset+15], record[exOffset+14], record[exOffset+13], record[exOffset+12], record[exOffset+11], record[exOffset+10], record[exOffset+9], record[exOffset+8]}
}
nfFile.ExporterList[exporter.SysId] = exporter
}
func (nfFile *NfFile) addExporterStat(record []byte) {
offset := 0
// recordType := binary.LittleEndian.Uint16(record[offset : offset+2])
// recordSize := binary.LittleEndian.Uint16(record[offset+2 : offset+4])
numStat := binary.LittleEndian.Uint16(record[offset+4 : offset+8])
// fmt.Printf("ExporterStat: type: %d, size: %d, numStats: %d\n", recordType, recordSize, numStat)
offset = 8
for i := 0; i < int(numStat); i++ {
sysId := binary.LittleEndian.Uint32(record[offset : offset+4]) // identifies the exporter
sequenceFailures := binary.LittleEndian.Uint32(record[offset+4 : offset+8]) // number of sequence failures
packets := binary.LittleEndian.Uint64(record[offset+8 : offset+16]) // number of packets sent by this exporter
flows := binary.LittleEndian.Uint64(record[offset+16 : offset+24]) // number of flows sent by this exporter
offset += 24
if int(sysId) >= len(nfFile.ExporterList) {
fmt.Printf("Invalid Exporter id: %d\n", sysId)
return
}
if nfFile.ExporterList[sysId].SysId == uint16(sysId) {
nfFile.ExporterList[sysId].SequenceFailures += sequenceFailures
nfFile.ExporterList[sysId].Packets += packets
nfFile.ExporterList[sysId].Flows += flows
} else {
fmt.Printf("Unknown Exporter id: %d\n", sysId)
}
}
}
func (nfFile *NfFile) addSampler(record []byte) {
samplerInfo := (*SamplerRecord)(unsafe.Pointer(&record[0]))
maxExporterID := len(nfFile.ExporterList)
if samplerInfo.Sysid >= uint16(maxExporterID) || nfFile.ExporterList[samplerInfo.Sysid].IP == nil {
// no valid exporter avilable
fmt.Printf("No valid exporter for new sampler\n")
return
}
nfFile.ExporterList[samplerInfo.Sysid].SamplerList = append(nfFile.ExporterList[samplerInfo.Sysid].SamplerList, Sampler{
Id: samplerInfo.Id,
Algorithm: samplerInfo.Algorithm,
PacketInterval: samplerInfo.PacketInterval,
SpaceInterval: samplerInfo.SpaceInterval,
})
}
// Get exporter list
func (nfFile *NfFile) GetExporterList() []Exporter {
return nfFile.ExporterList
}