swarm

package
v1.4.1 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: 18 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 swarm talks to the machines over ssh: it lists them, probes them, streams their logs and descriptions, opens shells and runs actions.

Index

Constants

View Source
const FullDisk = 90

FullDisk is the use, in percent, from which the fullest disk is a problem.

Variables

This section is empty.

Functions

func DescribeMachine

func DescribeMachine() string

DescribeMachine is the describe command of a machine, for ParseMachine. top's first frame averages since boot: the second covers the last second. A home that doesn't exist keeps the user's top settings (~/.toprc: a line per CPU, other columns...) out of the format parseTop reads. df leaves out pseudo filesystems, container layers and snaps. lscpu runs in the C locale, so that its field names are the ones sed looks for.

func DescribeUnit

func DescribeUnit(unit string) string

DescribeUnit is the describe command of a systemd unit, for ParseUnit.

func Reboot added in v1.2.0

func Reboot() string

Reboot is the command that reboots a machine, for remote.Run, 2 seconds later: once ssh has ended. It names systemctl by its full path, which systemd's own search path may lack (NixOS).

func RunProbe

func RunProbe(ctx context.Context, h inventory.Host, timeout time.Duration) (string, error)

RunProbe returns the probe's raw output, without control characters. It gives up after timeout.

func Stream

func Stream(ctx context.Context, h inventory.Host, cmd string, follow bool) <-chan string

Stream runs cmd on h and sends its output lines: the last ones, or with follow, those that come next too, cmd being a logs command, which -f follows. A followed command gets a terminal, so that ending ssh hangs it up; another stops on the host after oneShotTimeout. The channel closes when the command ends or ctx is cancelled.

func UnitAction added in v1.2.0

func UnitAction(v Verb, unit string) string

UnitAction is the command that starts, stops or restarts a systemd unit, for remote.Run.

func UnitLogs

func UnitLogs(unit string) string

UnitLogs is the log command of a systemd unit, for Stream.

Types

type Condition added in v1.3.0

type Condition int

Condition is how something on a machine is doing, from idle to broken.

const (
	Idle    Condition = iota // there, but doing nothing: an inactive unit
	Fine                     // running, or done well
	Warning                  // on its way, or held: starting, paused, just restarted; a machine with problems
	Broken                   // failed, crashed, unhealthy; a machine that's down
)

The conditions, from the best to the worst but for Idle, which is neither.

type Container

type Container struct {
	Name, State   string    // State: running, exited, created...
	Status        string    // as docker words it, e.g. "Up 2 hours (healthy)"; "" if unknown
	Health        string    // its healthcheck's: healthy, unhealthy or starting; "" without one
	ExitCode      int       // its last exit's
	Since         time.Time // when it last started or stopped; zero if never, or unknown
	Restarts      int       // by docker (its restart policy), since it was last started by hand
	RecentRestart bool      // the last of them was less than restartWindow ago
}

Container is one container of a compose stack.

func (Container) Condition added in v1.3.0

func (c Container) Condition() Condition

Condition rates c: fine running and not unhealthy, or done with exit code 0, as init containers are; a warning on its way or held: health starting, just restarted on its own, created, paused, being removed; else broken: crashed, restarting (a crash loop), dead.

func (Container) JustRestarted added in v1.4.0

func (c Container) JustRestarted() bool

JustRestarted says whether c is a warning for having just restarted on its own, which its status, "Up 40 seconds", wouldn't tell.

func (Container) Trouble added in v1.4.0

func (c Container) Trouble() string

Trouble words what's wrong with c, after its name: that it just restarted on its own, or else its status, or its state if docker didn't word one; "" if it's fine.

type ContainerInfo

type ContainerInfo struct {
	Container // but its Status, which docker inspect doesn't word

	Image, Command          string
	Project, Service        string // its compose stack and service, if any
	Running                 bool
	StartedAt               time.Time
	RestartPolicy           string
	HealthLast              string // its healthcheck's last output
	HealthStreak            int    // failed checks in a row
	Ports, Networks, Mounts []string
	EnvNames                []string
	CPUs                    int
	Stats                   *Stats    // nil unless docker stats answered
	Now                     time.Time // the host's clock, when described: ages are computed against it
}

ContainerInfo is what DescribeContainer found about a container. The values of its environment are left out on purpose: they're often secrets.

func ParseContainer

func ParseContainer(out string) (ContainerInfo, error)

ParseContainer reads DescribeContainer's output.

type Disk

type Disk struct {
	Mount            string
	Size, Used, Free uint64
}

Disk is a mounted filesystem, in bytes.

type Docker added in v1.3.0

type Docker string

Docker is the command that reaches docker on a host, as the probe found it, e.g. "sudo -n docker".

func (Docker) ContainerAction added in v1.3.0

func (d Docker) ContainerAction(v Verb, container string) string

ContainerAction is the command that starts, stops or restarts a container, for remote.Run.

func (Docker) ContainerLogs added in v1.3.0

func (d Docker) ContainerLogs(container string) string

ContainerLogs is the log command of one container, for Stream.

func (Docker) ContainerShell added in v1.3.0

func (d Docker) ContainerShell(container string) string

ContainerShell is the command that opens a shell in a container, for remote.Interactive: bash if the image has it, else sh.

func (Docker) DescribeContainer added in v1.3.0

func (d Docker) DescribeContainer(container string) string

DescribeContainer is the describe command of a container, for ParseContainer: what it uses, then everything docker knows about it.

func (Docker) DescribeStack added in v1.3.0

func (d Docker) DescribeStack(stack string) string

DescribeStack is the describe command of a compose stack, for ParseStack: its containers, what docker inspect says of them, then what the running ones use. Without any container to name, docker inspect would fail, and docker stats list every container: they're named.

func (Docker) StackAction added in v1.3.0

func (d Docker) StackAction(v Verb, stack string) string

StackAction is the command that starts, stops or restarts a compose stack's containers, for remote.Run.

func (Docker) StackLogs added in v1.3.0

func (d Docker) StackLogs(stack string) string

StackLogs is the log command of a compose stack, all its containers, for Stream.

type Machine

type Machine struct {
	OS, Kernel, Arch, Virt, Model string // Virt: e.g. kvm, "none" on bare metal
	CPUs                          int
	Uptime                        time.Duration
	Load                          [3]float64 // over 1, 5 and 15 minutes
	CPUBusy                       *float64   // percent of all CPUs busy over a second; nil without top

	MemTotal, MemAvailable, SwapTotal, SwapFree uint64 // bytes

	Disks     []Disk
	Processes []Process // the busiest, from top
}

Machine is what DescribeMachine found on a machine.

func ParseMachine

func ParseMachine(out string) (Machine, error)

ParseMachine reads DescribeMachine's output.

type Process

type Process struct {
	PID, User, Command string
	CPU, Mem           float64
}

Process is one of top's processes; CPU and Mem in percent.

type Report

type Report struct {
	Host          inventory.Host
	Err           error
	Errors        []string
	OS            string    // e.g. Debian GNU/Linux 12 (bookworm)
	Now           time.Time // the host's clock: ages are computed against it
	Uptime        time.Duration
	Deployed      time.Time // NixOS: the last deploy; zero elsewhere
	RebootPending bool
	Load          *float64 // percent, nil if unknown: the 1-minute load per CPU, past 100 when tasks wait
	Memory        *float64 // percent in use (not available to programs), nil if unknown
	Disk          *float64 // percent used of the fullest local disk, nil if unknown
	Revision      string   // NixOS: the git commit the system was built from, if set
	Units         []Unit
	Docker        Docker // "" without docker
	Stacks        []Stack
}

Report is what one probe found on one host. Err is set when the probe failed; Errors lists the probe's commands that failed while the rest worked.

func Parse

func Parse(out string) (Report, error)

Parse reads a probe's output, as RunProbe returns it. Only a missing meta section is an error: a command that failed, or output that doesn't parse, becomes one of Report.Errors.

func ProbeAll

func ProbeAll(ctx context.Context, hosts []inventory.Host, timeout time.Duration) []Report

ProbeAll probes the hosts in parallel, each within timeout. The reports keep the hosts' order.

func (Report) Age

func (r Report) Age(t time.Time) time.Duration

Age is the time since t, on the host's clock.

func (Report) Condition added in v1.3.0

func (r Report) Condition() Condition

Condition rates r's machine: broken when it's down, a warning when it has problems, else fine.

func (Report) Failed

func (r Report) Failed() []Unit

Failed returns the units in the failed state.

func (Report) Problems

func (r Report) Problems() []string

Problems lists what's wrong: the host is down, or failed commands, a disk nearly full, failed units, broken containers, and what just restarted on its own.

func (Report) StacksUp

func (r Report) StacksUp() int

StacksUp counts the stacks with no broken container.

func (Report) Watched added in v1.3.0

func (r Report) Watched() int

Watched counts the units worth watching (Unit.Interesting): those the services tab lists.

type Stack

type Stack struct {
	Name       string
	Containers []Container
}

Stack is a compose project and its containers.

func (Stack) Bad

func (s Stack) Bad() []Container

Bad returns the containers that are broken.

func (Stack) Condition added in v1.3.0

func (s Stack) Condition() Condition

Condition is the worst of s's containers' conditions.

func (Stack) Since added in v1.2.0

func (s Stack) Since() time.Time

Since is when anything in s last started or stopped: its youngest container's Since, or the zero time.

type StackContainer

type StackContainer struct {
	Container

	Service, Ports string
	Stats          *Stats
}

StackContainer is one of a stack's containers; Stats is nil unless it runs.

type StackInfo

type StackInfo struct {
	CPUs       int
	Containers []StackContainer
}

StackInfo is what DescribeStack found about a compose stack.

func ParseStack

func ParseStack(out string) (StackInfo, error)

ParseStack reads DescribeStack's output.

type Stats

type Stats struct {
	CPU, Mem                       float64 // percent: CPU of one CPU, as docker counts; memory of its limit
	MemUsage, NetIO, BlockIO, PIDs string  // as docker prints them: "620MiB / 15.2GiB"
}

Stats is what docker stats says of a running container.

type Unit

type Unit struct {
	Name, Active, Sub, Description string
	Result, UnitFileState          string
	Since                          time.Time // last state change; zero if never
	Restarts                       int       // by systemd (Restart=), since it was last started by hand
	RecentRestart                  bool      // the last of them was less than restartWindow ago
}

Unit is a systemd service on a host.

func (Unit) Condition added in v1.3.0

func (u Unit) Condition() Condition

Condition rates u by its active state: failed is broken, changing a warning, active fine unless it just restarted on its own, and anything else idle.

func (Unit) Interesting

func (u Unit) Interesting() bool

Interesting says whether u is worth watching: broken, just restarted on its own, or enabled to run. The rest are static or inactive units: hundreds of them, rarely of interest.

func (Unit) JustRestarted added in v1.4.0

func (u Unit) JustRestarted() bool

JustRestarted says whether u is a warning for having just restarted on its own.

type UnitInfo

type UnitInfo struct {
	Unit

	File, Restart, TriggeredBy string
	MainPID, ExitStatus        int // the main process, 0 if none; its last exit status

	Memory, MemoryMax uint64 // bytes, as docker counts a container's; 0: unknown, or no limit
	CPUTime           time.Duration
	Tasks             uint64

	Journal []string  // its last lines
	Now     time.Time // the host's clock, when described: ages are computed against it
}

UnitInfo is what DescribeUnit found about a systemd unit.

func ParseUnit

func ParseUnit(out string) (UnitInfo, error)

ParseUnit reads DescribeUnit's output.

type Verb added in v1.3.0

type Verb string

Verb is what an action does to a unit, a stack or a container.

const (
	Start   Verb = "start"
	Stop    Verb = "stop"
	Restart Verb = "restart"
)

The actions.

Jump to

Keyboard shortcuts

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