-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (92 loc) · 2.2 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
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
)
var (
collectorAddr string = ""
consoleOut bool = false
ioamCount uint64 = 0
overflowCount uint64 = 0
)
func main() {
parseCliOptions()
conn := setupListener()
defer conn.Close()
go writeStats(STATS_FILE)
log.Println("[IOAM Exporter] Started...")
// Message receiving loop
for {
messages, _, err := conn.Receive()
if err != nil {
// Assume that the error is due to a buffer overflow (ENOBUFS)
overflowCount++
}
for _, msg := range messages {
go readMessage(msg)
}
}
}
// Parses the netlink message and extracts IOAMData
func readMessage(msg genetlink.Message) error {
attrs, err := netlink.UnmarshalAttributes(msg.Data)
if err != nil {
log.Printf("failed to parse attributes: %v", err)
return err
}
var nodes []IoamNode
if msg.Header.Command == IOAM6_EVENT_TYPE_TRACE {
nodes, err = extractPtoData(attrs)
if err != nil {
log.Printf("failed to build IOAMdata: %v", err)
return err
}
} else if msg.Header.Command == IOAM6_EVENT_TYPE_DEX {
node, err := extractDexData(attrs)
if err != nil {
log.Printf("failed to build IoamNodeDEX: %d\n", err)
return err
}
nodes = append(nodes, node)
} else {
log.Println(("unexpected generic netlink command"))
return nil
}
if consoleOut {
for _, node := range nodes {
fmt.Printf("%+v\n\n", node)
}
}
if collectorAddr != "" {
msg, err := createIPFIXMessage(nodes)
if err != nil {
log.Printf("could not create ipfix message: %v", err)
return err
}
sendIPFIX(msg)
}
ioamCount++
return nil
}
// Writes the number of received IOAM messages to a file
func writeStats(fileName string) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error opening stats file: %v", err)
}
defer file.Close()
for range ticker.C {
// Update file statistics
file.Seek(0, io.SeekStart)
if _, err := fmt.Fprintf(file, "IOAM messages\t%d\nOverflow errors\t%d\n", ioamCount, overflowCount); err != nil {
log.Fatalf("Error writing to stats file: %v", err)
}
}
}