Skip to content

Commit 379c6b9

Browse files
committed
Add generic samplers
1 parent f9b6767 commit 379c6b9

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func main() {
6868
fmt.Printf("Record %d has no IPs\n", cnt)
6969
}
7070

71+
// sampling
72+
packetInterval, spaceInterval := record.SamplerInfo(nffile)
73+
fmt.Printf("Record sampler info: packet interval: %d, space interval: %d\n",
74+
packetInterval, spaceInterval)
75+
7176
// print the entire record using %v
7277
fmt.Printf("%v\n", record)
7378

defs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ type EXipReceived struct {
7474
}
7575

7676
type ExporterInfoRecord C.struct_exporter_info_record_s
77+
type SamplerRecord C.struct_sampler_record_s

example/reader/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func main() {
5858
fmt.Printf("Record %d has no IPs\n", cnt)
5959
}
6060

61+
// sampling
62+
packetInterval, spaceInterval := record.SamplerInfo(nffile)
63+
fmt.Printf("Record sampler info: packet interval: %d, space interval: %d\n", packetInterval, spaceInterval)
64+
6165
// print the entire record using %v
6266
fmt.Printf("%v\n", record)
6367

@@ -99,6 +103,10 @@ func main() {
99103
if exporter.IP != nil && id == int(exporter.SysId) { // valid exporter
100104
fmt.Printf(" SysID: %d, ID: %d, IP: %v, version: %d", exporter.SysId, exporter.Id, exporter.IP, exporter.Version)
101105
fmt.Printf(" Sequence failures: %d, packets: %d, flows: %d\n", exporter.SequenceFailures, exporter.Packets, exporter.Flows)
106+
for _, sampler := range exporter.SamplerList {
107+
fmt.Printf(" Sampler: id: %d, algorithm: %d, packet interval: %d, space interval: %d\n",
108+
sampler.Id, sampler.Algorithm, sampler.PacketInterval, sampler.SpaceInterval)
109+
}
102110
}
103111
}
104112
}

exporter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"unsafe"
1616
)
1717

18+
type Sampler struct {
19+
Id int64
20+
Algorithm uint16
21+
PacketInterval uint32
22+
SpaceInterval uint32
23+
}
24+
1825
type Exporter struct {
1926
IP net.IP // IP address
2027
isV4 bool
@@ -25,6 +32,7 @@ type Exporter struct {
2532
Packets uint64 // number of packets sent by this exporter
2633
Flows uint64 // number of flow records sent by this exporter
2734
SequenceFailures uint32 // number of sequence failures
35+
SamplerList []Sampler
2836
}
2937

3038
const MaxExporters = 256
@@ -84,6 +92,21 @@ func (nfFile *NfFile) addExporterStat(record []byte) {
8492
}
8593

8694
func (nfFile *NfFile) addSampler(record []byte) {
95+
samplerInfo := (*SamplerRecord)(unsafe.Pointer(&record[0]))
96+
97+
maxExporterID := len(nfFile.ExporterList)
98+
if samplerInfo.Sysid >= uint16(maxExporterID) || nfFile.ExporterList[samplerInfo.Sysid].IP == nil {
99+
// no valid exporter avilable
100+
fmt.Printf("No valid exporter for new sampler\n")
101+
return
102+
}
103+
104+
nfFile.ExporterList[samplerInfo.Sysid].SamplerList = append(nfFile.ExporterList[samplerInfo.Sysid].SamplerList, Sampler{
105+
Id: samplerInfo.Id,
106+
Algorithm: samplerInfo.Algorithm,
107+
PacketInterval: samplerInfo.PacketInterval,
108+
SpaceInterval: samplerInfo.SpaceInterval,
109+
})
87110

88111
}
89112

exporter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,20 @@ typedef struct exporter_stats_record_s {
7979

8080
} exporter_stats_record_t;
8181

82+
typedef struct sampler_record_s {
83+
// record header
84+
uint16_t type;
85+
uint16_t size;
86+
87+
// sampler data
88+
uint16_t exporter_sysid; // internal reference to exporter
89+
uint16_t algorithm; // #304 sampling algorithm
90+
int64_t id; // #302 id assigned by the exporting device or
91+
#define SAMPLER_OVERWRITE -3
92+
#define SAMPLER_DEFAULT -2
93+
#define SAMPLER_GENERIC -1
94+
uint32_t packetInterval; // #305 packet interval
95+
uint32_t spaceInterval; // #306 packet space
96+
} sampler_record_t;
97+
8298
#endif

nfxV3.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

record.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,28 @@ func (flowRecord *FlowRecordV3) IpReceived() *EXipReceived {
208208
// no IP received
209209
return nil
210210
}
211+
212+
// get sampler info for flow record
213+
func (flowRecord *FlowRecordV3) SamplerInfo(nfFile *NfFile) (packetInterval int, spaceInterval int) {
214+
215+
// default values - no sampling
216+
packetInterval = 1
217+
spaceInterval = 0
218+
219+
maxExporterID := len(nfFile.ExporterList)
220+
exporterID := flowRecord.recordHeader.ExporterID
221+
222+
if exporterID >= uint16(maxExporterID) || nfFile.ExporterList[exporterID].IP == nil {
223+
return
224+
}
225+
226+
exporter := nfFile.ExporterList[exporterID]
227+
228+
// samplers are added in sequence and may overwrite previous samplers
229+
// the last in chain is currently valid
230+
for _, sampler := range exporter.SamplerList {
231+
packetInterval = int(sampler.PacketInterval)
232+
spaceInterval = int(sampler.SpaceInterval)
233+
}
234+
return
235+
}

0 commit comments

Comments
 (0)