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
- func CheckEmail(e Email) error
- func CheckWebhook(h Webhook) error
- type Alert
- type Config
- type Disk
- type DiskRule
- type Email
- type Manager
- func (m *Manager) BackupFailed(ctx context.Context, err error)
- func (m *Manager) Check(ctx context.Context)
- func (m *Manager) Config(ctx context.Context) (Config, error)
- func (m *Manager) Firing() []Alert
- func (m *Manager) History(ctx context.Context) []Alert
- func (m *Manager) Run(ctx context.Context)
- func (m *Manager) SaveConfig(ctx context.Context, c Config) error
- func (m *Manager) Started(ctx context.Context)
- func (m *Manager) Stopped(ctx context.Context)
- func (m *Manager) Test(ctx context.Context, c Config) Alert
- type RateRule
- type Readings
- type Rules
- type Store
- type Toggle
- type Webhook
Constants ¶
const ( RuleDisk = "disk" RuleFolder = "folder" RuleErrors = "errors" RuleTranscodes = "transcodes" RuleRestart = "restart" RuleBackup = "backup" RuleTest = "test" )
Rule names, as alerts and the settings call them.
const ( LevelInfo = "info" LevelWarning = "warning" LevelCritical = "critical" )
Levels.
const ( KindDiscord = "discord" KindNtfy = "ntfy" KindHomeAssistant = "homeassistant" KindJSON = "json" )
Webhook kinds.
const Interval = 30 * time.Second
Interval is how often the rules are checked.
Variables ¶
This section is empty.
Functions ¶
func CheckEmail ¶
CheckEmail says what's wrong with the email settings, if anything.
func CheckWebhook ¶
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.
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 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
BackupFailed tells the admins a database backup failed.
func (*Manager) Check ¶
Check takes readings, and sends alerts for rules that became true (and all-clears for those that ended).
func (*Manager) Run ¶
Run checks the rules every Interval until ctx ends, after saying the server started.
func (*Manager) SaveConfig ¶
SaveConfig stores the admin's settings.
func (*Manager) Started ¶
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.
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.