alerts

package
v0.4.111 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 31 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 alerts tells the owner, on their phone or by mail, that something needs them: a backup that failed, a disk failing or full, an app that crashed, an automatic update that failed or paused. Its channels are Shoutrrr service URLs (ntfy://, telegram://, smtp://, ...) kept in alerts.json, and every alert goes to every channel.

The alerts come from AppManagement's events on the message bus, from an hourly look at LocalStorage's disks and storages, and from pkg/autoupdate, in-process. An alert already sent is not sent again for six hours, even when it cleared and came back meanwhile, and a condition that clears sends one "resolved" message, only if its alert was sent and once per alert sent. That memory is the core's: a restart forgets it, and at worst a reminder comes early.

Two things are about the update of Docker, which stops every container. While it runs and just after, the apps' container events are dropped (Hub.Muted), raise and resolve alike. And when it ends the owner is told once, whether or not the dashboard is open, even across a restart of the core: the nonce of the last run told is kept in docker-update.notified.

And when the operating system's security updates are about to end, or have ended, the owner is told once for each state of each release, from a look every day: the last one told is kept in os-support.notified, written once a message has gone out, so that a state is looked at again, every day, until a channel has taken it. A Raspbian 32-bit box, which has no state, is told once that Docker 28 is its last.

Index

Constants

View Source
const (
	Backups   = "backups"
	Disks     = "disks"
	Updates   = "updates"
	Apps      = "apps"
	Resources = "resources"
	Fleet     = "fleet"
)

The categories of an alert, each on unless the owner turns it off.

View Source
const ConfigFile = "/var/lib/casaos/alerts.json"

ConfigFile holds the channels, the categories and the disk threshold (root, 0600: the URLs carry tokens).

View Source
const DockerNotifiedFile = "/var/lib/casaos/docker-update.notified"

DockerNotifiedFile holds the nonce of the last Docker update that was told to the owner. It is the core's only memory of it: a restart of the core, at any moment of a run, changes nothing.

View Source
const OSSupportNotifiedFile = "/var/lib/casaos/os-support.notified"

OSSupportNotifiedFile holds the last state of the operating system's support that was told to the owner, "<id>-<version_id>-<state>", or "raspbian-docker28" for a Raspbian box. It is the core's only memory of it: a restart changes nothing.

Variables

View Source
var (
	// ErrInvalid is a PUT body that is not one: a URL Shoutrrr cannot parse, a
	// new channel without a URL, an unknown channel id, a threshold outside 50
	// to 99. Nothing is written.
	ErrInvalid = errors.New("invalid alerts settings")
	// ErrNoChannel is a test of a channel id that is not one.
	ErrNoChannel = errors.New("no such channel")
)
View Source
var Default = New("/")

Default is the box's own: the API answers from it and main runs its sources.

Functions

This section is empty.

Types

type Change

type Change struct {
	Channels      []Channel       `json:"channels"`
	Categories    map[string]bool `json:"categories"`
	DiskThreshold *int            `json:"disk_threshold"`
}

Change is PUT /v1/sys/alerts's body, each field optional, others ignored. Channels replaces the list: an entry with an id and no url keeps its stored URL, a new entry needs a url.

type Channel

type Channel struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	URL  string `json:"url"`
}

Channel is where alerts go: a Shoutrrr URL, never answered back.

type ChannelStatus

type ChannelStatus struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Service string `json:"service"`
	Host    string `json:"host"`
}

ChannelStatus is a channel as the dashboard sees it: service is the URL's scheme, host its host part (the ntfy server, the SMTP host), enough to recognise it. Host is "" for a service whose host part is not a server.

type Config

type Config struct {
	Channels      []Channel       `json:"channels"`
	Categories    map[string]bool `json:"categories"`
	DiskThreshold int             `json:"disk_threshold"` // a percentage, 50 to 99
}

Config is alerts.json.

type DockerNotReturned added in v0.4.67

type DockerNotReturned struct {
	Name string
	// RestartPolicy is Docker's. A container whose policy is "" or "no" does not start again by
	// itself; one that has another may, and was not running when the unit stopped waiting.
	RestartPolicy string
}

DockerNotReturned is a container that was running before the update and was not running when the unit stopped waiting for it.

type DockerRun added in v0.4.67

type DockerRun struct {
	// Nonce names the run, and is "" when there is no run to tell of.
	Nonce string
	// Outcome is "success", "restart_pending" or "failed" once the run has ended, and "" while it
	// has not.
	Outcome string
	// ErrorCode is why a run failed: guard, download, install, daemon, start or no_result.
	ErrorCode string
	// To is the engine version the status says: the one that runs after a run that succeeded, the
	// plan's for a run that did not. Only a success tells it.
	To string
	// NotReturned are the containers that were running before the update and were not after,
	// already validated by whoever read the log.
	NotReturned []DockerNotReturned
}

DockerRun is a run of the Docker update as far as the owner's phone is concerned. The service reads it from the run's log and from systemd; nothing here talks to Docker.

type Failure

type Failure struct {
	At      time.Time `json:"at"`
	Channel string    `json:"channel"` // its name
	Error   string    `json:"error"`
}

Failure is the last send that failed, since the core started.

type Hub

type Hub struct {
	// Root is where alerts.json is kept: "/" on a box, a temporary directory
	// in tests.
	Root string
	// RuntimePath is where the other services leave their addresses and this
	// boot's secret.
	RuntimePath func() string
	Now         func() time.Time
	Hostname    func() (string, error)
	// Address is the dashboard's address, or "" when the core does not know it.
	Address func() string
	// MasterLink is the fleet master's address and this box's id there when the box is a member.
	MasterLink func() (masterURL, boxID string, ok bool)
	// Send delivers one message to one channel URL.
	Send func(rawURL, title, message string) error
	// Latest is the newest release as the update button reads it, Current the
	// installed one, and AutoUpdates whether automatic updates are on: with
	// them off, a newer release is an alert of its own.
	Latest      func() string
	Current     func() string
	AutoUpdates func() bool
	// Muted says whether the apps' container events are to be ignored now: a Docker update stops
	// every container, and each app's watch says so. It is asked for each of those events, so it
	// must be cheap.
	Muted func() bool
	// LastDockerRun is the last run of the Docker update, from the run's log and systemd.
	LastDockerRun func() DockerRun
	// OSSupport is the operating system and how long its security updates last, as the service says
	// it: only the last two states that end it are told.
	OSSupport func() ossupport.Report
	// OSUpgradeRetries are the waits before a notice of the upgrade of the operating system that no channel took is sent again; a test shortens them.
	OSUpgradeRetries []time.Duration
	// OnAppEvent, when set, hears app:install-end, app:uninstall-end and app:apply-changes-end, muted or
	// not (the sharing service follows the apps with it). It runs on the bus's goroutine: it returns at once.
	OnAppEvent func(name string, properties map[string]string)
	// contains filtered or unexported fields
}

Hub is the alerts of one box.

func New

func New(root string) *Hub

New keeps alerts.json under root and sends through Shoutrrr. Address, Latest, AutoUpdates, Muted, LastDockerRun and OSSupport are left to the caller.

func (*Hub) AutoUpdate

func (h *Hub) AutoUpdate(result, release string)

AutoUpdate is pkg/autoupdate's hook: an automatic update of release that succeeded, failed, or failed a second time and paused.

func (*Hub) Delivers added in v0.4.104

func (h *Hub) Delivers(category string) bool

Delivers reports whether an alert of category would be sent: a channel is set and the category is on.

func (*Hub) FleetBackupProblem added in v0.4.104

func (h *Hub) FleetBackupProblem(key, sentence string, problem bool)

FleetBackupProblem is pkg/fleet's hook: a backup problem of a box, raised at every pass of the master's poller while it lasts (said once in six hours), or resolved. The sentence is pkg/fleet's, with no text from the box.

func (*Hub) FleetOffline added in v0.4.90

func (h *Hub) FleetOffline(boxID, name string, offline bool)

FleetOffline is pkg/fleet's hook: a box of the master's fleet has had no tunnel for ten minutes (offline), or has one again.

func (*Hub) Handle added in v0.4.80

func (h *Hub) Handle(name string, properties map[string]string)

Handle is the entry of a source that runs in this process: it takes an event as the message bus would hand it.

func (*Hub) OSUpgradeNotice added in v0.4.74

func (h *Hub) OSUpgradeNotice(key, sentence string) bool

OSUpgradeNotice tells the owner of what became of an upgrade of the operating system, or that it waits for them: the core says each thing once (it keeps its own record of what it told, which survives a restart of the core), and this is only the way out to the channels. The key is the core's own, one for each thing there is to say, so that a run that stopped, was resumed and stopped again is told twice, and the six hours that an alert stays quiet are for the repeats of one thing and not for the next. It returns at once, and says whether a channel was there to take the message (the category of updates is on and there is one): the sending is made on its own, and when no channel takes the message it is sent again after the waits of osUpgradeRetries, for as long as the core lives and a channel is there.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run follows the message bus until ctx is done, tells a Docker update that ended or is going on, tells the operating system's support when it is ending or ended (five minutes after the start, then every day), and looks at the disks and for a newer release five minutes after the start, then every hour, and at the software RAID arrays five minutes after the start, then every five minutes.

func (*Hub) Status

func (h *Hub) Status() Status

Status reads alerts.json.

func (*Hub) Test

func (h *Hub) Test(id string) ([]Result, error)

Test sends a test message to the channel id, or to every channel when id is "", and answers per channel once each has answered, in 15 seconds at most.

func (*Hub) Update

func (h *Hub) Update(change Change) (Status, error)

Update applies c, rewriting a malformed alerts.json, and answers the new status. A body that is not one is ErrInvalid, and nothing is written.

func (*Hub) WatchDockerUpdate added in v0.4.67

func (h *Hub) WatchDockerUpdate()

WatchDockerUpdate is the update button's hook: a run was just started, tell the owner when it ends. It returns at once.

type Result

type Result struct {
	ID    string `json:"id"`
	OK    bool   `json:"ok"`
	Error string `json:"error"`
}

Result is one channel's answer to a test.

type Status

type Status struct {
	Channels      []ChannelStatus `json:"channels"`
	Categories    map[string]bool `json:"categories"`
	DiskThreshold int             `json:"disk_threshold"`
	LastFailure   *Failure        `json:"last_failure"`
}

Status is what GET and PUT /v1/sys/alerts answer in data: the channels without their URLs, which never leave the box's disk.

type Volume added in v0.4.80

type Volume struct {
	MountPoint, Label string
	Size, Used        uint64
}

Volume is a storage of LocalStorage's list, sizes in bytes.

func StorageVolumes added in v0.4.80

func StorageVolumes(ctx context.Context, runtimePath string) ([]Volume, error)

StorageVolumes is the dashboard's list of storages (GET /v1/storage?system=show, children flattened). A storage whose sizes are not numbers, or without a mount point, is skipped.

Jump to

Keyboard shortcuts

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