-
Notifications
You must be signed in to change notification settings - Fork 8
/
forcegraph.go
113 lines (93 loc) · 2.77 KB
/
forcegraph.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
// Copyright 2017-20 Daniel Swarbrick. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
// Package forecegraph implements the ForceGraphWriter, which writes the fabric topology to a JSON
// file suitable for use by the d3.js force graph functions.
package forcegraph
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/dswarbrick/fabricmon/infiniband"
)
type d3Node struct {
ID string `json:"id"`
Desc string `json:"desc"`
NodeType int `json:"nodetype"`
VendorID uint `json:"vendor_id"`
DeviceID uint `json:"device_id"`
}
type d3Link struct {
Source string `json:"source"`
Target string `json:"target"`
Width string `json:"link_width"`
Speed string `json:"link_speed"`
}
type d3Topology struct {
Nodes []d3Node `json:"nodes"`
Links []d3Link `json:"links"`
}
type ForceGraphWriter struct {
OutputDir string
}
// TODO: Rename this to something more descriptive (and which is not so easily confused with method
// receivers).
func (fg *ForceGraphWriter) Receiver(input chan infiniband.Fabric) {
for fabric := range input {
if fg.OutputDir != "" {
if err := writeTopology(fg.OutputDir, fabric); err != nil {
slog.Error("cannot marshal fabric to force graph topology", "err", err)
}
}
}
}
// buildTopology transforms the internal representation of InfiniBand nodes into d3.js nodes and
// links.
func buildTopology(nodes []infiniband.Node) d3Topology {
topo := d3Topology{}
for _, node := range nodes {
d3n := d3Node{
ID: fmt.Sprintf("%016x", node.GUID),
NodeType: node.NodeType,
Desc: node.NodeDesc,
VendorID: node.VendorID,
DeviceID: node.DeviceID,
}
topo.Nodes = append(topo.Nodes, d3n)
for _, port := range node.Ports {
if port.RemoteGUID != 0 {
topo.Links = append(topo.Links, d3Link{
Source: fmt.Sprintf("%016x", node.GUID),
Target: fmt.Sprintf("%016x", port.RemoteGUID),
Width: port.LinkWidth,
Speed: port.LinkSpeed,
})
}
}
}
return topo
}
// writeTopology writes a d3.js force graph JSON object file.
func writeTopology(outputDir string, fabric infiniband.Fabric) error {
// Write d3.js topology to a temporary file, then rename it to target file, to ensure atomic
// updates and avoid partial reads by clients.
tempFile, err := os.CreateTemp(outputDir, ".fabricmon")
if err != nil {
return err
}
enc := json.NewEncoder(tempFile)
if err := enc.Encode(buildTopology(fabric.Nodes)); err != nil {
tempFile.Close()
os.Remove(tempFile.Name())
return err
}
tempFile.Chmod(0644)
tempFile.Close()
destFile := fmt.Sprintf("%s-%s-p%d.json", fabric.Hostname, fabric.CAName, fabric.SourcePort)
if err := os.Rename(tempFile.Name(), filepath.Join(outputDir, destFile)); err != nil {
os.Remove(tempFile.Name())
return err
}
return nil
}