hostwatch

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 24 Imported by: ? 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 hostwatch measures the host's disk, inode and memory usage and mails an operator when one of them crosses a threshold. It runs as its own container (cmd/hostwatch) so that it keeps reporting when the API does not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadHostname

func ReadHostname(rootFS string) string

ReadHostname returns the host's name from rootFS/etc/hostname. The kernel hostname under /proc is per UTS namespace and would name the container.

Types

type Collector

type Collector interface {
	// Collect returns every sample it could take. A non-nil error with samples
	// means some measurements failed and the rest are still valid.
	Collect(ctx context.Context) ([]Sample, error)
}

type Config

type Config struct {
	Interval       time.Duration
	Hostname       string
	RootFS         string
	Rules          map[Kind]Rule
	Hysteresis     float64
	RemindEvery    time.Duration
	ExcludeFSTypes map[string]bool
	ExcludeMounts  map[string]bool
	SMTP           SMTPConfig
	HeartbeatURL   string
}

func LoadConfig

func LoadConfig(getenv func(string) string) (Config, error)

LoadConfig reads HOSTWATCH_* variables through getenv. Unlike pkg/config it rejects a malformed value instead of falling back, since a typo in a threshold would otherwise silently disable the alert it was meant to set.

type Evaluator

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

Evaluator turns samples into due notifications. It is not safe for concurrent use; the monitor loop owns it.

func NewEvaluator

func NewEvaluator(rules map[Kind]Rule, hysteresis float64, remind time.Duration) *Evaluator

func (*Evaluator) MarkSent

func (e *Evaluator) MarkSent(now time.Time, events []Event)

MarkSent records that events were delivered at now.

func (*Evaluator) Observe

func (e *Evaluator) Observe(now time.Time, samples []Sample) []Event

Observe records samples and returns the events due now, most severe first. Nothing is marked as sent; call MarkSent once delivery succeeded, so that a failed mail is retried on the next tick. A key absent from samples keeps its state untouched, so a mount that failed statfs once does not re-alert.

type Event

type Event struct {
	Sample Sample
	Level  Level
	Reason Reason
	Since  time.Time
}

Event is a notification that is due for one sample.

type Heartbeat

type Heartbeat interface {
	Beat(ctx context.Context, up bool, msg string) error
}

Heartbeat tells an external watcher that hostwatch is alive, so that its silence is noticed from outside the host.

type HostCollector

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

HostCollector reads the host through gopsutil. In a container, point HOST_PROC and HOST_PROC_MOUNTINFO at the host's /proc and RootFS at the host's / so that statfs lands on the host's filesystems.

func NewHostCollector

func NewHostCollector(cfg Config) *HostCollector

func (*HostCollector) Collect

func (c *HostCollector) Collect(ctx context.Context) ([]Sample, error)

type Kind

type Kind string
const (
	KindDisk   Kind = "disk"
	KindInode  Kind = "inode"
	KindMemory Kind = "memory"
)

type Level

type Level int
const (
	LevelOK Level = iota
	LevelWarning
	LevelCritical
)

func (Level) String

func (l Level) String() string

type Message

type Message struct {
	Subject string
	Body    string
}

type Monitor

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

Monitor runs collect, evaluate, notify and heartbeat once per tick.

func NewMonitor

func NewMonitor(cfg Config, c Collector, n Notifier, hb Heartbeat, log zerolog.Logger) *Monitor

func (*Monitor) Run

func (m *Monitor) Run(ctx context.Context, interval time.Duration)

Run ticks immediately and then every interval until ctx is done.

func (*Monitor) Start

func (m *Monitor) Start(ctx context.Context) error

Start takes one full reading and mails it. Any failure is returned so the process exits and a broken setup shows up as a restart loop, not silence.

func (*Monitor) Tick

func (m *Monitor) Tick(ctx context.Context)

type Notifier

type Notifier interface {
	Send(ctx context.Context, msg Message) error
}

type PushHeartbeat

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

PushHeartbeat calls an Uptime Kuma style push URL with status and msg.

func NewPushHeartbeat

func NewPushHeartbeat(raw string) (*PushHeartbeat, error)

func (*PushHeartbeat) Beat

func (h *PushHeartbeat) Beat(ctx context.Context, up bool, msg string) error

type Reason

type Reason int
const (
	ReasonRaised Reason = iota
	ReasonEscalated
	ReasonImproved
	ReasonRecovered
	ReasonReminder
)

func (Reason) String

func (r Reason) String() string

type Rule

type Rule struct {
	Warn float64
	Crit float64
	// For is how long a level must hold before it is raised; zero raises at once.
	For time.Duration
}

Rule holds the thresholds for one kind of measurement, in percent.

type SMTPConfig

type SMTPConfig struct {
	Host     string
	Port     int
	Username string
	Password string
	From     *mail.Address
	To       []*mail.Address
}

type SMTPNotifier

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

SMTPNotifier delivers over STARTTLS, or implicit TLS on port 465. There is no plaintext mode: the session carries the SMTP password.

func NewSMTPNotifier

func NewSMTPNotifier(cfg SMTPConfig) *SMTPNotifier

func (*SMTPNotifier) Send

func (n *SMTPNotifier) Send(ctx context.Context, msg Message) error

type Sample

type Sample struct {
	Key     string
	Kind    Kind
	Label   string
	Percent float64
	Detail  string
}

Sample is one measurement. Key identifies it across ticks.

Jump to

Keyboard shortcuts

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