Cabling: BMC ports to the out-of-band switches, NetBox cables, cables by hand
The host's LLDP never sees the BMC's own port. A port's cable now comes from LLDP first (what is really there), then from a cable recorded in NetBox on the machine's device (read with the layout, traced through patch panels to the far interface via connected_endpoints), then from one entered by hand (POST/DELETE /api/v1/cables, the pencil in the cabling list). A NetBox interface marked management-only or named like a BMC (iDRAC, iLO, XCC, IMM, IPMI, BMC, OOB, mgmt) is the machine's "bmc" port, so BMCs get their cable to the out-of-band switch. When NetBox records another switch port than LLDP sees, the port says what NetBox says. A switch whose NetBox role names out-of-band or management, or that only BMCs are cabled to, is marked OOB. In 3D the BMC cable leaves the BMC port on the back (thinner); the side panel lists every machine's ports with their source, the BMC always, and switches carry an OOB tag. Estimated switch places now go around the units placed switches take. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2624b2fc2
commit
a613014073
12 files changed
+385
-38
No files matched your search
@@ -14,6 +14,9 @@ publish a tag that has no section here.
|
||||
- Cabling from LLDP: `GET /api/v1/cabling` (each machine's ports and the switch port they lead to),
|
||||
switches placed from NetBox or by hand (`POST`/`DELETE /api/v1/switches`), cables drawn in the 3D
|
||||
room from port to switch port, coloured per switch, and listed per rack in the side panel.
|
||||
Where LLDP sees nothing, cables recorded in NetBox (through patch panels) or entered by hand
|
||||
(`POST`/`DELETE /api/v1/cables`); the BMC's port to the out-of-band switch that way, OOB
|
||||
switches marked; NetBox cables that disagree with LLDP are shown.
|
||||
- NetBox's web UI through Rollcall at its `BASE_PATH` (admins only), and links to NetBox from the
|
||||
web UI (sidebar, sites, rooms, racks, switches, machine devices); `ipam.ui_url` for direct links.
|
||||
- Sites page: sites on an OpenStreetMap map (Leaflet shipped in the binary), a site's rooms, and a
|
||||
|
||||
@@ -909,6 +909,18 @@ needs `view` on `dcim.device`) or set by hand (`POST /api/v1/switches
|
||||
and the cables; a switch with no place is drawn where most of its cables are,
|
||||
marked as estimated.
|
||||
|
||||
Where LLDP sees nothing, a port's cable comes from NetBox: the cables recorded
|
||||
on Rollcall's devices (through patch panels, to the far interface), read with
|
||||
the layout; the NetBox user needs `view` on `dcim.interface` and `dcim.cable`.
|
||||
Then from one entered by hand (`POST /api/v1/cables
|
||||
{"machine","port","switch","switch_port"}`, the ✎ in the cabling list).
|
||||
The BMC's own port is never seen by the host's LLDP: its cable to the
|
||||
out-of-band switch is NetBox's (an interface marked management-only or named
|
||||
iDRAC, iLO, XCC, IMM, IPMI, BMC, OOB or mgmt) or entered by hand with port
|
||||
`bmc`. When NetBox records another switch port than LLDP sees, the port shows
|
||||
what NetBox says. A switch whose NetBox role names out-of-band or management,
|
||||
or that only BMCs are cabled to, is marked OOB.
|
||||
|
||||
### Infoblox
|
||||
|
||||
The same contract over Infoblox WAPI (v2.12 by default; give a full
|
||||
|
||||
+259
-24
@@ -1,17 +1,27 @@
|
||||
package main
|
||||
|
||||
// Cabling: what each machine's ports are plugged into, from the LLDP its
|
||||
// commissioning saw (lldpcli's neighbours per interface), and where the
|
||||
// switches at the other end stand. A switch's place comes from NetBox (the
|
||||
// device's site, location, rack and position, read with the layout) or is
|
||||
// set here; a switch with neither is drawn where most of its machines are.
|
||||
// Cabling: what each machine's ports are plugged into, and where the
|
||||
// switches at the other end stand. A port's switch port comes from, first,
|
||||
// the LLDP its commissioning saw (lldpcli's neighbours per interface): what
|
||||
// is really there; then a cable recorded in NetBox on the machine's device
|
||||
// (dcim interfaces with link peers); then one entered here. The BMC's port
|
||||
// is not seen by the host's LLDP: its cable (to the out-of-band switch) is
|
||||
// NetBox's or entered here. When NetBox records another switch port than
|
||||
// LLDP sees, the port says so (netbox_says).
|
||||
//
|
||||
// A switch's place comes from NetBox (the device's site, location, rack and
|
||||
// position, read with the layout) or is set here; a switch with neither is
|
||||
// drawn where most of its machines are. A switch whose NetBox role names
|
||||
// out-of-band or management, or that only BMCs are cabled to, is "oob".
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -26,6 +36,18 @@ type Port struct {
|
||||
SwitchPort string `json:"switch_port,omitempty"`
|
||||
PortDescr string `json:"port_descr,omitempty"`
|
||||
VLAN string `json:"vlan,omitempty"`
|
||||
Kind string `json:"kind,omitempty"` // bmc for the BMC's own port
|
||||
Source string `json:"source,omitempty"` // lldp | netbox | local
|
||||
NetBoxSays string `json:"netbox_says,omitempty"` // NetBox's switch port when LLDP sees another
|
||||
}
|
||||
|
||||
// Cable is one recorded port-to-switch-port connection (NetBox's or entered here).
|
||||
type Cable struct {
|
||||
Machine string `json:"machine"`
|
||||
Port string `json:"port"` // an interface name, or "bmc"
|
||||
Switch string `json:"switch"`
|
||||
SwitchPort string `json:"switch_port"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
type MachinePorts struct {
|
||||
@@ -43,6 +65,7 @@ type Switch struct {
|
||||
Unit int `json:"unit,omitempty"`
|
||||
Height int `json:"height,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Role string `json:"role,omitempty"` // oob for an out-of-band (BMC) switch
|
||||
Source string `json:"source,omitempty"` // netbox | local | "" (not placed)
|
||||
Links int `json:"links"`
|
||||
}
|
||||
@@ -121,7 +144,7 @@ func portsOf(m *Machine) ([]Port, map[string]lldpNeighbour) {
|
||||
p.Card, _, _ = strings.Cut(bus, ".")
|
||||
}
|
||||
if l, ok := nb[p.Name]; ok {
|
||||
p.Switch, p.SwitchPort, p.PortDescr, p.VLAN = l.Switch, l.Port, l.PortDescr, l.VLAN
|
||||
p.Switch, p.SwitchPort, p.PortDescr, p.VLAN, p.Source = l.Switch, l.Port, l.PortDescr, l.VLAN, "lldp"
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
@@ -132,7 +155,7 @@ func portsOf(m *Machine) ([]Port, map[string]lldpNeighbour) {
|
||||
// ---------------------------------------------------------------- store
|
||||
|
||||
func (s *Store) Switches(ctx context.Context) ([]Switch, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT name, coalesce(building,''), coalesce(room,''), coalesce(rack,''), coalesce(unit,0), coalesce(height,1), coalesce(model,''), source FROM switches ORDER BY name`)
|
||||
rows, err := s.pool.Query(ctx, `SELECT name, coalesce(building,''), coalesce(room,''), coalesce(rack,''), coalesce(unit,0), coalesce(height,1), coalesce(model,''), coalesce(role,''), source FROM switches ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -140,7 +163,7 @@ func (s *Store) Switches(ctx context.Context) ([]Switch, error) {
|
||||
out := []Switch{}
|
||||
for rows.Next() {
|
||||
var x Switch
|
||||
if err := rows.Scan(&x.Name, &x.Building, &x.Room, &x.Rack, &x.Unit, &x.Height, &x.Model, &x.Source); err != nil {
|
||||
if err := rows.Scan(&x.Name, &x.Building, &x.Room, &x.Rack, &x.Unit, &x.Height, &x.Model, &x.Role, &x.Source); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, x)
|
||||
@@ -150,12 +173,61 @@ func (s *Store) Switches(ctx context.Context) ([]Switch, error) {
|
||||
|
||||
// PutSwitch places a switch; one placed by hand is not moved by NetBox.
|
||||
func (s *Store) PutSwitch(ctx context.Context, x Switch) error {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO switches(name, building, room, rack, unit, height, model, source) VALUES ($1,$2,$3,$4,nullif($5,0),nullif($6,0),nullif($7,''),$8)
|
||||
ON CONFLICT (name) DO UPDATE SET building=$2, room=$3, rack=$4, unit=nullif($5,0), height=nullif($6,0), model=coalesce(nullif($7,''), switches.model), source=$8
|
||||
WHERE switches.source <> 'local' OR $8 = 'local'`, x.Name, x.Building, x.Room, x.Rack, x.Unit, x.Height, x.Model, orDefault(x.Source, "local"))
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO switches(name, building, room, rack, unit, height, model, role, source) VALUES ($1,$2,$3,$4,nullif($5,0),nullif($6,0),nullif($7,''),nullif($8,''),$9)
|
||||
ON CONFLICT (name) DO UPDATE SET building=$2, room=$3, rack=$4, unit=nullif($5,0), height=nullif($6,0), model=coalesce(nullif($7,''), switches.model),
|
||||
role=coalesce(nullif($8,''), switches.role), source=$9
|
||||
WHERE switches.source <> 'local' OR $9 = 'local'`, x.Name, x.Building, x.Room, x.Rack, x.Unit, x.Height, x.Model, x.Role, orDefault(x.Source, "local"))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Cables(ctx context.Context) ([]Cable, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT machine_id, port, switch, switch_port, source FROM cables ORDER BY machine_id, port`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []Cable{}
|
||||
for rows.Next() {
|
||||
var c Cable
|
||||
if err := rows.Scan(&c.Machine, &c.Port, &c.Switch, &c.SwitchPort, &c.Source); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// PutCable records a cable; one entered here is not replaced by NetBox's.
|
||||
func (s *Store) PutCable(ctx context.Context, c Cable) error {
|
||||
_, err := s.pool.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) VALUES ($1,$2,$3,$4,$5)
|
||||
ON CONFLICT (machine_id, port, source) DO UPDATE SET switch=$3, switch_port=$4`, c.Machine, c.Port, c.Switch, c.SwitchPort, orDefault(c.Source, "local"))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) DeleteCable(ctx context.Context, machine, port, source string) error {
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM cables WHERE machine_id=$1 AND port=$2 AND source=$3`, machine, port, source)
|
||||
return err
|
||||
}
|
||||
|
||||
// ReplaceNetBoxCables puts NetBox's cables in place of the ones read before.
|
||||
func (s *Store) ReplaceNetBoxCables(ctx context.Context, cs []Cable) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM cables WHERE source='netbox'`); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range cs {
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) VALUES ($1,$2,$3,$4,'netbox') ON CONFLICT DO NOTHING`,
|
||||
c.Machine, c.Port, c.Switch, c.SwitchPort); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteSwitch(ctx context.Context, name string) error {
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM switches WHERE name=$1`, name)
|
||||
return err
|
||||
@@ -179,30 +251,51 @@ func (a *App) handleCabling(w http.ResponseWriter, r *http.Request) {
|
||||
for i := range placed {
|
||||
sw[key(placed[i].Name)] = &placed[i]
|
||||
}
|
||||
recorded, _ := a.store.Cables(r.Context())
|
||||
byMachine := map[string][]Cable{}
|
||||
for _, c := range recorded {
|
||||
byMachine[c.Machine] = append(byMachine[c.Machine], c)
|
||||
}
|
||||
nonBMC := map[string]bool{} // switches something other than a BMC is cabled to
|
||||
out := []MachinePorts{}
|
||||
for _, m := range ms {
|
||||
if m.Location.Rack == "" {
|
||||
continue
|
||||
}
|
||||
ports, nb := portsOf(m)
|
||||
for _, l := range nb {
|
||||
s := sw[key(l.Switch)]
|
||||
ports = mergeCables(ports, byMachine[m.ID])
|
||||
get := func(name string) *Switch {
|
||||
s := sw[key(name)]
|
||||
if s == nil {
|
||||
s = &Switch{Name: l.Switch}
|
||||
sw[key(l.Switch)] = s
|
||||
s = &Switch{Name: name}
|
||||
sw[key(name)] = s
|
||||
}
|
||||
return s
|
||||
}
|
||||
for _, l := range nb { // what the switch says of itself
|
||||
s := get(l.Switch)
|
||||
s.Descr = orDefault(s.Descr, l.Descr)
|
||||
s.MgmtIP = orDefault(s.MgmtIP, l.MgmtIP)
|
||||
}
|
||||
for _, p := range ports {
|
||||
if p.Switch == "" {
|
||||
continue
|
||||
}
|
||||
s := get(p.Switch)
|
||||
s.Links++
|
||||
if s.Descr == "" {
|
||||
s.Descr = l.Descr
|
||||
}
|
||||
if s.MgmtIP == "" {
|
||||
s.MgmtIP = l.MgmtIP
|
||||
if p.Kind != "bmc" {
|
||||
nonBMC[key(p.Switch)] = true
|
||||
}
|
||||
}
|
||||
if len(ports) > 0 {
|
||||
out = append(out, MachinePorts{ID: m.ID, Ports: ports})
|
||||
}
|
||||
}
|
||||
for k, s := range sw {
|
||||
if s.Role == "" && s.Links > 0 && !nonBMC[k] {
|
||||
s.Role = "oob"
|
||||
}
|
||||
}
|
||||
for _, mp := range out { // switch names as the machines saw them, the place as stored
|
||||
for i := range mp.Ports {
|
||||
if s := sw[key(mp.Ports[i].Switch)]; s != nil && mp.Ports[i].Switch != "" {
|
||||
@@ -218,6 +311,82 @@ func (a *App) handleCabling(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, obj{"machines": out, "switches": list})
|
||||
}
|
||||
|
||||
// mergeCables adds the recorded cables to the ports LLDP filled: a port
|
||||
// without a neighbour takes NetBox's cable, else one entered here; the BMC's
|
||||
// port is added when a cable is recorded for it. A NetBox cable that
|
||||
// disagrees with LLDP is kept as netbox_says.
|
||||
func mergeCables(ports []Port, cs []Cable) []Port {
|
||||
pick := func(port string) (nb, local *Cable) {
|
||||
for i := range cs {
|
||||
if strings.EqualFold(cs[i].Port, port) {
|
||||
if cs[i].Source == "netbox" {
|
||||
nb = &cs[i]
|
||||
} else {
|
||||
local = &cs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
fill := func(p *Port) {
|
||||
nb, local := pick(p.Name)
|
||||
switch {
|
||||
case p.Source == "lldp":
|
||||
if nb != nil && (!strings.EqualFold(nb.SwitchPort, p.SwitchPort) || !sameSwitch(nb.Switch, p.Switch)) {
|
||||
p.NetBoxSays = nb.Switch + " " + nb.SwitchPort
|
||||
}
|
||||
case local != nil:
|
||||
p.Switch, p.SwitchPort, p.Source = local.Switch, local.SwitchPort, "local"
|
||||
case nb != nil:
|
||||
p.Switch, p.SwitchPort, p.Source = nb.Switch, nb.SwitchPort, "netbox"
|
||||
}
|
||||
}
|
||||
for i := range ports {
|
||||
fill(&ports[i])
|
||||
}
|
||||
bmc := Port{Name: "bmc", Kind: "bmc"}
|
||||
if fill(&bmc); bmc.Switch != "" {
|
||||
ports = append(ports, bmc)
|
||||
}
|
||||
return ports
|
||||
}
|
||||
|
||||
// sameSwitch: an LLDP name may be the FQDN of a NetBox name
|
||||
func sameSwitch(a, b string) bool {
|
||||
a, _, _ = strings.Cut(strings.ToLower(a), ".")
|
||||
b, _, _ = strings.Cut(strings.ToLower(b), ".")
|
||||
return a == b
|
||||
}
|
||||
|
||||
// handleCables records a cable by hand: {"machine","port","switch","switch_port"}
|
||||
// (port "bmc" for the BMC's); DELETE ?machine=&port= removes it.
|
||||
func (a *App) handleCables(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodDelete {
|
||||
q := r.URL.Query()
|
||||
if err := a.store.DeleteCable(r.Context(), q.Get("machine"), q.Get("port"), "local"); err != nil {
|
||||
fail(w, 500, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, obj{"deleted": q.Get("machine") + " " + q.Get("port")})
|
||||
return
|
||||
}
|
||||
var c Cable
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&c); err != nil || c.Machine == "" || c.Port == "" || strings.TrimSpace(c.Switch) == "" {
|
||||
fail(w, 400, errors.New("machine, port and switch are required"))
|
||||
return
|
||||
}
|
||||
if _, err := a.store.Get(r.Context(), c.Machine); err != nil {
|
||||
fail(w, 404, fmt.Errorf("machine %s: %w", c.Machine, err))
|
||||
return
|
||||
}
|
||||
c.Switch, c.SwitchPort, c.Source = strings.TrimSpace(c.Switch), strings.TrimSpace(c.SwitchPort), "local"
|
||||
if err := a.store.PutCable(r.Context(), c); err != nil {
|
||||
fail(w, 500, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, c)
|
||||
}
|
||||
|
||||
// handleSwitches places a switch by hand: {"name","building","room","rack","unit","height"};
|
||||
// DELETE ?name= forgets the place (NetBox's comes back on the next sync).
|
||||
func (a *App) handleSwitches(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -248,14 +417,25 @@ func (a *App) handleSwitches(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// ---------------------------------------------------------------- NetBox
|
||||
|
||||
// syncSwitches copies the place of every switch the machines' LLDP names
|
||||
// that NetBox has as a device with a rack.
|
||||
// syncSwitches reads the cables NetBox records on Rollcall's devices, then
|
||||
// copies the place of every switch those and the machines' LLDP name that
|
||||
// NetBox has as a device with a rack.
|
||||
func (a *App) syncSwitches(ctx context.Context, nb *netBox) error {
|
||||
ms, err := a.store.List(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
names := map[string]bool{}
|
||||
cables, err := nb.cables(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cables: %w", err)
|
||||
}
|
||||
if err := a.store.ReplaceNetBoxCables(ctx, cables); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range cables {
|
||||
names[c.Switch] = true
|
||||
}
|
||||
for _, m := range ms {
|
||||
for _, l := range lldpOf(invOf(m)) {
|
||||
names[l.Switch] = true
|
||||
@@ -291,8 +471,63 @@ func switchPlaces(devs []obj) []Switch {
|
||||
if rack == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, Switch{Name: str(d["name"]), Building: str(dig(d, "site", "name")), Room: str(dig(d, "location", "name")), Rack: rack,
|
||||
Unit: int(num(d["position"])), Height: 1, Model: str(dig(d, "device_type", "model")), Source: "netbox"})
|
||||
x := Switch{Name: str(d["name"]), Building: str(dig(d, "site", "name")), Room: str(dig(d, "location", "name")), Rack: rack,
|
||||
Unit: int(num(d["position"])), Height: 1, Model: str(dig(d, "device_type", "model")), Source: "netbox"}
|
||||
if role := strings.ToLower(str(dig(d, "role", "name")) + " " + str(dig(d, "role", "slug"))); strings.Contains(role, "oob") || strings.Contains(role, "out-of-band") || strings.Contains(role, "mgmt") || strings.Contains(role, "management") {
|
||||
x.Role = "oob"
|
||||
}
|
||||
out = append(out, x)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// bmcName: an interface NetBox models for the BMC (iDRAC, iLO, XCC, IMM, IPMI, BMC, OOB, mgmt)
|
||||
var bmcName = regexp.MustCompile(`(?i)^(idrac|ilo|xcc|imm|ipmi|bmc|oob|mgmt|management)`)
|
||||
|
||||
// cables: the cables on Rollcall's devices (tag and machine field) to
|
||||
// another device's interface, through patch panels (NetBox's connected
|
||||
// endpoint), as machine port → switch port; an interface
|
||||
// marked management-only or named like a BMC is the machine's "bmc".
|
||||
func (n *netBox) cables(ctx context.Context) ([]Cable, error) {
|
||||
devs, err := n.all(ctx, "dcim/devices/", url.Values{"tag": {n.tag}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
machineOf := map[int64]string{}
|
||||
var ids []string
|
||||
for _, d := range devs {
|
||||
if id := str(dig(d, "custom_fields", n.field)); id != "" && id != "-" {
|
||||
machineOf[nbID(d)] = id
|
||||
ids = append(ids, fmt.Sprint(nbID(d)))
|
||||
}
|
||||
}
|
||||
var out []Cable
|
||||
for len(ids) > 0 { // a page of devices per request
|
||||
batch := ids[:min(50, len(ids))]
|
||||
ids = ids[len(batch):]
|
||||
ifs, err := n.all(ctx, "dcim/interfaces/", url.Values{"device_id": batch, "cabled": {"true"}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range ifs {
|
||||
m := machineOf[nbRef(f, "device")]
|
||||
peers, kind := f["connected_endpoints"], str(f["connected_endpoints_type"]) // the far end, through patch panels
|
||||
if kind != "dcim.interface" {
|
||||
peers, kind = f["link_peers"], str(f["link_peers_type"])
|
||||
}
|
||||
for _, p := range toList(peers) {
|
||||
po := asObj(p)
|
||||
sw := str(dig(po, "device", "name"))
|
||||
if m == "" || sw == "" || kind != "dcim.interface" {
|
||||
continue
|
||||
}
|
||||
port := str(f["name"])
|
||||
if f["mgmt_only"] == true || bmcName.MatchString(port) {
|
||||
port = "bmc"
|
||||
}
|
||||
out = append(out, Cable{Machine: m, Port: port, Switch: sw, SwitchPort: str(po["name"]), Source: "netbox"})
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -62,3 +66,61 @@ func TestSwitchPlaces(t *testing.T) {
|
||||
t.Fatalf("%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeCables(t *testing.T) {
|
||||
ports := []Port{{Name: "eno1", Switch: "leaf-1.dc1", SwitchPort: "Ethernet3", Source: "lldp"}, {Name: "eno2"}, {Name: "eno3"}}
|
||||
got := mergeCables(ports, []Cable{
|
||||
{Port: "eno1", Switch: "leaf-1", SwitchPort: "Ethernet4", Source: "netbox"}, // NetBox thinks otherwise
|
||||
{Port: "eno2", Switch: "leaf-2", SwitchPort: "Ethernet3", Source: "netbox"},
|
||||
{Port: "eno2", Switch: "leaf-9", SwitchPort: "Ethernet9", Source: "local"}, // entered by hand: wins over NetBox
|
||||
{Port: "bmc", Switch: "oob-1", SwitchPort: "Gi1/0/7", Source: "netbox"},
|
||||
})
|
||||
if got[0].Switch != "leaf-1.dc1" || got[0].NetBoxSays != "leaf-1 Ethernet4" {
|
||||
t.Fatalf("lldp stays, netbox noted: %+v", got[0])
|
||||
}
|
||||
if got[1].Switch != "leaf-9" || got[1].Source != "local" || got[2].Switch != "" {
|
||||
t.Fatalf("recorded: %+v %+v", got[1], got[2])
|
||||
}
|
||||
if len(got) != 4 || got[3].Kind != "bmc" || got[3].Switch != "oob-1" || got[3].Source != "netbox" {
|
||||
t.Fatalf("bmc: %+v", got)
|
||||
}
|
||||
if n := mergeCables([]Port{{Name: "eno1", Switch: "a", SwitchPort: "1", Source: "lldp"}}, []Cable{{Port: "eno1", Switch: "A.example", SwitchPort: "1", Source: "netbox"}}); n[0].NetBoxSays != "" {
|
||||
t.Fatalf("the same switch by FQDN is no mismatch: %+v", n[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetBoxCables(t *testing.T) {
|
||||
nb, srv := fakeNB(t, map[string]string{
|
||||
"/api/dcim/devices/": `{"results": [{"id": 7, "name": "node7", "custom_fields": {"rollcall_machine": "m-7"}}, {"id": 8, "name": "x", "custom_fields": {"rollcall_machine": "-"}}], "next": null}`,
|
||||
"/api/dcim/interfaces/": `{"results": [
|
||||
{"name": "eno1", "device": {"id": 7}, "mgmt_only": false, "link_peers_type": "dcim.interface", "link_peers": [{"name": "Ethernet3", "device": {"name": "leaf-1"}}]},
|
||||
{"name": "iDRAC", "device": {"id": 7}, "mgmt_only": false, "link_peers_type": "dcim.interface", "link_peers": [{"name": "Gi1/0/7", "device": {"name": "oob-1"}}]},
|
||||
{"name": "eno2", "device": {"id": 7}, "link_peers_type": "dcim.frontport", "link_peers": [{"name": "1", "device": {"name": "patch-1"}}],
|
||||
"connected_endpoints_type": "dcim.interface", "connected_endpoints": [{"name": "Ethernet9", "device": {"name": "leaf-2"}}]},
|
||||
{"name": "eno3", "device": {"id": 7}, "link_peers_type": "dcim.frontport", "link_peers": [{"name": "2", "device": {"name": "patch-1"}}]}], "next": null}`,
|
||||
})
|
||||
defer srv.Close()
|
||||
got, err := nb.cables(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Cable{{Machine: "m-7", Port: "eno1", Switch: "leaf-1", SwitchPort: "Ethernet3", Source: "netbox"}, {Machine: "m-7", Port: "bmc", Switch: "oob-1", SwitchPort: "Gi1/0/7", Source: "netbox"},
|
||||
{Machine: "m-7", Port: "eno2", Switch: "leaf-2", SwitchPort: "Ethernet9", Source: "netbox"}} // through the patch panel; eno3 ends on one
|
||||
if len(got) != 3 || got[0] != want[0] || got[1] != want[1] || got[2] != want[2] {
|
||||
t.Fatalf("%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeNB: a NetBox answering fixed JSON per path
|
||||
func fakeNB(t *testing.T, pages map[string]string) (*netBox, *httptest.Server) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, ok := pages[r.URL.Path]
|
||||
if !ok {
|
||||
t.Errorf("unexpected %s", r.URL.Path)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, b)
|
||||
}))
|
||||
return &netBox{base: srv.URL, token: "t", tag: "rollcall", field: "rollcall_machine", client: srv.Client()}, srv
|
||||
}
|
||||
@@ -294,6 +294,8 @@ func (a *App) routes() http.Handler {
|
||||
}
|
||||
}
|
||||
mux.HandleFunc("POST /api/v1/switches", a.adminAuth(a.handleSwitches))
|
||||
mux.HandleFunc("POST /api/v1/cables", a.adminAuth(a.handleCables))
|
||||
mux.HandleFunc("DELETE /api/v1/cables", a.adminAuth(a.handleCables))
|
||||
mux.HandleFunc("DELETE /api/v1/switches", a.adminAuth(a.handleSwitches))
|
||||
mux.HandleFunc("GET /api/v1/sites", a.adminAuth(a.handleSites))
|
||||
mux.HandleFunc("POST /api/v1/sites", a.adminAuth(a.handleSites))
|
||||
|
||||
@@ -114,6 +114,15 @@ CREATE TABLE IF NOT EXISTS switches (
|
||||
model text,
|
||||
source text NOT NULL DEFAULT 'local'
|
||||
);
|
||||
ALTER TABLE switches ADD COLUMN IF NOT EXISTS role text;
|
||||
CREATE TABLE IF NOT EXISTS cables (
|
||||
machine_id text NOT NULL REFERENCES machines(id) ON DELETE CASCADE,
|
||||
port text NOT NULL,
|
||||
switch text NOT NULL,
|
||||
switch_port text NOT NULL DEFAULT '',
|
||||
source text NOT NULL,
|
||||
PRIMARY KEY (machine_id, port, source)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS sites (
|
||||
name text PRIMARY KEY,
|
||||
slug text,
|
||||
|
||||
@@ -88,7 +88,7 @@ async function refresh(full) {
|
||||
[S.ips, S.ipam] = await Promise.all([api('GET', '/api/v1/ips'), api('GET', '/api/v1/ipam').catch(() => null)]);
|
||||
}
|
||||
$('#n-machines').textContent = machines.length;
|
||||
const nb = $('#nav-netbox'), ui = S.ipam && S.ipam.provider === 'netbox' && S.ipam.ui; nb.hidden = !ui; if (ui) nb.href = ui + '/';
|
||||
const nb = $('#nav-netbox'), ui = S.ipam && S.ipam.provider === 'netbox' && S.ipam.ui; nb.style.display = ui ? '' : 'none'; if (ui) nb.href = ui + '/'; // .side a sets display: hidden would not hold
|
||||
const active = S.jobs.filter(j => j.state === 'pending' || j.state === 'running').length;
|
||||
$('#n-jobs').textContent = active ? `${active} active` : '';
|
||||
$('#status').textContent = `updated ${new Date().toLocaleTimeString()}`;
|
||||
|
||||
@@ -163,7 +163,7 @@ function drawRear(p, span, sleds, colors, px = 768) {
|
||||
if (n === 1) { rect('#13243f', xx, yMid - 4, 16, 8); xx += 20; }
|
||||
// network cards, one plate each, ports along it
|
||||
const cards = new Map();
|
||||
for (const pt of s.ports || []) { const kk = pt.card || pt.name; if (!cards.has(kk)) cards.set(kk, []); cards.get(kk).push(pt); }
|
||||
for (const pt of (s.ports || []).filter(pt => pt.kind !== 'bmc')) { const kk = pt.card || pt.name; if (!cards.has(kk)) cards.set(kk, []); cards.get(kk).push(pt); }
|
||||
for (const ports of cards.values()) {
|
||||
const fast = ports.some(pt => parseInt(pt.speed) >= 10000), pw = fast ? 14 : 12.5, ph = fast ? 9 : 10.5;
|
||||
const perRow = Math.max(1, Math.floor((x0 + sw - 4 - xx - 6) / (pw + 2)));
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<a href="#/tags" data-nav="tags">Tags</a>
|
||||
<a href="#/profiles" data-nav="profiles">Profiles</a>
|
||||
<a href="#/images" data-nav="images">OS images</a>
|
||||
<a id="nav-netbox" href="#" target="_blank" rel="noopener" hidden title="NetBox, through Rollcall: its own login">NetBox ↗</a>
|
||||
<a id="nav-netbox" href="#" target="_blank" rel="noopener" style="display:none" title="NetBox, through Rollcall: its own login">NetBox ↗</a>
|
||||
<div class="side-foot">
|
||||
<span id="status" class="muted"></span>
|
||||
<button id="logout" class="btn ghost" title="forget the admin token">Token</button>
|
||||
|
||||
@@ -204,7 +204,8 @@ function placeSwitches(site, room, cab, rackAt, portAt) {
|
||||
const at = pt.switch && portAt.get(mp.id + '|' + pt.name); if (!at) continue;
|
||||
(links[pt.switch] = links[pt.switch] || {})[at.rack] = (links[pt.switch][at.rack] || 0) + 1;
|
||||
}
|
||||
const taken = {}; // rack -> units switches took
|
||||
const taken = {}; // rack -> units switches took, the placed ones first so estimates go around them
|
||||
for (const s of cab.switches) if (links[s.name] && s.rack && s.unit && rackAt[lc(s.rack)] && sameRoom(s.room || '', room)) (taken[lc(s.rack)] = taken[lc(s.rack)] || []).push(s.unit);
|
||||
return cab.switches.filter(s => links[s.name]).map((s, i) => {
|
||||
const out = { ...s, color: SWITCH_COLORS[i % SWITCH_COLORS.length], here: Object.values(links[s.name]).reduce((a, b) => a + b, 0) };
|
||||
const inRoom = s.rack && (!s.building || inSite(site, s.building)) && sameRoom(s.room || '', room) && rackAt[lc(s.rack)];
|
||||
@@ -212,7 +213,7 @@ function placeSwitches(site, room, cab, rackAt, portAt) {
|
||||
const rack = inRoom ? lc(s.rack) : Object.entries(links[s.name]).sort((a, b) => b[1] - a[1])[0][0];
|
||||
const ra = rackAt[rack]; let unit = inRoom ? s.unit : 0;
|
||||
if (!unit) { for (let u = ra.H; u >= 1; u--) if (!ra.used.has(u) && !(taken[rack] || []).includes(u)) { unit = u; break; } }
|
||||
(taken[rack] = taken[rack] || []).push(unit);
|
||||
if (!inRoom || !s.unit) (taken[rack] = taken[rack] || []).push(unit);
|
||||
out.at = { rack, unit: unit || ra.H + 1 + (taken[rack].filter(u => !u).length - 1), estimated: !inRoom || !s.unit };
|
||||
return out;
|
||||
});
|
||||
@@ -233,7 +234,7 @@ function drawSwitchIn(sw, ra, keep, mat, colorOf) {
|
||||
m.position.set(0, y, TILE + 0.006 - 0.15);
|
||||
const edge = new T.LineSegments(keep(new T.EdgesGeometry(m.geometry)), mat('swl' + sw.color, () => new T.LineBasicMaterial({ color: new T.Color(sw.color) })));
|
||||
edge.position.copy(m.position);
|
||||
m.userData = { rack: ra.r.name, tip: `${sw.name}${sw.model ? ' · ' + sw.model : ''} · ${sw.here} cables here · ${sw.at.estimated ? 'place estimated: ' + (sw.source ? 'no unit' : 'not in NetBox') + ', set it in the panel' : 'U' + sw.at.unit + ' (' + sw.source + ')'}` };
|
||||
m.userData = { rack: ra.r.name, tip: `${sw.name}${sw.role === 'oob' ? ' (out-of-band)' : ''}${sw.model ? ' · ' + sw.model : ''} · ${sw.here} cables here · ${sw.at.estimated ? 'place estimated: ' + (sw.source ? 'no unit' : 'not in NetBox') + ', set it in the panel' : 'U' + sw.at.unit + ' (' + sw.source + ')'}` };
|
||||
ra.g.add(m, edge); R3.pick.push(m);
|
||||
sw.port = name => { const n = switchPortNo(name), p = d.at[n] || d.at['u' + (n % 6)]; return { lx: -RAIL / 2 + RAIL * p.x / MM.W, ly: y + (U_M - 0.004) / 2 - (U_M - 0.004) * p.y / (MM.U - 1) }; };
|
||||
}
|
||||
@@ -258,10 +259,10 @@ function cables(cab, rackAt, portAt, keep, mat) {
|
||||
} else pts.push(W(ra, side, ra.h + 0.08 + lift, OUT), W(ra, side * 0.5, 2.9, OUT)); // to a switch in another room: up through the ceiling
|
||||
const dim = SITE.sel && lc(SITE.sel) !== a.rack && !(sw.at && lc(SITE.sel) === sw.at.rack);
|
||||
const col = pt.link === false ? '#7a7f86' : sw.color;
|
||||
const m = new T.Mesh(keep(new T.TubeGeometry(new T.CatmullRomCurve3(pts, false, 'catmullrom', 0.02), pts.length * 12, 0.0035, 6, false)),
|
||||
const m = new T.Mesh(keep(new T.TubeGeometry(new T.CatmullRomCurve3(pts, false, 'catmullrom', 0.02), pts.length * 12, pt.kind === 'bmc' ? 0.0025 : 0.0035, 6, false)),
|
||||
mat('cab' + col + dim, () => new T.MeshStandardMaterial({ color: new T.Color(col), roughness: 0.5, transparent: dim, opacity: dim ? 0.15 : 1 })));
|
||||
const m0 = S.machines.find(x => x.id === mp.id);
|
||||
m.userData = { rack: ra.r.name, id: mp.id, tip: `${m0 ? nameOf(m0) : mp.id} ${pt.name}${pt.speed ? ' ' + pt.speed : ''} → ${pt.switch} ${pt.switch_port}${pt.vlan ? ' · VLAN ' + pt.vlan : ''}${pt.link === false ? ' · no link' : ''}${sw.away ? ' (in ' + sw.away + ')' : ''}` };
|
||||
m.userData = { rack: ra.r.name, id: mp.id, tip: `${m0 ? nameOf(m0) : mp.id} ${pt.kind === 'bmc' ? 'BMC' : pt.name}${pt.speed ? ' ' + pt.speed : ''} → ${pt.switch}${sw.role === 'oob' ? ' (OOB)' : ''} ${pt.switch_port}${pt.vlan ? ' · VLAN ' + pt.vlan : ''}${pt.link === false ? ' · no link' : ''}${sw.away ? ' (in ' + sw.away + ')' : ''} · ${pt.source === 'lldp' ? 'seen by LLDP' : pt.source === 'netbox' ? 'NetBox cable' : 'entered by hand'}${pt.netbox_says ? ' · NetBox says ' + pt.netbox_says : ''}` };
|
||||
R3.scene.add(m); if (!dim) R3.pick.push(m);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +239,7 @@ function wireRoom(siteName, roomArg) {
|
||||
const tu = $('#r3-turn'); if (tu) tu.onclick = () => room3dTurn();
|
||||
const cb = $('#r3-cables'); if (cb) cb.onclick = () => { cablesView.set(!cablesView.get()); render(); };
|
||||
$$('[data-swplace]').forEach(b => b.onclick = () => placeSwitch(site, room, b.dataset.swplace));
|
||||
$$('[data-cable]').forEach(a => a.onclick = ev => { ev.preventDefault(); const [id, port] = a.dataset.cable.split('|'); editCable(id, port); });
|
||||
if ($('#room3d')) { room3d(site, roomArg, racksOnFloor(site, room)); return; }
|
||||
room3dStop();
|
||||
const svg = $('svg.floor'); if (!svg) return;
|
||||
@@ -302,12 +303,32 @@ function cablingTable(site, rk) {
|
||||
const cab = S.cabling; if (!cab) return '';
|
||||
const ms = S.machines.filter(m => m.location && inSite(site, m.location.building) && sameRoom(m.location.room || '', rk.room) && lc(m.location.rack) === lc(rk.name))
|
||||
.sort((a, b) => (b.location.unit || 0) - (a.location.unit || 0));
|
||||
const rows = [];
|
||||
for (const m of ms) for (const p of ((cab.machines.find(x => x.id === m.id) || {}).ports || [])) {
|
||||
rows.push([`<a href="#/machine/${esc(m.id)}">${esc(nameOf(m))}</a>`, `<span class="mono">${esc(p.name)}</span>`, esc(p.speed || ''),
|
||||
p.switch ? `${esc(p.switch)} <span class="mono">${esc(p.switch_port)}</span>` : `<span class="muted">${p.link === false ? 'no link' : 'no LLDP'}</span>`, esc(p.vlan || '')]);
|
||||
const rows = [], src = { lldp: 'LLDP', netbox: 'NetBox', local: 'by hand' };
|
||||
for (const m of ms) {
|
||||
const ports = [...((cab.machines.find(x => x.id === m.id) || {}).ports || [])];
|
||||
if (!ports.some(p => p.kind === 'bmc')) ports.push({ name: 'bmc', kind: 'bmc' }); // the BMC's cable is NetBox's or entered here
|
||||
for (const p of ports) {
|
||||
const to = p.switch ? `${esc(p.switch)} <span class="mono">${esc(p.switch_port)}</span> <span class="muted">${src[p.source] || ''}</span>` : `<span class="muted">${p.kind === 'bmc' ? 'none recorded' : p.link === false ? 'no link' : 'no LLDP'}</span>`;
|
||||
rows.push(`<div class="cab-p"><span class="mono">${p.kind === 'bmc' ? 'BMC' : esc(p.name)}</span><span>${to}${p.vlan ? ` <span class="muted">VLAN ${esc(p.vlan)}</span>` : ''}${p.netbox_says ? `<br><span class="warn">NetBox: ${esc(p.netbox_says)}</span>` : ''}</span>
|
||||
${p.source === 'lldp' ? '<span></span>' : `<a href="#" data-cable="${esc(m.id)}|${esc(p.name)}" title="set the switch port by hand">✎</a>`}</div>`);
|
||||
}
|
||||
rows.push('</div>');
|
||||
rows.splice(rows.length - ports.length - 1, 0, `<div class="cab-m"><a href="#/machine/${esc(m.id)}">${esc(nameOf(m))}</a> <span class="muted">U${m.location.unit || '?'}</span>`);
|
||||
}
|
||||
return rows.length ? `<h3 style="margin-top:12px">Cabling</h3><div class="tw">${table(['Machine', 'Port', 'Speed', 'Switch port', 'VLAN'], rows)}</div>` : '';
|
||||
return rows.length ? `<h3 style="margin-top:12px">Cabling</h3>${rows.join('')}` : '';
|
||||
}
|
||||
|
||||
async function editCable(id, port) {
|
||||
const m = S.machines.find(x => x.id === id), cur = (((S.cabling || {}).machines || []).find(x => x.id === id) || { ports: [] }).ports.find(p => p.name === port) || {};
|
||||
const v = await dialog(`${m ? nameOf(m) : id} ${port === 'bmc' ? 'BMC' : port}`, [{ name: 'switch', label: 'Switch', value: cur.source === 'local' ? cur.switch : '' , placeholder: 'oob-1' },
|
||||
{ name: 'switch_port', label: 'Switch port', value: cur.source === 'local' ? cur.switch_port : '', placeholder: 'Gi1/0/7' }],
|
||||
'What the port is plugged into, when LLDP cannot see it (the BMC\'s port always) and NetBox has no cable. Empty: forget the one entered here.', 'Save');
|
||||
if (!v) return;
|
||||
try {
|
||||
if (!v.switch.trim()) await api('DELETE', `/api/v1/cables?machine=${encodeURIComponent(id)}&port=${encodeURIComponent(port)}`);
|
||||
else await api('POST', '/api/v1/cables', { machine: id, port, switch: v.switch.trim(), switch_port: v.switch_port.trim() });
|
||||
refresh(true);
|
||||
} catch (e) { toast(e.message, true); }
|
||||
}
|
||||
|
||||
// switchTable: the switches this room's cables lead to, where they stand
|
||||
@@ -315,7 +336,7 @@ function switchTable() {
|
||||
const sws = (typeof R3 !== 'undefined' && R3.switches) || []; if (!sws.length) return '';
|
||||
const where = s => s.away ? `in ${esc(s.away)}` : s.at ? `${esc(rackLabel(s.at.rack))} U${s.at.unit}${s.at.estimated ? ' <span class="warn">estimated</span>' : ` <span class="muted">${esc(s.source)}</span>`}` : '';
|
||||
return `<h3 style="margin-top:12px">Switches</h3><div class="sw-list">${sws.map(s => `<div class="sw-row"><b class="sw-dot" style="background:${esc(s.color)}"></b>
|
||||
<div><div><b>${esc(s.name)}</b>${s.model ? ` <span class="muted">${esc(s.model)}</span>` : ''} ${s.source === 'netbox' ? nbLink('/dcim/devices/' + nbQ({ name: s.name }), '↗') : ''}</div><div class="muted">${where(s)} · ${s.here} cables</div></div>
|
||||
<div><div><b>${esc(s.name)}</b>${s.role === 'oob' ? ' <span class="pill tag" title="out-of-band: BMC ports">OOB</span>' : ''}${s.model ? ` <span class="muted">${esc(s.model)}</span>` : ''} ${s.source === 'netbox' ? nbLink('/dcim/devices/' + nbQ({ name: s.name }), '↗') : ''}</div><div class="muted">${where(s)} · ${s.here} cables</div></div>
|
||||
<button class="btn sm" data-swplace="${esc(s.name)}">place…</button></div>`).join('')}</div>
|
||||
<p class="muted">A switch's place comes from NetBox (its rack and position) or is set here; without either it is drawn where most of its cables are.</p>`;
|
||||
}
|
||||
|
||||
@@ -241,3 +241,5 @@ svg.floor .rk text { font-size: 9px; fill: var(--fg); pointer-events: none; font
|
||||
.sw-row > div { min-width: 0; overflow-wrap: anywhere; } .sw-dot { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
|
||||
.nb-link { font-size: 12px; font-weight: 400; text-transform: none; letter-spacing: 0; white-space: nowrap; align-self: center; }
|
||||
.page-head .right .nb-link { margin-right: 6px; }
|
||||
.cab-m { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; }
|
||||
.cab-p { display: grid; grid-template-columns: 58px minmax(0, 1fr) 14px; gap: 6px; font-size: 12px; padding: 1px 0; } .cab-p > span { overflow-wrap: anywhere; }
|
||||
Reference in new issue
Block a user