Newer
Older
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
/*
* Copyright (C) 2022 IBM, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package main
import (
"flag"
"log"
"net"
"github.com/netobserv/netobserv-ebpf-agent/pkg/grpc"
"github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow"
)
var (
port = flag.Int("listen_port", 9999, "TCP port to listen for flows")
)
var protocolByNumber = map[uint32]string{
1: "icmp",
2: "igmp",
6: "tcp",
17: "udp",
58: "ipv6-icmp",
}
func ipIntToNetIP(ipAsInt uint32) net.IP {
var bytes [4]byte
bytes[0] = byte(ipAsInt & 0xFF)
bytes[1] = byte((ipAsInt >> 8) & 0xFF)
bytes[2] = byte((ipAsInt >> 16) & 0xFF)
bytes[3] = byte((ipAsInt >> 24) & 0xFF)
return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
}
// tcpdump flow collector
func main() {
log.SetFlags(0)
flag.Parse()
receivedRecords := make(chan *pbflow.Records, 100)
log.Println("starting flowlogs-dump-collector on port", *port)
go func() {
_, err := grpc.StartCollector(*port, receivedRecords)
if err != nil {
panic(err)
}
}()
for records := range receivedRecords {
for _, record := range records.Entries {
log.Printf("%v %s IP %s:%d > %s:%d: protocol:%s dir:%d bytes:%d packets:%d ends: %v\n",
record.TimeFlowStart.AsTime().Local().Format("15:04:05.000000"),
record.Interface,
ipIntToNetIP(record.Network.GetSrcAddr().GetIpv4()).String(),
record.Transport.SrcPort,
ipIntToNetIP(record.Network.GetDstAddr().GetIpv4()).String(),
record.Transport.DstPort,
protocolByNumber[record.Transport.Protocol],
record.Direction,
record.Bytes,
record.Packets,
record.TimeFlowEnd.AsTime().Local().Format("15:04:05.000000"),
)
}
}
}