inventory

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: GPL-3.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package inventory models the set of machines hlab manages and the rules for selecting subsets of them.

Index

Constants

View Source
const (
	IfaceEthernet = "ethernet"
	IfaceWiFi     = "wifi"
	IfaceBridge   = "bridge"
	IfaceVirtual  = "virtual"
	IfaceLoopback = "loopback"
)

Interface kinds, as reported by the collector.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	Runtime string `yaml:"runtime" json:"runtime"`
	Name    string `yaml:"name" json:"name"`
	Image   string `yaml:"image,omitempty" json:"image,omitempty"`
	Status  string `yaml:"status,omitempty" json:"status,omitempty"`
}

Container is a running container on a managed machine.

type Host

type Host struct {
	Name    string `yaml:"name" json:"name"`
	Address string `yaml:"address" json:"address"`
	Port    int    `yaml:"port,omitempty" json:"port"`

	// Managed reports whether hlab has credentials for this machine. Only
	// managed machines are reachable, so only they can be selected by commands
	// that connect.
	//
	// It has no yaml tag because the on-disk form needs to tell an absent key
	// from an explicit false: see config.hostEntry.
	Managed bool `yaml:"-" json:"managed"`

	// Access details, meaningful only when Managed.
	User         string `yaml:"user,omitempty" json:"user,omitempty"`
	IdentityFile string `yaml:"identityFile,omitempty" json:"identityFile,omitempty"`

	Tags        []string `yaml:"tags,omitempty" json:"tags,omitempty"`
	Description string   `yaml:"description,omitempty" json:"description,omitempty"`

	// Observed fields, refreshed by each scan.
	Hostname  string    `yaml:"hostname,omitempty" json:"hostname,omitempty"`
	OS        string    `yaml:"os,omitempty" json:"os,omitempty"`
	Banner    string    `yaml:"banner,omitempty" json:"banner,omitempty"`
	FirstSeen time.Time `yaml:"firstSeen,omitempty" json:"firstSeen,omitempty"`
	LastSeen  time.Time `yaml:"lastSeen,omitempty" json:"lastSeen,omitempty"`

	// Durable attributes of the machine, filled in by `hlab discover services`.
	// Volatile state — running services, listening ports, containers — is
	// deliberately not here: it belongs in the facts store, so this file stays
	// something a person can read and edit.
	Kernel   string       `yaml:"kernel,omitempty" json:"kernel,omitempty"`
	Arch     string       `yaml:"arch,omitempty" json:"arch,omitempty"`
	CPUs     int          `yaml:"cpus,omitempty" json:"cpus,omitempty"`
	MemoryMB int64        `yaml:"memoryMB,omitempty" json:"memoryMB,omitempty"`
	Network  *NetworkInfo `yaml:"network,omitempty" json:"network,omitempty"`

	// Containers are recorded here rather than in the facts store because they
	// describe what a machine is *for*, not what it happens to be doing: a
	// homelab's containers change about as often as its installed services.
	Containers []Container `yaml:"containers,omitempty" json:"containers,omitempty"`

	LastCollected time.Time `yaml:"lastCollected,omitempty" json:"lastCollected,omitempty"`
}

Host is one machine hlab knows about, whether or not it manages it.

A single record accumulates everything learned about a machine: what a scan observed, and — once enrolled — how to reach it. Managed reports which of those two states it is in.

func (Host) Addr

func (h Host) Addr() string

Addr is the dial target, host:port.

func (Host) HasTag

func (h Host) HasTag(tag string) bool

HasTag reports whether the host carries the given tag (case insensitive).

type Interface

type Interface struct {
	Name string `yaml:"name" json:"name"`
	// Type is ethernet, wifi, bridge, virtual or loopback, determined from
	// /sys rather than guessed from the name.
	Type string `yaml:"type,omitempty" json:"type,omitempty"`
	MAC  string `yaml:"mac,omitempty" json:"mac,omitempty"`
	MTU  int    `yaml:"mtu,omitempty" json:"mtu,omitempty"`
	// Up is the administrative state; Carrier is whether a link is actually
	// present — a cable plugged in, or a wireless association. An interface can
	// be up with no carrier, which is exactly the case that must not be
	// mistaken for a working connection.
	Up      bool `yaml:"up" json:"up"`
	Carrier bool `yaml:"carrier" json:"carrier"`
	// SpeedMbps is the negotiated link speed, where the driver reports one.
	SpeedMbps int `yaml:"speedMbps,omitempty" json:"speedMbps,omitempty"`
	// DHCP reports whether any address on this interface was leased rather
	// than configured statically.
	DHCP      bool     `yaml:"dhcp" json:"dhcp"`
	Addresses []string `yaml:"addresses,omitempty,flow" json:"addresses,omitempty"`
}

Interface is one network interface on a machine.

The MAC address matters beyond diagnostics: it identifies the hardware across DHCP changes, so a machine that moves address can still be recognised.

func (Interface) Working

func (i Interface) Working() bool

Working reports whether the interface is both up and carrying a link.

type Inventory

type Inventory struct {
	// contains filtered or unexported fields
}

Inventory is the resolved set of hosts plus named groups.

func New

func New(hosts []Host, groups map[string][]string) (*Inventory, error)

New builds an inventory, validating that host names are unique and that every group member refers to a real host.

func (*Inventory) ByAddress

func (i *Inventory) ByAddress(address string, port int) (Host, bool)

ByAddress looks a machine up by address and port, which is how scan results are matched to existing records.

func (*Inventory) GroupNames

func (i *Inventory) GroupNames() []string

GroupNames returns the sorted list of group names.

func (*Inventory) Groups

func (i *Inventory) Groups() map[string][]string

Groups returns the group name -> member names map.

func (*Inventory) Host

func (i *Inventory) Host(name string) (Host, bool)

Host looks a host up by exact name.

func (*Inventory) Hosts

func (i *Inventory) Hosts() []Host

Hosts returns every known machine, managed or not, sorted by name.

func (*Inventory) Managed

func (i *Inventory) Managed() []Host

Managed returns only the machines hlab has credentials for.

func (*Inventory) Select

func (i *Inventory) Select(expr string) (selected, skipped []Host, err error)

Select resolves a target expression to machines that can actually be reached, i.e. managed ones. Commands that connect must use this rather than SelectAll: an unmanaged machine has no credentials, so targeting it could only fail, and silently including every device on the network in `all` would be dangerous.

Matching unmanaged machines are reported in skipped so callers can say so.

func (*Inventory) SelectAll

func (i *Inventory) SelectAll(expr string) ([]Host, error)

SelectAll resolves a target expression against every known machine, managed or not. It backs listing and reporting, never connecting.

The expression language (all, @group, #tag, name/glob, !exclusion) lives in internal/selector, shared with the printer inventory.

func (*Inventory) Unmanaged

func (i *Inventory) Unmanaged() []Host

Unmanaged returns machines that have been seen but not enrolled.

type NetworkInfo

type NetworkInfo struct {
	Gateway string   `yaml:"gateway,omitempty" json:"gateway,omitempty"`
	DNS     []string `yaml:"dns,omitempty,flow" json:"dns,omitempty"`
	Search  []string `yaml:"search,omitempty,flow" json:"search,omitempty"`
	// Manager is what owns the network configuration on this machine —
	// NetworkManager, netplan, dhcpcd and so on. It decides how the
	// configuration can later be changed.
	Manager    string      `yaml:"manager,omitempty" json:"manager,omitempty"`
	Interfaces []Interface `yaml:"interfaces,omitempty" json:"interfaces,omitempty"`
}

NetworkInfo is how a machine is attached to the network.

func (*NetworkInfo) ActiveWiFi

func (n *NetworkInfo) ActiveWiFi() []Interface

ActiveWiFi returns the wireless interfaces that are currently associated.

func (*NetworkInfo) MACs

func (n *NetworkInfo) MACs() []string

MACs returns every MAC address observed on the machine.

func (*NetworkInfo) PrimaryAddress

func (n *NetworkInfo) PrimaryAddress() string

PrimaryAddress returns the first non-loopback IPv4 address, which is the one worth showing in a table.

func (*NetworkInfo) WiFiRedundant

func (n *NetworkInfo) WiFiRedundant() bool

WiFiRedundant reports machines where wireless is active while a working ethernet link is also present — the candidates for turning WiFi off.

func (*NetworkInfo) WorkingEthernet

func (n *NetworkInfo) WorkingEthernet() []Interface

WorkingEthernet returns the ethernet interfaces that are up, carrying a link and hold an address — the ones a machine is genuinely reachable over.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL