-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
147 lines (117 loc) · 3.61 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
// Copyright 2017-20 Daniel Swarbrick. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
// cgo wrapper around libibumad / libibnetdiscover.
// Note: Due to the usual permissions on /dev/infiniband/umad*, this will probably need to be
// executed as root.
// Package fabricmon is an InfiniBand fabric monitor daemon.
package main
import (
"fmt"
"log/slog"
"os"
"os/signal"
"time"
"golang.org/x/sys/unix"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/dswarbrick/fabricmon/config"
"github.com/dswarbrick/fabricmon/infiniband"
"github.com/dswarbrick/fabricmon/version"
"github.com/dswarbrick/fabricmon/writer"
"github.com/dswarbrick/fabricmon/writer/forcegraph"
"github.com/dswarbrick/fabricmon/writer/influxdb"
)
// router duplicates a Fabric struct received via channel and outputs it to multiple receiver
// channels.
func router(input chan infiniband.Fabric, writers []writer.FabricWriter) {
outputs := make([]chan infiniband.Fabric, len(writers))
// Create output channels for workers, and start worker goroutine
for i, w := range writers {
outputs[i] = make(chan infiniband.Fabric)
go w.Receiver(outputs[i])
}
for fabric := range input {
for _, c := range outputs {
c <- fabric
}
}
// Close output channels
for _, c := range outputs {
close(c)
}
slog.Debug("Router input channel closed. Exiting function.")
}
func main() {
var (
configFile = kingpin.Flag("config", "Path to config file.").Default("fabricmon.yml").File()
daemonize = kingpin.Flag("daemonize", "Run forever, fetching counters periodically.").Default("true").Bool()
)
kingpin.Parse()
conf, err := config.ReadConfig(*configFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
(*configFile).Close()
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: conf.Logging.LogLevel})))
// Initialise umad library (also required in order to run under ibsim).
if infiniband.UmadInit() < 0 {
slog.Error("Error initialising umad library. Exiting.")
os.Exit(1)
}
slog.Info("FabricMon " + version.Info())
hcas := infiniband.GetCAs()
if len(hcas) == 0 {
slog.Error("No HCAs found in system. Exiting.")
os.Exit(1)
}
// Channel to signal goroutines that we are shutting down.
shutdownChan := make(chan bool)
// Setup signal handler to catch SIGINT, SIGTERM.
sigChan := make(chan os.Signal)
signal.Notify(sigChan, unix.SIGINT, unix.SIGTERM)
go func() {
s := <-sigChan
slog.Debug("shutting down due to signal", "signal", s)
close(shutdownChan)
}()
// Initialize empty slice to hold writers
writers := make([]writer.FabricWriter, 0)
if conf.Topology.Enabled {
writers = append(writers, &forcegraph.ForceGraphWriter{OutputDir: conf.Topology.OutputDir})
}
// First sweep.
for _, hca := range hcas {
hca.NetDiscover(nil, conf.Mkey, conf.ResetThreshold)
}
if *daemonize {
for _, c := range conf.InfluxDB {
w := influxdb.NewInfluxDBWriter(c)
writers = append(writers, w)
}
// FIXME: Move this outside of daemonize if-block
splitter := make(chan infiniband.Fabric)
go router(splitter, writers)
ticker := time.NewTicker(time.Duration(conf.PollInterval))
defer ticker.Stop()
Loop:
// Loop indefinitely, scanning fabrics every tick.
for {
select {
case <-ticker.C:
for _, hca := range hcas {
hca.NetDiscover(splitter, conf.Mkey, conf.ResetThreshold)
}
case <-shutdownChan:
slog.Debug("shutdown received in polling loop")
break Loop
}
}
close(splitter)
}
slog.Debug("cleaning up")
// Free associated memory from pointers in umad_ca_t.ports
for _, hca := range hcas {
hca.Release()
}
infiniband.UmadDone()
}