SPB Git

Repository Public

inventory.go 4.3 KB 136 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/agent/collect/inventory.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package collect4344import (45	"context"46	"net"47	"runtime"48	"sort"49	"strings"50	"time"5152	"clustr.spboucher.ai/mcc/internal/agent/runcmd"53	"clustr.spboucher.ai/mcc/internal/protocol"54)5556// Inventory describes the machine. It is collected at start, every 6 hours57// and on demand. Fields that cannot be determined are left empty.58func (c *Collector) Inventory(ctx context.Context) protocol.Inventory {59	ctx, cancel := context.WithTimeout(ctx, 20*time.Second)60	defer cancel()61	inv := protocol.Inventory{Arch: runtime.GOARCH, Hostname: platformHostname()}62	platformInventory(ctx, &inv)63	if inv.CPUCores == 0 {64		inv.CPUCores = runtime.NumCPU()65	}66	if vols, err := platformVolumes(ctx); err == nil {67		for _, v := range vols {68			if v.Mount == "/" {69				inv.StorageBytes = v.Total70			}71		}72	}73	_, defIface := c.Gateway(ctx, time.Now())74	inv.Interfaces = interfaces(ctx, defIface)75	// Managers detected on the machine (installed), whether or not they manage anything.76	if runcmd.Find("pm2", "/opt/homebrew/bin/pm2", "/usr/local/bin/pm2") != "" {77		inv.Managers = append(inv.Managers, "pm2")78	}79	if runcmd.Find("docker", "/usr/local/bin/docker", "/opt/homebrew/bin/docker", "/usr/bin/docker") != "" {80		inv.Managers = append(inv.Managers, "docker")81	}82	sort.Strings(inv.Managers)83	return inv84}8586func interfaces(ctx context.Context, defIface string) []protocol.Interface {87	ifs, err := net.Interfaces()88	if err != nil {89		return nil90	}91	var names []string92	for _, ifc := range ifs {93		names = append(names, ifc.Name)94	}95	ports, media, wireless := platformInterfaceMeta(ctx, names)96	var out []protocol.Interface97	for _, ifc := range ifs {98		if ifc.Flags&net.FlagLoopback != 0 || skipInterface(ifc) {99			continue100		}101		addrs, _ := ifc.Addrs()102		var list []string103		for _, a := range addrs {104			if usableAddr(a) {105				list = append(list, a.String())106			}107		}108		up := ifc.Flags&net.FlagUp != 0 && ifc.Flags&net.FlagRunning != 0109		if len(list) == 0 && !(up && ports[ifc.Name] != "") {110			continue // nothing to say about an address-less virtual interface111		}112		if len(list) == 0 {113			continue114		}115		mac := strings.ToLower(ifc.HardwareAddr.String())116		if mac == "02:00:00:00:00:00" {117			mac = "" // placeholder macOS hands to unprivileged processes: not a real address118		}119		out = append(out, protocol.Interface{120			Name: ifc.Name, Kind: InterfaceKind(ifc.Name, ports[ifc.Name], wireless[ifc.Name]),121			MAC: mac, Addrs: list, Media: media[ifc.Name],122			Up: up, Default: ifc.Name == defIface,123		})124	}125	sort.SliceStable(out, func(i, j int) bool {126		if out[i].Default != out[j].Default {127			return out[i].Default128		}129		return out[i].Name < out[j].Name130	})131	if len(out) > 24 {132		out = out[:24]133	}134	return out135}136
Files main
Menu

Theme