Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit ac97aba

Browse files
committed
Fields as array
1 parent 4fb6a8a commit ac97aba

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

main.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

msgstats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ var (
1111
msgs int
1212
)
1313

14-
func accountMsgStats(sets []recordMap) {
14+
func accountMsgStats(recs []InterpretedRecord) {
1515
msgs++
16-
records += len(sets)
16+
records += len(recs)
1717
}
1818

1919
type MsgStats struct {

trafficstats.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ type TrafStats struct {
1616
OutKbps int
1717
}
1818

19-
func accountTraffic(set recordMap) {
20-
if elems, ok := set["elements"]; ok {
21-
// Account the in/out traffic rate if we have Procera Networks vendor fields available.
22-
elems := elems.(map[string]interface{})
23-
if in, ok := elems["proceraIncomingOctets"]; ok {
24-
inOctets += in.(uint64)
25-
}
26-
if out, ok := elems["proceraOutgoingOctets"]; ok {
27-
outOctets += out.(uint64)
19+
func accountTraffic(rec InterpretedRecord) {
20+
for _, f := range rec.Fields {
21+
if f.Name == "proceraIncomingOctets" {
22+
inOctets += f.Value.(uint64)
23+
} else if f.Name == "proceraOutgoingOctets" {
24+
outOctets += f.Value.(uint64)
2825
}
2926
}
3027
}

0 commit comments

Comments
 (0)