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.
Index ¶
- Constants
- Variables
- type Change
- type Channel
- type ChannelStatus
- type Config
- type DockerNotReturned
- type DockerRun
- type Failure
- type Hub
- func (h *Hub) AutoUpdate(result, release string)
- func (h *Hub) FleetOffline(boxID, name string, offline bool)
- func (h *Hub) Handle(name string, properties map[string]string)
- func (h *Hub) OSUpgradeNotice(key, sentence string) bool
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) Status() Status
- func (h *Hub) Test(id string) ([]Result, error)
- func (h *Hub) Update(change Change) (Status, error)
- func (h *Hub) WatchDockerUpdate()
- type Result
- type Status
- type Volume
Constants ¶
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.
const ConfigFile = "/var/lib/casaos/alerts.json"
ConfigFile holds the channels, the categories and the disk threshold (root, 0600: the URLs carry tokens).
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.
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>". It is the core's only memory of it: a restart changes nothing.
Variables ¶
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") )
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 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
// contains filtered or unexported fields
}
Hub is the alerts of one box.
func New ¶
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 ¶
AutoUpdate is pkg/autoupdate's hook: an automatic update of release that succeeded, failed, or failed a second time and paused.
func (*Hub) FleetOffline ¶ added in v0.4.90
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
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
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 ¶
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) Test ¶
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 ¶
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 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.