|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | + |
| 13 | + ipfixCollector "github.com/vmware/go-ipfix/pkg/collector" |
| 14 | + "github.com/vmware/go-ipfix/pkg/entities" |
| 15 | + "github.com/vmware/go-ipfix/pkg/registry" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + hostPortIPv4 = "127.0.0.1:9999" |
| 20 | + hostPortIPv6 = "[::1]:0" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + transportType = flag.String("transport", "tcp", "transport type :tcp/udp") |
| 25 | +) |
| 26 | + |
| 27 | +func printIPFIXMessage(msg *entities.Message) { |
| 28 | + var buf bytes.Buffer |
| 29 | + fmt.Fprint(&buf, "\nIPFIX-HDR:\n") |
| 30 | + fmt.Fprintf(&buf, " version: %v, Message Length: %v\n", msg.GetVersion(), msg.GetMessageLen()) |
| 31 | + fmt.Fprintf(&buf, " Exported Time: %v (%v)\n", msg.GetExportTime(), time.Unix(int64(msg.GetExportTime()), 0)) |
| 32 | + fmt.Fprintf(&buf, " Sequence No.: %v, Observation Domain ID: %v\n", msg.GetSequenceNum(), msg.GetObsDomainID()) |
| 33 | + |
| 34 | + set := msg.GetSet() |
| 35 | + if set.GetSetType() == entities.Template { |
| 36 | + fmt.Fprint(&buf, "TEMPLATE SET:\n") |
| 37 | + for i, record := range set.GetRecords() { |
| 38 | + fmt.Fprintf(&buf, " TEMPLATE RECORD-%d:\n", i) |
| 39 | + for _, ie := range record.GetOrderedElementList() { |
| 40 | + elem := ie.GetInfoElement() |
| 41 | + fmt.Fprintf(&buf, " %s: len=%d (enterprise ID = %d) \n", elem.Name, elem.Len, elem.EnterpriseId) |
| 42 | + } |
| 43 | + } |
| 44 | + } else { |
| 45 | + fmt.Fprint(&buf, "DATA SET:\n") |
| 46 | + for i, record := range set.GetRecords() { |
| 47 | + fmt.Fprintf(&buf, " DATA RECORD-%d:\n", i) |
| 48 | + for _, ie := range record.GetOrderedElementList() { |
| 49 | + elem := ie.GetInfoElement() |
| 50 | + switch elem.DataType { |
| 51 | + case entities.Unsigned8: |
| 52 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned8Value()) |
| 53 | + case entities.Unsigned16: |
| 54 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned16Value()) |
| 55 | + case entities.Unsigned32: |
| 56 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned32Value()) |
| 57 | + case entities.Unsigned64: |
| 58 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned64Value()) |
| 59 | + case entities.Signed8: |
| 60 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetSigned8Value()) |
| 61 | + case entities.Signed16: |
| 62 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetSigned16Value()) |
| 63 | + case entities.Signed32: |
| 64 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetSigned32Value()) |
| 65 | + case entities.Signed64: |
| 66 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetSigned64Value()) |
| 67 | + case entities.Float32: |
| 68 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetFloat32Value()) |
| 69 | + case entities.Float64: |
| 70 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetFloat64Value()) |
| 71 | + case entities.Boolean: |
| 72 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetBooleanValue()) |
| 73 | + case entities.DateTimeSeconds: |
| 74 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned32Value()) |
| 75 | + case entities.DateTimeMilliseconds: |
| 76 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned64Value()) |
| 77 | + case entities.DateTimeMicroseconds, entities.DateTimeNanoseconds: |
| 78 | + err := fmt.Errorf("API does not support micro and nano seconds types yet") |
| 79 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, err) |
| 80 | + case entities.MacAddress: |
| 81 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetMacAddressValue()) |
| 82 | + case entities.Ipv4Address, entities.Ipv6Address: |
| 83 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetIPAddressValue()) |
| 84 | + case entities.String: |
| 85 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetStringValue()) |
| 86 | + default: |
| 87 | + err := fmt.Errorf("API supports only valid information elements with datatypes given in RFC7011") |
| 88 | + fmt.Fprintf(&buf, " %s: %v \n", elem.Name, err) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + log.Printf(buf.String()) |
| 94 | +} |
| 95 | + |
| 96 | +func signalHandler(stopCh chan struct{}, messageReceived chan *entities.Message) { |
| 97 | + signalCh := make(chan os.Signal, 1) |
| 98 | + signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) |
| 99 | + |
| 100 | + for { |
| 101 | + select { |
| 102 | + case msg := <-messageReceived: |
| 103 | + printIPFIXMessage(msg) |
| 104 | + case <-signalCh: |
| 105 | + close(stopCh) |
| 106 | + return |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func main() { |
| 112 | + log.SetFlags(0) |
| 113 | + flag.Parse() |
| 114 | + |
| 115 | + // Create exporter using local server info |
| 116 | + var input = ipfixCollector.CollectorInput{ |
| 117 | + Address: hostPortIPv4, |
| 118 | + Protocol: *transportType, |
| 119 | + MaxBufferSize: 1024, |
| 120 | + } |
| 121 | + registry.LoadRegistry() |
| 122 | + cp, err := ipfixCollector.InitCollectingProcess(input) |
| 123 | + if err != nil { |
| 124 | + log.Fatalf("UDP Collecting Process does not start correctly: %v", err) |
| 125 | + } |
| 126 | + // Start listening to connections and receiving messages. |
| 127 | + messageReceived := make(chan *entities.Message) |
| 128 | + go func() { |
| 129 | + go cp.Start() |
| 130 | + msgChan := cp.GetMsgChan() |
| 131 | + for message := range msgChan { |
| 132 | + messageReceived <- message |
| 133 | + } |
| 134 | + }() |
| 135 | + |
| 136 | + stopCh := make(chan struct{}) |
| 137 | + go signalHandler(stopCh, messageReceived) |
| 138 | + |
| 139 | + <-stopCh |
| 140 | + // Stop the collector process |
| 141 | + cp.Stop() |
| 142 | + log.Printf("Stopping IPFIX collector") |
| 143 | +} |
0 commit comments