This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
154 lines (132 loc) · 3.21 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/json"
"flag"
"io"
"log"
"os"
"time"
"github.com/calmh/ipfix"
)
var ipfixcatVersion string
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 []int `json:"raw,omitempty"`
}
func messagesGenerator(r io.Reader, s *ipfix.Session, i *ipfix.Interpreter) <-chan []InterpretedRecord {
c := make(chan []InterpretedRecord)
errors := 0
go func() {
for {
msg, err := s.ParseReader(r)
if err == io.EOF {
close(c)
return
}
if err != nil {
errors++
if errors > 3 {
panic(err)
} else {
log.Println(err)
}
continue
} else {
errors = 0
}
irecs := make([]InterpretedRecord, len(msg.DataRecords))
for j, record := range msg.DataRecords {
ifs := i.Interpret(record)
mfs := make([]myInterpretedField, len(ifs))
for k, iif := range ifs {
mfs[k] = myInterpretedField{iif.Name, iif.EnterpriseID, iif.FieldID, iif.Value, integers(iif.RawValue)}
}
ir := InterpretedRecord{msg.Header.ExportTime, record.TemplateID, mfs}
irecs[j] = ir
}
c <- irecs
}
}()
return c
}
func main() {
log.Println("ipfixcat", ipfixcatVersion)
dictFile := flag.String("dict", "", "User dictionary file")
messageStats := flag.Bool("mstats", false, "Log IPFIX message statistics")
trafficStats := flag.Bool("acc", false, "Log traffic rates (Procera)")
output := flag.Bool("output", true, "Display received flow records in JSON format")
statsIntv := flag.Int("statsintv", 60, "Statistics log interval (s)")
flag.Parse()
if *messageStats {
log.Printf("Logging message statistics every %d seconds", *statsIntv)
}
if *trafficStats {
log.Printf("Logging traffic rates every %d seconds", *statsIntv)
}
if !*messageStats && !*trafficStats && !*output {
log.Fatal("If you don't want me to do anything, don't run me at all.")
}
s := ipfix.NewSession()
i := ipfix.NewInterpreter(s)
if *dictFile != "" {
if err := loadUserDictionary(*dictFile, i); err != nil {
log.Fatal(err)
}
}
msgs := messagesGenerator(os.Stdin, s, i)
tick := time.Tick(time.Duration(*statsIntv) * time.Second)
enc := json.NewEncoder(os.Stdout)
for {
select {
case irecs, ok := <-msgs:
if !ok {
return
}
if *messageStats {
accountMsgStats(irecs)
}
for _, rec := range irecs {
if *trafficStats {
accountTraffic(rec)
}
if *output {
for i := range rec.Fields {
f := &rec.Fields[i]
switch v := f.Value.(type) {
case []byte:
f.RawValue = integers(v)
f.Value = nil
}
}
enc.Encode(rec)
}
}
case <-tick:
if *messageStats {
logMsgStats()
}
if *trafficStats {
logAccountedTraffic()
}
}
}
}
func integers(s []byte) []int {
if s == nil {
return nil
}
r := make([]int, len(s))
for i := range s {
r[i] = int(s[i])
}
return r
}