-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipfix.go
108 lines (87 loc) · 2.7 KB
/
ipfix.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
package main
import (
"encoding/json"
"strconv"
"github.com/calmh/ipfix"
"github.com/golang/glog"
)
// initialize an IPFIX context and interpreter instances
func initIpfixContext() *IpfixContext {
ipfixSession := ipfix.NewSession()
ipfixInterpreter := ipfix.NewInterpreter(ipfixSession)
initIpfixVendors(ipfixInterpreter)
ipfixContext := IpfixContext{
session: ipfixSession,
interpreter: ipfixInterpreter,
}
return &ipfixContext
}
// extends IPFIX interpreter with vendor fields.
func initIpfixVendors(ipfixInterpreter *ipfix.Interpreter) {
for i := 0; i < len(globalServerOptions.vendors); i++ {
switch globalServerOptions.vendors[i] {
case VendorVmwareNSX:
glog.V(4).Infoln("Include vendor fields",
VendorVmwareNSX)
includeVmwareNsxFields(ipfixInterpreter)
case VendorVmwareVDS:
glog.V(4).Infoln("Include vendor fields",
VendorVmwareVDS)
includeVmwareVDSFields(ipfixInterpreter)
case VendorNokia:
glog.V(4).Infoln("Include vendor fields",
VendorNokia)
includeNokiaFields(ipfixInterpreter)
}
}
}
// golang `map[string]interface{}` to JSON string
func mapToJSON(myMap map[string]interface{}) string {
jsonBytes, _ := json.Marshal(myMap)
return string(jsonBytes[:])
}
func parseIpfixMessage(buf []byte, n int,
ipfixContext *IpfixContext) map[string]interface{} {
msg, err := ipfixContext.session.ParseBuffer(buf[0:n])
if err != nil {
glog.Errorln("Error received:", err)
}
if len(msg.DataRecords) > 0 {
glog.V(4).Infoln("msg.DataRecords: ", msg.DataRecords)
} else {
glog.V(4).Infoln("msg.DateRecords empty. " +
"Waiting for schema?")
}
var fieldList []ipfix.InterpretedField
aliasFieldList := make(map[string]interface{})
for a, rec := range msg.DataRecords {
glog.V(4).Infoln("Rec: ", rec)
glog.V(4).Infoln("a: ", a)
fieldList = ipfixContext.interpreter.InterpretInto(rec,
fieldList[:cap(fieldList)])
for i := 0; i < len(fieldList); i++ {
if fieldList[i].Name != "" {
aliasFieldList[fieldList[i].Name] = fieldList[i].Value
glog.V(3).Infoln("field name=", fieldList[i].Name,
" field value:", fieldList[i].Value)
}
if fieldList[i].Name == Layer2SegmentID {
nsxSegmentID := getNSXSegmentID(fieldList[i].Value.(uint64))
aliasFieldList[NSXSegmentID] = strconv.Itoa(int(nsxSegmentID))
}
}
}
glog.V(3).Infoln("MSG FIELDS MAP:", aliasFieldList)
return aliasFieldList
}
// parse IPFIX messages and returns a JSON string representation
func parseIpfix(buf []byte, n int, ipfixContext *IpfixContext) string {
msgMap := parseIpfixMessage(buf, n, ipfixContext)
var jsonStr string
if len(msgMap) > 0 {
jsonStr = mapToJSON(msgMap)
} else {
glog.V(3).Infoln("Empty message: waiting for schema?")
}
return jsonStr
}