alerts

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: AGPL-3.0 Imports: 22 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 alerts tells admins when something needs them: a disk that's nearly full, a library folder that can't be read, a run of errors, transcodes that keep failing, or the server starting again. Alerts go straight from the server to the admin's own email server or webhooks (Discord, ntfy, Home Assistant or any other); nothing goes through anyone else's servers.

Index

Constants

View Source
const (
	RuleDisk       = "disk"
	RuleFolder     = "folder"
	RuleErrors     = "errors"
	RuleTranscodes = "transcodes"
	RuleRestart    = "restart"
	RuleBackup     = "backup"
	RuleTest       = "test"
)

Rule names, as alerts and the settings call them.

View Source
const (
	LevelInfo     = "info"
	LevelWarning  = "warning"
	LevelCritical = "critical"
)

Levels.

View Source
const (
	KindDiscord       = "discord"
	KindNtfy          = "ntfy"
	KindHomeAssistant = "homeassistant"
	KindJSON          = "json"
)

Webhook kinds.

View Source
const Interval = 30 * time.Second

Interval is how often the rules are checked.

Variables

This section is empty.

Functions

func CheckEmail

func CheckEmail(e Email) error

CheckEmail says what's wrong with the email settings, if anything.

func CheckWebhook

func CheckWebhook(h Webhook) error

CheckWebhook says what's wrong with a webhook's settings, if anything.

Types

type Alert

type Alert struct {
	ID string `json:"id"`
	// Rule is what raised it; Key tells apart alerts from the same rule,
	// such as one per disk.
	Rule     string `json:"rule"`
	Key      string `json:"key"`
	Level    string `json:"level"`
	Title    string `json:"title"`
	Message  string `json:"message"`
	Time     int64  `json:"time"` // milliseconds
	Resolved bool   `json:"resolved,omitempty"`
	// Sent is how many channels got it, and Failed says why the others
	// didn't.
	Sent   int      `json:"sent"`
	Failed []string `json:"failed,omitempty"`
}

Alert is one message to the admins.

type Config

type Config struct {
	Email    Email     `json:"email"`
	Webhooks []Webhook `json:"webhooks"`
	Rules    Rules     `json:"rules"`
}

Config is what admins set up under Settings → Server health → Alerts.

func (Config) Channels

func (c Config) Channels() int

Channels says whether the config sends alerts anywhere.

func (*Config) Normalize

func (c *Config) Normalize()

Normalize fills in defaults and keeps numbers in range.

type Disk

type Disk struct {
	// Name says what's on it, such as "data, transcode" or "Movies".
	Name      string
	Path      string
	Total     uint64
	Free      uint64
	Reachable bool
	// Library is true for a disk with a library folder on it.
	Library bool
}

Disk is a disk the server uses, for the disk and folder rules.

type DiskRule

type DiskRule struct {
	On      bool `json:"on"`
	Percent int  `json:"percent"`
}

DiskRule alerts when a disk is Percent full or more.

type Email

type Email struct {
	On   bool   `json:"on"`
	Host string `json:"host"`
	Port int    `json:"port"`
	// Security is "starttls" (the default), "tls" for a connection that's
	// encrypted from the start (usually port 465), or "none".
	Security string `json:"security"`
	Username string `json:"username"`
	Password string `json:"password,omitempty"`
	From     string `json:"from"`
	// To is one or more addresses, separated by commas.
	To string `json:"to"`
}

Email sends alerts through the admin's own mail server.

type Manager

type Manager struct {
	Store Store
	Log   *slog.Logger
	// Server is the server's name, and URL its address (for a link to
	// Server health), when it has one.
	Server string
	URL    string
	// Version is the server's version, for the restart alert.
	Version string
	// Read takes the readings; nil reads nothing.
	Read func(ctx context.Context) Readings
	// Client sends webhooks; nil uses a client with a timeout.
	Client *http.Client
	// SendMail sends an email; nil uses the email settings' server.
	SendMail func(ctx context.Context, e Email, subject, body string) error
	// contains filtered or unexported fields
}

Manager checks the rules and sends alerts.

func (*Manager) BackupFailed added in v0.2.0

func (m *Manager) BackupFailed(ctx context.Context, err error)

BackupFailed tells the admins a database backup failed.

func (*Manager) Check

func (m *Manager) Check(ctx context.Context)

Check takes readings, and sends alerts for rules that became true (and all-clears for those that ended).

func (*Manager) Config

func (m *Manager) Config(ctx context.Context) (Config, error)

Config reads the admin's settings, with defaults for what was never set.

func (*Manager) Firing

func (m *Manager) Firing() []Alert

Firing lists the alerts that are true now and were sent.

func (*Manager) History

func (m *Manager) History(ctx context.Context) []Alert

History lists the latest alerts sent, newest first.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context)

Run checks the rules every Interval until ctx ends, after saying the server started.

func (*Manager) SaveConfig

func (m *Manager) SaveConfig(ctx context.Context, c Config) error

SaveConfig stores the admin's settings.

func (*Manager) Started

func (m *Manager) Started(ctx context.Context)

Started notes that the server is running, and sends the restart alert: whether it was updated, and whether it stopped without shutting down cleanly last time.

func (*Manager) Stopped

func (m *Manager) Stopped(ctx context.Context)

Stopped notes a clean shutdown, so the next start isn't taken for a crash.

func (*Manager) Test

func (m *Manager) Test(ctx context.Context, c Config) Alert

Test sends a test alert to every channel in c and says how it went.

type RateRule

type RateRule struct {
	On      bool `json:"on"`
	Count   int  `json:"count"`
	Minutes int  `json:"minutes"`
}

RateRule alerts when Count or more happen within Minutes.

type Readings

type Readings struct {
	Disks []Disk
	// Errors is how many errors were logged since the server started, and
	// Problems the latest kinds of them.
	Errors   int64
	Problems []health.Problem
	// TranscodeFailures is how many transcodes failed since it started.
	TranscodeFailures int64
}

Readings is the server now, for the rules.

type Rules

type Rules struct {
	Disk       DiskRule `json:"disk"`
	Folder     Toggle   `json:"folder"`
	Errors     RateRule `json:"errors"`
	Transcodes RateRule `json:"transcodes"`
	Restart    Toggle   `json:"restart"`
	Backup     Toggle   `json:"backup"`
}

Rules says which alerts to send.

func DefaultRules

func DefaultRules() Rules

DefaultRules are the rules until an admin changes them: all on.

type Store

type Store interface {
	Setting(ctx context.Context, key string) (string, error)
	SetSetting(ctx context.Context, key, value string) error
}

Store keeps settings.

type Toggle

type Toggle struct {
	On bool `json:"on"`
}

Toggle is a rule that's on or off.

type Webhook

type Webhook struct {
	ID   string `json:"id"`
	Kind string `json:"kind"`
	Name string `json:"name,omitempty"`
	URL  string `json:"url,omitempty"`
	// Token is an ntfy access token, for a topic that needs one.
	Token string `json:"token,omitempty"`
}

Webhook posts alerts to an address.

Jump to

Keyboard shortcuts

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