Make nmon easier to install and run without editing YAML by hand: - Settings: point-and-click forms over config.yaml (GET/PUT /api/v1/settings/form); the YAML editor moves to /settings/yaml. - First install: with no sign-in configured nmon starts in install mode, serves only the setup wizard (sign-in, basics, devices, alerting) and closes it for good once sign-in is saved. A self-signed certificate is generated when no TLS source is given. poller.interval becomes a config setting. - Reverse proxies: opt-in --http-listen plain-HTTP listener beside HTTPS; sign-in stays required. - Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets, template preview and test send. - Help: the operations, coverage and profile docs served at /help. - Polling: per-device "Poll now", a dry-run test poll returning an SNMP trace, and time-limited in-memory SNMP debug recording, in the UI and as `nmon device poll`. - Ansible: deploys into install mode when no sign-in is set, and no longer overwrites the config.yaml the wizard wrote. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
211 lines
6.3 KiB
Go
211 lines
6.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/v10networks/nmon/internal/alert"
|
|
"github.com/v10networks/nmon/internal/alertpolicy"
|
|
"github.com/v10networks/nmon/internal/alertrule"
|
|
"github.com/v10networks/nmon/internal/clock"
|
|
"github.com/v10networks/nmon/internal/profile"
|
|
"github.com/v10networks/nmon/internal/store"
|
|
)
|
|
|
|
// ruleRefresh is how often the custom rule table is re-read; an API write invalidates it at once.
|
|
const ruleRefresh = 30 * time.Second
|
|
|
|
// ruleStore is what the rule registry reads.
|
|
type ruleStore interface {
|
|
store.AlertRules
|
|
store.Devices
|
|
store.InterfaceInventory
|
|
store.ParentLinks
|
|
}
|
|
|
|
// ruleRegistry is the one list of alert rules: the built-ins, then config.yaml rules (deprecated), then the
|
|
// custom rules in the database. The evaluator asks it for the rule set every cycle, so an edit applies on the
|
|
// next cycle without a restart. It also backs the API: the catalogue the builder offers, validation, and a dry run.
|
|
type ruleRegistry struct {
|
|
st ruleStore
|
|
clk clock.Clock
|
|
log *slog.Logger
|
|
static []alert.Rule // built-ins with their config patches, then config rules
|
|
builtin map[string]alert.Rule
|
|
pollEach time.Duration
|
|
cadence *store.PollCadence // the live default; overrides pollEach when set
|
|
query alert.Querier
|
|
profiles func() *profile.Set
|
|
inv *inventoryReader
|
|
|
|
custom *ttlCache[struct{}, []customRule]
|
|
cat *ttlCache[struct{}, alertrule.Catalogue]
|
|
mu sync.Mutex
|
|
lastCat alertrule.Catalogue
|
|
lastOK map[string]alert.Rule // last rule that compiled, by name, kept while the catalogue is briefly short
|
|
}
|
|
|
|
// customRule is a stored rule and what it compiled to.
|
|
type customRule struct {
|
|
row store.AlertRule
|
|
spec alertrule.RuleSpec
|
|
built alertrule.Built
|
|
rule alert.Rule
|
|
err error
|
|
}
|
|
|
|
func newRuleRegistry(st ruleStore, clk clock.Clock, static []alert.Rule, pollEach time.Duration, q alert.Querier,
|
|
profiles func() *profile.Set, log *slog.Logger) *ruleRegistry {
|
|
r := &ruleRegistry{st: st, clk: clk, log: log, static: static, builtin: map[string]alert.Rule{}, pollEach: pollEach,
|
|
query: q, profiles: profiles, lastOK: map[string]alert.Rule{}}
|
|
for _, d := range alert.DefaultRules() {
|
|
r.builtin[d.Name] = d
|
|
}
|
|
r.inv = newInventoryReader(st, clk, log)
|
|
r.custom = newTTLCache[struct{}, []customRule](clk, ruleRefresh)
|
|
r.cat = newTTLCache[struct{}, alertrule.Catalogue](clk, catalogueTTL)
|
|
return r
|
|
}
|
|
|
|
// Changed drops the caches, so a rule write applies on the next evaluation cycle.
|
|
func (r *ruleRegistry) Changed() {
|
|
r.custom.invalidate()
|
|
r.inv.invalidate()
|
|
}
|
|
|
|
// Reserved reports a name a custom rule cannot take.
|
|
func (r *ruleRegistry) Reserved(name string) bool {
|
|
for _, s := range r.static {
|
|
if s.Name == name {
|
|
return true
|
|
}
|
|
}
|
|
switch name {
|
|
case alertpolicy.RuleDeviceDown, alertpolicy.RuleDeviceWarning, alertpolicy.RuleSystem:
|
|
return true
|
|
}
|
|
return alert.SystemRule(name)
|
|
}
|
|
|
|
// Set is alert.Options.RuleSet: the static rules plus every custom rule that compiles. A custom rule that does
|
|
// not compile now (its metric left the catalogue for a moment) keeps its last good form.
|
|
func (r *ruleRegistry) Set() []alert.Rule {
|
|
out := append([]alert.Rule(nil), r.static...)
|
|
for _, c := range r.customRules() {
|
|
if r.Reserved(c.row.Name) {
|
|
r.log.Warn("custom alert rule ignored: its name is taken by a built-in or config rule", "rule", c.row.Name)
|
|
continue
|
|
}
|
|
out = append(out, c.rule)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// customRules returns the stored rules, compiled. While the store fails it returns the last good list.
|
|
func (r *ruleRegistry) customRules() []customRule {
|
|
cs, err := r.custom.get(struct{}{}, func() ([]customRule, error) { return r.loadCustom(context.Background()) })
|
|
if err != nil {
|
|
r.log.Warn("load custom alert rules", "err", err)
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
for _, old := range r.lastOK {
|
|
if !r.isStatic(old.Name) {
|
|
cs = append(cs, customRule{row: store.AlertRule{Name: old.Name}, rule: old})
|
|
}
|
|
}
|
|
}
|
|
return cs
|
|
}
|
|
|
|
func (r *ruleRegistry) isStatic(name string) bool {
|
|
for _, s := range r.static {
|
|
if s.Name == name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (r *ruleRegistry) loadCustom(ctx context.Context) ([]customRule, error) {
|
|
rows, err := r.st.ListAlertRules(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cat := r.catalogue(ctx)
|
|
out := make([]customRule, 0, len(rows))
|
|
live := map[string]bool{}
|
|
for _, row := range rows {
|
|
c := r.compileRow(row, cat)
|
|
live[row.Name] = true
|
|
r.remember(c)
|
|
if c.err != nil {
|
|
r.log.Warn("custom alert rule does not compile", "rule", row.Name, "err", c.err)
|
|
if old, ok := r.lastGood(row.Name); ok {
|
|
c.rule = old
|
|
c.rule.Disabled = !row.Enabled
|
|
} else {
|
|
continue
|
|
}
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
r.forget(live)
|
|
return out, nil
|
|
}
|
|
|
|
func (r *ruleRegistry) remember(c customRule) {
|
|
if c.err != nil {
|
|
return
|
|
}
|
|
r.mu.Lock()
|
|
r.lastOK[c.row.Name] = c.rule
|
|
r.mu.Unlock()
|
|
}
|
|
|
|
func (r *ruleRegistry) lastGood(name string) (alert.Rule, bool) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
rule, ok := r.lastOK[name]
|
|
return rule, ok
|
|
}
|
|
|
|
// forget drops last-good entries of rules that no longer exist.
|
|
func (r *ruleRegistry) forget(live map[string]bool) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
for n := range r.lastOK {
|
|
if !live[n] {
|
|
delete(r.lastOK, n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// compileRow turns a stored rule into an evaluator rule.
|
|
func (r *ruleRegistry) compileRow(row store.AlertRule, cat alertrule.Catalogue) customRule {
|
|
c := customRule{row: row}
|
|
if err := json.Unmarshal([]byte(row.Spec), &c.spec); err != nil {
|
|
c.err = fmt.Errorf("spec: %w", err)
|
|
return c
|
|
}
|
|
d := alertrule.Draft{Name: row.Name, Kind: row.Kind, Severity: row.Severity, For: time.Duration(row.ForSeconds) * time.Second,
|
|
Summary: row.Summary, Description: row.Description, Spec: c.spec}
|
|
c.built, c.err = alertrule.Build(d, cat, r.every())
|
|
if c.err != nil {
|
|
return c
|
|
}
|
|
c.rule = alert.Rule{Name: row.Name, Severity: row.Severity, Expr: c.built.Expr, Op: c.built.Op, Threshold: c.built.Threshold,
|
|
For: d.For, Labels: c.built.Labels, Summary: c.built.Summary, Description: c.built.Description,
|
|
SkipUnreliable: c.built.SkipUnreliable, Disabled: !row.Enabled}
|
|
if c.built.Inventory != nil {
|
|
c.rule.Source = r.inv.source(c.built.Inventory)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// every is the default poll interval rules are built against: the live cadence, else the fixed one.
|
|
func (r *ruleRegistry) every() time.Duration { return r.cadence.Or(r.pollEach) }
|