@@ -10,28 +10,43 @@ import (
1010 "time"
1111)
1212
13- type recordMap map [string ]interface {}
13+ type InterpretedRecord struct {
14+ ExportTime uint32 `json:"exportTime"`
15+ TemplateId uint16 `json:"templateId"`
16+ Fields []myInterpretedField `json:"fields"`
17+ }
18+
19+ // Because we want to control JSON serialization
20+ type myInterpretedField struct {
21+ Name string `json:"name"`
22+ EnterpriseId uint32 `json:"enterprise,omitempty"`
23+ FieldId uint16 `json:"field"`
24+ Value interface {} `json:"value,omitempty"`
25+ RawValue []byte `json:"raw,omitempty"`
26+ }
1427
15- func messagesGenerator (s * ipfix.Session ) <- chan []recordMap {
16- c := make (chan []recordMap )
28+ func messagesGenerator (s * ipfix.Session ) <- chan []InterpretedRecord {
29+ c := make (chan []InterpretedRecord )
1730
1831 go func () {
1932 for {
20- sets := make ([]recordMap , 0 , 32 )
2133 msg , err := s .ReadMessage ()
2234 if err != nil {
2335 panic (err )
2436 }
2537
26- for _ , record := range msg .DataRecords {
27- set := make (map [string ]interface {})
28- set ["templateId" ] = record .TemplateId
29- set ["exportTime" ] = msg .Header .ExportTime
30- set ["elements" ] = s .Interpret (& record )
31- sets = append (sets , set )
38+ irecs := make ([]InterpretedRecord , len (msg .DataRecords ))
39+ for i , record := range msg .DataRecords {
40+ ifs := s .Interpret (& record )
41+ mfs := make ([]myInterpretedField , len (ifs ))
42+ for i , iif := range ifs {
43+ mfs [i ] = myInterpretedField {iif .Name , iif .EnterpriseId , iif .FieldId , iif .Value , iif .RawValue }
44+ }
45+ ir := InterpretedRecord {msg .Header .ExportTime , record .TemplateId , mfs }
46+ irecs [i ] = ir
3247 }
3348
34- c <- sets
49+ c <- irecs
3550 }
3651 }()
3752
@@ -74,18 +89,18 @@ func main() {
7489 tick := time .Tick (time .Duration (* statsIntv ) * time .Second )
7590 for {
7691 select {
77- case sets := <- msgs :
92+ case irecs := <- msgs :
7893 if * messageStats {
79- accountMsgStats (sets )
94+ accountMsgStats (irecs )
8095 }
8196
82- for _ , set := range sets {
97+ for _ , rec := range irecs {
8398 if * trafficStats {
84- accountTraffic (set )
99+ accountTraffic (rec )
85100 }
86101
87102 if * output {
88- bs , _ := json .Marshal (set )
103+ bs , _ := json .Marshal (rec )
89104 fmt .Println (string (bs ))
90105 }
91106 }
0 commit comments