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

Commit

Permalink
Fields as array
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Jul 25, 2013
1 parent 4fb6a8a commit ac97aba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
47 changes: 31 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ import (
"time"
)

type recordMap map[string]interface{}
type InterpretedRecord struct {
ExportTime uint32 `json:"exportTime"`
TemplateId uint16 `json:"templateId"`
Fields []myInterpretedField `json:"fields"`
}

// Because we want to control JSON serialization
type myInterpretedField struct {
Name string `json:"name"`
EnterpriseId uint32 `json:"enterprise,omitempty"`
FieldId uint16 `json:"field"`
Value interface{} `json:"value,omitempty"`
RawValue []byte `json:"raw,omitempty"`
}

func messagesGenerator(s *ipfix.Session) <-chan []recordMap {
c := make(chan []recordMap)
func messagesGenerator(s *ipfix.Session) <-chan []InterpretedRecord {
c := make(chan []InterpretedRecord)

go func() {
for {
sets := make([]recordMap, 0, 32)
msg, err := s.ReadMessage()
if err != nil {
panic(err)
}

for _, record := range msg.DataRecords {
set := make(map[string]interface{})
set["templateId"] = record.TemplateId
set["exportTime"] = msg.Header.ExportTime
set["elements"] = s.Interpret(&record)
sets = append(sets, set)
irecs := make([]InterpretedRecord, len(msg.DataRecords))
for i, record := range msg.DataRecords {
ifs := s.Interpret(&record)
mfs := make([]myInterpretedField, len(ifs))
for i, iif := range ifs {
mfs[i] = myInterpretedField{iif.Name, iif.EnterpriseId, iif.FieldId, iif.Value, iif.RawValue}
}
ir := InterpretedRecord{msg.Header.ExportTime, record.TemplateId, mfs}
irecs[i] = ir
}

c <- sets
c <- irecs
}
}()

Expand Down Expand Up @@ -74,18 +89,18 @@ func main() {
tick := time.Tick(time.Duration(*statsIntv) * time.Second)
for {
select {
case sets := <-msgs:
case irecs := <-msgs:
if *messageStats {
accountMsgStats(sets)
accountMsgStats(irecs)
}

for _, set := range sets {
for _, rec := range irecs {
if *trafficStats {
accountTraffic(set)
accountTraffic(rec)
}

if *output {
bs, _ := json.Marshal(set)
bs, _ := json.Marshal(rec)
fmt.Println(string(bs))
}
}
Expand Down
4 changes: 2 additions & 2 deletions msgstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var (
msgs int
)

func accountMsgStats(sets []recordMap) {
func accountMsgStats(recs []InterpretedRecord) {
msgs++
records += len(sets)
records += len(recs)
}

type MsgStats struct {
Expand Down
15 changes: 6 additions & 9 deletions trafficstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ type TrafStats struct {
OutKbps int
}

func accountTraffic(set recordMap) {
if elems, ok := set["elements"]; ok {
// Account the in/out traffic rate if we have Procera Networks vendor fields available.
elems := elems.(map[string]interface{})
if in, ok := elems["proceraIncomingOctets"]; ok {
inOctets += in.(uint64)
}
if out, ok := elems["proceraOutgoingOctets"]; ok {
outOctets += out.(uint64)
func accountTraffic(rec InterpretedRecord) {
for _, f := range rec.Fields {
if f.Name == "proceraIncomingOctets" {
inOctets += f.Value.(uint64)
} else if f.Name == "proceraOutgoingOctets" {
outOctets += f.Value.(uint64)
}
}
}
Expand Down

0 comments on commit ac97aba

Please sign in to comment.