console: approved alert copy
Applies the Luna copy pass and ruling R-31 to the alert messages, the box's own alert text and the carlos status --live words. Subjectless variants for a message with every member snoozed, rule display names in headlines, clears and the box footer, no format verb without its argument (TestAlertEmitNoteHasNoFormatErrors).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
14 files changed,
+627
−102
internal/alerting/ruletext.go+64 −0internal/console/alertcopy_test.go+199 −0internal/console/alertdial.go+8 −8internal/console/alertengine_pass.go+45 −10internal/console/alertengine_pass_test.go+9 −9internal/console/alertnotify.go+177 −49internal/console/jobs.go+1 −1internal/console/jobs_test.go+1 −1internal/host/alertsink.go+42 −11internal/host/alertsink_geoip_test.go+2 −2internal/host/alertsink_test.go+70 −2internal/host/enginebeat.go+3 −3internal/host/enginebeat_linux_test.go+4 −4statuslive_test.go+2 −2
diff --git a/internal/alerting/ruletext.go b/internal/alerting/ruletext.go| new file mode 100644 |
| index 000000000..0e6eed593 |
| --- /dev/null |
| +++ b/internal/alerting/ruletext.go |
| @@ -0,0 +1,64 @@ |
| +package alerting |
| + |
| +// The words the console and a box's own alert path both say about the |
| +// rules a box evaluates, so the two cannot drift: the console's alertCopy |
| +// reads them from here, and the box's formatAlert (internal/host) does too. |
| +// Approved by the copy review of fleet alerts plan 1 (the deck is |
| +// docs/superpowers/notes/copy-fleet-alerts-1.md). A box message never names |
| +// a channel, so none of these may. |
| + |
| +// Labels in front of a message's lines. |
| +const ( |
| + LabelHowLong = "How long you have" |
| + LabelWhatToDo = "What to do" |
| +) |
| + |
| +// ReadingFormat introduces a rule's last reading in a message. |
| +const ReadingFormat = "Reading: %s." |
| + |
| +// RuleText is one rule's display name and its "how long you have" and "what |
| +// to do" lines. |
| +type RuleText struct { |
| + Title string |
| + HowLong string |
| + WhatToDo string |
| +} |
| + |
| +// BoxRuleText is the text of each rule a box evaluates, by rule ID. |
| +var BoxRuleText = map[string]RuleText{ |
| + "disk_eta": { |
| + Title: "Disk full forecast", |
| + HowLong: "The last reading gives the time to full.", |
| + WhatToDo: "Free space on it, or give it a bigger disk before it is full.", |
| + }, |
| + "disk": { |
| + Title: "Disk nearly full", |
| + HowLong: "Once it is full, writes fail. It may fill soon.", |
| + WhatToDo: "Free space on it now, or give it a bigger disk.", |
| + }, |
| + "mem": { |
| + Title: "High memory use", |
| + HowLong: "There is no fixed time. If memory runs out, the system stops processes, and apps on the box go down with them.", |
| + WhatToDo: "Check what is using memory on it.", |
| + }, |
| + "cpu": { |
| + Title: "High CPU use", |
| + HowLong: "Nothing is down, but requests are slower while it stays this busy.", |
| + WhatToDo: "Check what is using it, or move apps to another box.", |
| + }, |
| + "lag": { |
| + Title: "Mail relay behind", |
| + HowLong: "Messages are already late. The delay grows until the relay catches up.", |
| + WhatToDo: "Check the relay on that box.", |
| + }, |
| + "geoip": { |
| + Title: "Location database", |
| + HowLong: "Lookups keep using the old data. If the database expires, they stop.", |
| + WhatToDo: "Check the location data feed.", |
| + }, |
| + "engine_silent": { |
| + Title: "Alert engine silent", |
| + HowLong: "Until it runs again, the console sends no alerts. Each box still sends its own.", |
| + WhatToDo: "Check the console service on that box and restart it if it is stuck.", |
| + }, |
| +} |
diff --git a/internal/console/alertcopy_test.go b/internal/console/alertcopy_test.go| new file mode 100644 |
| index 000000000..4722c64a5 |
| --- /dev/null |
| +++ b/internal/console/alertcopy_test.go |
| @@ -0,0 +1,199 @@ |
| +package console |
| + |
| +import ( |
| + "errors" |
| + "maps" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Every rule the console knows, and one it does not (a newer box's). |
| +func alertCopyRules() []string { |
| + return append(slices.Sorted(maps.Keys(alertRuleLevels)), "rule_from_a_newer_box") |
| +} |
| + |
| +// noFormatDebris fails on what a mismatched Sprintf leaves in text a person |
| +// reads, and on a verb that was never filled. |
| +func noFormatDebris(t *testing.T, what, text string) { |
| + t.Helper() |
| + for _, bad := range []string{"%!", "%[", "%s", "%d", "%q", "%v", "MISSING", "EXTRA"} { |
| + if strings.Contains(text, bad) { |
| + t.Errorf("%s has %q in it: %s", what, bad, text) |
| + } |
| + } |
| +} |
| + |
| +// TestAlertEmitNoteHasNoFormatErrors renders every message the engine can |
| +// emit, for every rule and kind, named and subjectless, through every |
| +// renderer. A string given arguments it has no verb for, or too few, shows |
| +// up as "%!(EXTRA" or "%!s(MISSING)" in what a person reads. |
| +func TestAlertEmitNoteHasNoFormatErrors(t *testing.T) { |
| + now := alertT0.Add(72 * time.Hour) |
| + sig := newAlertSignals() |
| + for _, rule := range alertCopyRules() { |
| + for _, box := range []string{"box-1", "box-2"} { |
| + sig.add(alertSignal{Key: alertKey{alertScopeFleet, rule, box}, Level: alertRuleLevel(rule), Firing: true, Value: 3, At: now}) |
| + } |
| + } |
| + kinds := []struct { |
| + kind string |
| + closeAs string |
| + flap bool |
| + }{ |
| + {kind: alertKindFire}, {kind: alertKindUpdate}, {kind: alertKindReminder}, |
| + {kind: alertKindClear, closeAs: alertCloseCleared}, |
| + {kind: alertKindClear, closeAs: alertCloseGone}, |
| + {kind: alertKindClear, closeAs: alertCloseCleared, flap: true}, |
| + } |
| + for _, rule := range alertCopyRules() { |
| + for _, k := range kinds { |
| + for _, members := range [][]string{nil, {"box-1"}, {"box-1", "box-2"}} { |
| + name := rule + "/" + k.kind + "/" + k.closeAs + "/" + strings.Join(members, ",") |
| + in := alertIncident{Scope: alertScopeFleet, Rule: rule, Level: alertRuleLevel(rule), Members: members, |
| + OpenedAt: now.Add(-50 * time.Hour), CloseReason: k.closeAs, Flapping: k.flap} |
| + n := alertEmitNote(alertEmit{Incident: in, Kind: k.kind, Joined: members}, sig, now) |
| + for field, text := range map[string]string{"what": n.What, "how_long": n.HowLong, "what_to_do": n.WhatToDo, |
| + "headline": alertHeadline(n), "subject": n.Subject} { |
| + if text == "" { |
| + t.Errorf("%s: %s is empty", name, field) |
| + } |
| + noFormatDebris(t, name+" "+field, text) |
| + } |
| + subject, body := renderEmail(n) |
| + noFormatDebris(t, name+" email subject", subject) |
| + noFormatDebris(t, name+" email body", body) |
| + noFormatDebris(t, name+" slack", string(renderSlack(n))) |
| + noFormatDebris(t, name+" webhook", string(renderWebhook(n))) |
| + } |
| + } |
| + } |
| + // The notes that are not the engine's: a test, a confirmation and the |
| + // suspended notice. |
| + for _, c := range []alertConnector{ |
| + {Kind: alertConnectorSlack, Name: "ops", Confirmed: true}, {Kind: alertConnectorSlack, Name: "ops"}, |
| + {Kind: alertConnectorEmail, Name: "me"}, {Kind: alertConnectorWebhook, Name: "hook"}, |
| + } { |
| + n := alertTestNote(c, "console", now) |
| + noFormatDebris(t, "test note headline", alertHeadline(n)) |
| + noFormatDebris(t, "test note what", n.What) |
| + subject, body := renderEmail(n) |
| + noFormatDebris(t, "test note email subject", subject) |
| + noFormatDebris(t, "test note email", body) |
| + } |
| + for _, key := range slices.Sorted(maps.Keys(alertCopy)) { |
| + switch key { |
| + case "suspended.what", "test.what", "update.what", "reminder.what", "test.failed", "sendErr.status", "url.kind": |
| + default: |
| + if strings.Contains(alertCopy[key], "%") && !strings.HasPrefix(key, "head.") && !strings.HasPrefix(key, "clear.what") && |
| + !strings.HasPrefix(key, "rule.") && key != "what.value" && key != "email.subject" { |
| + t.Errorf("copy %q has a verb no caller fills: %s", key, alertCopy[key]) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// A message with every member snoozed names no subject and no fleet. |
| +func TestAlertEmitNoteWithoutMembersNamesNoSubject(t *testing.T) { |
| + now := alertT0 |
| + for _, rule := range alertCopyRules() { |
| + for _, kind := range []string{alertKindFire, alertKindUpdate, alertKindReminder, alertKindClear} { |
| + in := alertIncident{Scope: alertScopeFleet, Rule: rule, Level: alertRuleLevel(rule), OpenedAt: now.Add(-48 * time.Hour)} |
| + n := alertEmitNote(alertEmit{Incident: in, Kind: kind}, newAlertSignals(), now) |
| + for field, text := range map[string]string{"what": n.What, "headline": alertHeadline(n)} { |
| + if strings.Contains(text, "the fleet") || strings.Contains(text, " on .") || strings.HasSuffix(text, " on ") { |
| + t.Errorf("%s/%s %s names no one but reads %q", rule, kind, field, text) |
| + } |
| + } |
| + } |
| + } |
| + in := alertIncident{Scope: alertScopeFleet, Rule: "disk", Level: "incident", CloseReason: alertCloseCleared} |
| + n := alertEmitNote(alertEmit{Incident: in, Kind: alertKindClear}, newAlertSignals(), now) |
| + if n.What != "Disk nearly full has cleared." || alertHeadline(n) != "Cleared: Disk nearly full" { |
| + t.Errorf("subjectless clear = %q / %q", n.What, alertHeadline(n)) |
| + } |
| + in.Rule = "mem" |
| + n = alertEmitNote(alertEmit{Incident: in, Kind: alertKindFire}, newAlertSignals(), now) |
| + if n.What != "Memory is running high." || alertHeadline(n) != "High memory use" { |
| + t.Errorf("subjectless fire = %q / %q", n.What, alertHeadline(n)) |
| + } |
| +} |
| + |
| +// A reminder says how long the incident has been open, in whole days. |
| +func TestAlertReminderSaysHowLong(t *testing.T) { |
| + now := alertT0.Add(60 * time.Hour) |
| + in := alertIncident{Scope: alertScopeFleet, Rule: "disk", Level: "incident", Members: []string{"box-1"}, OpenedAt: alertT0} |
| + n := alertEmitNote(alertEmit{Incident: in, Kind: alertKindReminder}, newAlertSignals(), now) |
| + if want := "This has been open for 2 days. The disk on box-1 is nearly full."; n.What != want { |
| + t.Errorf("reminder what = %q, want %q", n.What, want) |
| + } |
| + if got := alertHeadline(n); got != "Still open: Disk nearly full on box-1" { |
| + t.Errorf("reminder headline = %q", got) |
| + } |
| + for d, want := range map[time.Duration]string{23 * time.Hour: "23 hours", 24 * time.Hour: "1 day", 49 * time.Hour: "2 days", 10 * time.Minute: "1 hour"} { |
| + if got := alertAge(d); got != want { |
| + t.Errorf("alertAge(%v) = %q, want %q", d, got, want) |
| + } |
| + } |
| +} |
| + |
| +// The footer of a Slack message names the channel; an email's names the |
| +// connector. |
| +func TestAlertUnsubscribeFooterByKind(t *testing.T) { |
| + n := alertNote{Kind: alertKindFire, Rule: "disk", Subject: "box-1", Members: []string{"box-1"}, What: "w", |
| + UnsubscribeURL: "https://example.com/u"} |
| + if got := string(renderSlack(n)); !strings.Contains(got, "Stop alerts to this channel") { |
| + t.Errorf("Slack footer: %s", got) |
| + } |
| + if _, body := renderEmail(n); !strings.Contains(body, "Stop all alerts to this connector: https://example.com/u") { |
| + t.Errorf("email footer: %s", body) |
| + } |
| +} |
| + |
| +// A failed test is told in the approved words, whatever the code stored. |
| +func TestAlertTestFailedWords(t *testing.T) { |
| + for code, want := range map[string]string{ |
| + "refused": "The test did not get through: The address resolves to a private or reserved network.", |
| + "timeout": "The test did not get through: The address did not respond in time.", |
| + "connect": "The test did not get through: The connection could not be made.", |
| + "tls": "The test did not get through: The secure connection failed.", |
| + "status 503": "The test did not get through: The address returned HTTP status 503.", |
| + "invalid": "The test did not get through: The stored URL is no longer valid.", |
| + "needs re-entry": "The test did not get through: The connector needs its URL entered again.", |
| + "suspended": "The test did not get through: The connector is suspended.", |
| + "unconfirmed": "The test did not get through: The connector is not confirmed.", |
| + "no seal key": "The test did not get through: The connector key is missing.", |
| + "seal": "The test did not get through: The connector key could not open the stored address.", |
| + "email": "The test did not get through: email.", |
| + } { |
| + if got := alertTestFailed(code).Error(); got != want { |
| + t.Errorf("alertTestFailed(%q) = %q, want %q", code, got, want) |
| + } |
| + } |
| + for _, err := range []error{errAlertTestUnconfirmed, errAlertTestLimit, errAlertTestReentry} { |
| + if strings.Contains(err.Error(), "alert test:") || errors.Unwrap(err) != nil { |
| + t.Errorf("%q still carries its internal prefix", err) |
| + } |
| + } |
| +} |
| + |
| +// The URL errors name the rule broken, in the approved words, with no |
| +// internal prefix. |
| +func TestValidConnectorURLWords(t *testing.T) { |
| + for _, c := range []struct{ kind, raw, want string }{ |
| + {alertConnectorEmail, "https://x.example/", `Connector kind "email" has no URL.`}, |
| + {alertConnectorWebhook, "http://[::1", "The URL does not parse."}, |
| + {alertConnectorWebhook, "http://example.com/", "The URL must start with https://."}, |
| + {alertConnectorWebhook, "https://u:p@example.com/", "The URL must not include a user name or password."}, |
| + {alertConnectorWebhook, "https://example.com:8443/", "The URL must use port 443."}, |
| + {alertConnectorWebhook, "https:///path", "The URL has no host."}, |
| + {alertConnectorWebhook, "https://10.0.0.1/", "The URL must name a host, not an IP address."}, |
| + {alertConnectorSlack, "https://example.com/x", "A Slack URL must be on hooks.slack.com."}, |
| + } { |
| + _, err := validConnectorURL(c.kind, c.raw) |
| + if err == nil || err.Error() != c.want { |
| + t.Errorf("validConnectorURL(%s, %s) = %v, want %q", c.kind, c.raw, err, c.want) |
| + } |
| + } |
| +} |
diff --git a/internal/console/alertdial.go b/internal/console/alertdial.go| index 5ef99f2d1..9c4eedaf0 100644 |
| --- a/internal/console/alertdial.go |
| +++ b/internal/console/alertdial.go |
| @@ -50,33 +50,33 @@ const ( |
| // the rule broken and never the URL. |
| func validConnectorURL(kind, raw string) (*url.URL, error) { |
| if kind != alertConnectorSlack && kind != alertConnectorWebhook { |
| - return nil, fmt.Errorf("alert connector: %q has no URL", kind) |
| + return nil, fmt.Errorf(alertCopy["url.kind"], kind) |
| } |
| u, err := url.Parse(raw) |
| if err != nil { |
| - return nil, errors.New("alert connector: the URL does not parse") |
| + return nil, errors.New(alertCopy["url.parse"]) |
| } |
| if u.Scheme != "https" || u.Opaque != "" { |
| - return nil, errors.New("alert connector: the URL must start with https://") |
| + return nil, errors.New(alertCopy["url.scheme"]) |
| } |
| if u.User != nil { |
| - return nil, errors.New("alert connector: the URL must not carry a user name or password") |
| + return nil, errors.New(alertCopy["url.userinfo"]) |
| } |
| if p := u.Port(); p != "" && p != "443" { |
| - return nil, errors.New("alert connector: the URL must use port 443") |
| + return nil, errors.New(alertCopy["url.port"]) |
| } |
| host := u.Hostname() |
| if host == "" { |
| - return nil, errors.New("alert connector: the URL has no host") |
| + return nil, errors.New(alertCopy["url.host"]) |
| } |
| if _, err := netip.ParseAddr(host); err == nil || numericLastLabel(host) { |
| - return nil, errors.New("alert connector: the URL must name a host, not an IP address") |
| + return nil, errors.New(alertCopy["url.ip"]) |
| } |
| // ASCII only, then an ASCII comparison: strings.EqualFold would let |
| // "hooks.\u017flack.com" (long s) or a Kelvin sign stand in for the |
| // Slack host. |
| if kind == alertConnectorSlack && (!isASCII(host) || strings.ToLower(host) != slackHookHost) { |
| - return nil, errors.New("alert connector: a Slack URL must be on hooks.slack.com") |
| + return nil, errors.New(alertCopy["url.slack"]) |
| } |
| return u, nil |
| } |
diff --git a/internal/console/alertengine_pass.go b/internal/console/alertengine_pass.go| index cb3e34471..ecc91e74c 100644 |
| --- a/internal/console/alertengine_pass.go |
| +++ b/internal/console/alertengine_pass.go |
| @@ -193,7 +193,7 @@ func (s *Server) alertEngineStep(ctx context.Context, now time.Time) (open []ale |
| // not the job failing: the pass works around it, and seal_key is what |
| // alerts, so the job status carries it as a note (ruling R-21). |
| if e.sealErr != nil { |
| - jobNoteOn(ctx, fmt.Sprintf("%v; Slack and webhook connectors are held", e.sealErr)) |
| + jobNoteOn(ctx, alertCopy["jobnote.seal"]) |
| } |
| reentry := slices.ContainsFunc(conns, func(c alertConnector) bool { return c.NeedsReentry }) |
| sig.add(alertSignal{Key: alertKey{alertScopeFleet, "seal_key", alertSealSubject}, Level: alertRuleLevel("seal_key"), |
| @@ -346,38 +346,73 @@ var alertValued = map[string]bool{ |
| // is the first of them. A clear for a member that went (a close reason of |
| // "box gone") or a flapping incident says "no further reports", not that |
| // it cleared. |
| +// |
| +// With no member left to name (every one snoozed) each line is its ".none" |
| +// variant, which takes no subject (ruling R-31). Every string is rendered |
| +// with exactly the arguments it has verbs for, so no line can print a |
| +// "%!(EXTRA" or "%!s(MISSING)" (TestAlertEmitNoteHasNoFormatErrors). |
| func alertEmitNote(em alertEmit, sig *alertSignals, now time.Time) alertNote { |
| in := em.Incident |
| n := alertNote{Scope: in.Scope, Rule: in.Rule, Level: in.Level, Kind: em.Kind, Subject: alertCopy["subject.none"], |
| Members: slices.Clone(in.Members), At: now} |
| - if len(in.Members) > 0 { |
| + named := len(in.Members) > 0 |
| + if named { |
| n.Subject = in.Members[0] |
| } |
| + title := alertRuleTitle(in.Rule) |
| if em.Kind == alertKindClear { |
| gone := "" |
| if in.CloseReason == alertCloseGone || in.Flapping { |
| gone = ".gone" |
| } |
| - subjects := n.Subject |
| - if len(in.Members) > 1 { |
| - subjects = strings.Join(in.Members, ", ") |
| + if named { |
| + subjects := n.Subject |
| + if len(in.Members) > 1 { |
| + subjects = strings.Join(in.Members, ", ") |
| + } |
| + n.What = fmt.Sprintf(alertCopy["clear.what"+gone], title, subjects) |
| + } else { |
| + n.What = fmt.Sprintf(alertCopy["clear.what"+gone+".none"], title) |
| } |
| - n.What = fmt.Sprintf(alertCopy["clear.what"+gone], in.Rule, subjects) |
| n.HowLong, n.WhatToDo = alertCopy["clear.how_long"+gone], alertCopy["clear.what_to_do"+gone] |
| return n |
| } |
| - n.What = fmt.Sprintf(alertRuleCopy(in.Rule, "what"), n.Subject) |
| - if v, ok := sig.value(alertKey{in.Scope, in.Rule, n.Subject}); ok && alertValued[in.Rule] && len(in.Members) > 0 { |
| + if named { |
| + n.What = fmt.Sprintf(alertRuleCopy(in.Rule, "what"), n.Subject) |
| + } else { |
| + n.What = alertRuleCopy(in.Rule, "what.none") |
| + } |
| + if v, ok := sig.value(alertKey{in.Scope, in.Rule, n.Subject}); ok && alertValued[in.Rule] && named { |
| n.What += " " + fmt.Sprintf(alertCopy["what.value"], alertValueText(in.Rule, v)) |
| } |
| - if em.Kind == alertKindUpdate && len(em.Joined) > 0 { |
| + switch { |
| + case em.Kind == alertKindUpdate && len(em.Joined) > 0: |
| n.What = fmt.Sprintf(alertCopy["update.what"], strings.Join(em.Joined, ", ")) + " " + n.What |
| + case em.Kind == alertKindReminder && !in.OpenedAt.IsZero(): |
| + n.What = fmt.Sprintf(alertCopy["reminder.what"], alertAge(now.Sub(in.OpenedAt))) + " " + n.What |
| } |
| n.HowLong, n.WhatToDo = alertRuleCopy(in.Rule, "how_long"), alertRuleCopy(in.Rule, "what_to_do") |
| return n |
| } |
| -// alertRuleCopy is rule's draft line part, or the default rule's. |
| +// alertAge is how long an incident has been open, in whole days, or in |
| +// hours for the first day (a reminder is never sent before 24 hours, but the |
| +// word must not be wrong for a clock that is a little early). |
| +func alertAge(d time.Duration) string { |
| + if days := int(d / (24 * time.Hour)); days >= 1 { |
| + return alertUnits(days, "day") |
| + } |
| + return alertUnits(max(int(d/time.Hour), 1), "hour") |
| +} |
| + |
| +func alertUnits(n int, unit string) string { |
| + if n == 1 { |
| + return "1 " + unit |
| + } |
| + return strconv.Itoa(n) + " " + unit + "s" |
| +} |
| + |
| +// alertRuleCopy is rule's line part, or the default rule's. |
| func alertRuleCopy(rule, part string) string { |
| if s, ok := alertCopy["rule."+rule+"."+part]; ok { |
| return s |
diff --git a/internal/console/alertengine_pass_test.go b/internal/console/alertengine_pass_test.go| index 09ea00354..698aa27c7 100644 |
| --- a/internal/console/alertengine_pass_test.go |
| +++ b/internal/console/alertengine_pass_test.go |
| @@ -151,7 +151,7 @@ func TestEnginePassFiresSlackForDiskETA(t *testing.T) { |
| if len(reqs) != 1 { |
| t.Fatalf("%d posts at minute 5, want 1", len(reqs)) |
| } |
| - for _, want := range []string{"disk_eta on box-1", "filling up", "40 hours to full"} { |
| + for _, want := range []string{"Disk full forecast on box-1", "filling up", "Reading: 40 hours to full."} { |
| if !strings.Contains(string(reqs[0].body), want) { |
| t.Errorf("the Slack post lacks %q: %s", want, reqs[0].body) |
| } |
| @@ -326,7 +326,7 @@ func TestEnginePassBoxGoneCloses(t *testing.T) { |
| if len(fire) != 1 { |
| t.Fatalf("A got %v, want the fire", fire) |
| } |
| - for _, want := range []string{"What is wrong: The disk on box-1 is nearly full. The last reading was 93%.", "How long you have:", "What to do:"} { |
| + for _, want := range []string{"What is wrong: The disk on box-1 is nearly full. Reading: 93%.", "How long you have:", "What to do:"} { |
| if !strings.Contains(fire[0], want) { |
| t.Errorf("the fire lacks %q:\n%s", want, fire[0]) |
| } |
| @@ -337,7 +337,7 @@ func TestEnginePassBoxGoneCloses(t *testing.T) { |
| } |
| ef.passAt(t, 6*time.Minute) |
| got := ef.mailsTo(opA) |
| - if len(got) != 2 || !strings.Contains(got[1], "No further reports of disk on box-1") { |
| + if len(got) != 2 || !strings.Contains(got[1], "No further reports of Disk nearly full on box-1") { |
| t.Fatalf("A got %v, want the fire then a no further reports clear", got) |
| } |
| if n := ef.count(t, `SELECT COUNT(*) FROM alert_incidents WHERE closed_at IS NOT NULL AND close_reason = 'box gone'`); n != 1 { |
| @@ -417,7 +417,7 @@ func TestEnginePassCutoverNoRefire(t *testing.T) { |
| } |
| publishAlert(t, ef.rel, diskState("box-1", false)) |
| ef.passAt(t, 11*time.Minute) |
| - if got := ef.mailsTo(opA); len(got) != 1 || !strings.Contains(got[0], "disk has cleared on box-1") { |
| + if got := ef.mailsTo(opA); len(got) != 1 || !strings.Contains(got[0], "Disk nearly full has cleared on box-1") { |
| t.Fatalf("A got %v, want one clear", got) |
| } |
| } |
| @@ -480,12 +480,12 @@ func TestEnginePassSealKeyUnreadable(t *testing.T) { |
| t.Fatal(err) |
| } |
| // Not a job failure (ruling R-21): the pass completes and notes it. |
| - if st, _ := jobStatusOf(ef.srv, "StartAlertEngine"); st.State != JobOK || !strings.Contains(st.Note, "alert seal key") { |
| - t.Fatalf("job status %+v, want OK with a note naming the seal key", st) |
| + if st, _ := jobStatusOf(ef.srv, "StartAlertEngine"); st.State != JobOK || st.Note != "Slack and webhook connectors are held." { |
| + t.Fatalf("job status %+v, want OK with the held note", st) |
| } |
| ef.passesTo(t, time.Minute, 5*time.Minute) |
| got := ef.mailsTo(opA) |
| - if len(got) != 1 || !strings.Contains(got[0], "connectors on console cannot be used") || !strings.Contains(got[0], "Email still works") { |
| + if len(got) != 1 || !strings.Contains(got[0], "the console cannot open the key that protects their addresses") || !strings.Contains(got[0], "Email still works") { |
| t.Fatalf("A got %v, want one seal_key fire", got) |
| } |
| if n := len(ef.hook.requests()); n != 0 { |
| @@ -509,7 +509,7 @@ func TestEnginePassNewSealKeyMarksReentry(t *testing.T) { |
| if !ef.connector(t, slack).NeedsReentry { |
| t.Fatal("a Slack connector sealed under the lost key does not need re-entering") |
| } |
| - if got := ef.mailsTo(opA); len(got) != 1 || !strings.Contains(got[0], "connectors on console cannot be used") { |
| + if got := ef.mailsTo(opA); len(got) != 1 || !strings.Contains(got[0], "the console cannot open the key that protects their addresses") { |
| t.Fatalf("A got %v, want one seal_key fire", got) |
| } |
| } |
| @@ -768,7 +768,7 @@ func TestEnginePassHeartbeatPastTheHorizonCloses(t *testing.T) { |
| t.Fatalf("%d box_silent incidents closed as gone, want 1", n) |
| } |
| got := ef.mailsTo(opA) |
| - if len(got) != 2 || !strings.Contains(got[1], "No further reports of box_silent on box-9") { |
| + if len(got) != 2 || !strings.Contains(got[1], "No further reports of Box silent on box-9") { |
| t.Fatalf("A got %v, want the fire then a no further reports clear", got) |
| } |
| } |
diff --git a/internal/console/alertnotify.go b/internal/console/alertnotify.go| index 7a581c3ac..bd370deb6 100644 |
| --- a/internal/console/alertnotify.go |
| +++ b/internal/console/alertnotify.go |
| @@ -29,6 +29,7 @@ import ( |
| "log" |
| "net/http" |
| "net/url" |
| + "strconv" |
| "strings" |
| "sync" |
| "time" |
| @@ -78,8 +79,11 @@ type alertNote struct { |
| } |
| // alertCopy is every word the senders put around a note's own text. These |
| -// are drafts: Task 9's copy review replaces them here, in place. Headlines |
| -// take the rule, then the subject. |
| +// are the approved words of the copy review (docs/superpowers/notes/ |
| +// copy-fleet-alerts-1.md, applied in Task 9): change them there first. |
| +// Headlines take the rule's display name, then the subject. Every string is |
| +// rendered with exactly its own arguments (see alertEmitNote), so a verb |
| +// added to or removed from one is a change to its caller. |
| var alertCopy = map[string]string{ |
| "head.fire": "%[1]s on %[2]s", |
| "head.clear": "Cleared: %[1]s on %[2]s", |
| @@ -88,21 +92,30 @@ var alertCopy = map[string]string{ |
| "head.suspended": "Alerts to %[2]s have stopped", |
| "head.verify": "Check alerts reach %[2]s", |
| + // The same headlines when every member is snoozed and none is left to |
| + // name: the approved line without its subject clause (ruling R-31). |
| + "head.fire.none": "%[1]s", |
| + "head.clear.none": "Cleared: %[1]s", |
| + "head.update.none": "More affected: %[1]s", |
| + "head.reminder.none": "Still open: %[1]s", |
| + |
| "email.subject": "[carlos] %s", |
| "label.what": "What is wrong", |
| - "label.how_long": "How long you have", |
| - "label.what_to_do": "What to do", |
| + "label.how_long": alerting.LabelHowLong, |
| + "label.what_to_do": alerting.LabelWhatToDo, |
| "label.members": "Affected", |
| "link.incident": "See the incident", |
| "link.snooze": "Snooze this alert", |
| "link.confirm": "Confirm alerts here", |
| - "link.unsubscribe": "Stop these alerts", |
| + "link.unsubscribe": "Stop all alerts to this connector", |
| + // A Slack connector is a channel, so its footer says so. |
| + "footer.unsubscribe.slack": "Stop alerts to this channel", |
| "suspended.what": "Sending to %s has failed for 24 hours, so alerts no longer go there.", |
| "suspended.how_long": "Until it is fixed, every alert meant for it is missed.", |
| - "suspended.what_to_do": "Check it on the alerts page, then send a test. A test that gets through turns it back on.", |
| + "suspended.what_to_do": "Check it on the Alerts page, then send a test. A test that gets through turns it back on.", |
| "test.what": "This is a test alert from %s.", |
| "test.how_long": "Nothing is wrong. There is no rush.", |
| @@ -113,77 +126,155 @@ var alertCopy = map[string]string{ |
| // The engine's messages (alertwatch.go, alertEmitNote). Each rule's |
| // what takes the subject; "what.value" follows it when the pass has a |
| - // reading. A rule with no entry of its own uses rule.default. |
| - "subject.none": "the fleet", |
| - "what.value": "The last reading was %s.", |
| - "update.what": "Now also affected: %s.", |
| + // reading. A rule with no entry of its own uses rule.default. A |
| + // ".none" what is the same line for a message with no subject left to |
| + // name (ruling R-31); subject.none stays as the webhook's subject field. |
| + "subject.none": "the fleet", |
| + "what.value": alerting.ReadingFormat, |
| + "update.what": "Now also affected: %s.", |
| + "reminder.what": "This has been open for %s.", |
| + "clear.what": "%[1]s has cleared on %[2]s.", |
| + "clear.what.gone": "No further reports of %[1]s on %[2]s.", |
| + |
| + "clear.what.none": "%[1]s has cleared.", |
| + "clear.what.gone.none": "No further reports of %[1]s.", |
| - "clear.what": "%[1]s has cleared on %[2]s.", |
| - "clear.what.gone": "No further reports of %[1]s on %[2]s.", |
| "clear.how_long": "Nothing is wrong now.", |
| - "clear.how_long.gone": "It may have stopped, or what reported it may have gone.", |
| + "clear.how_long.gone": "No new change has been reported. The incident may have stopped, or what reported it may have gone.", |
| "clear.what_to_do": "Nothing.", |
| - "clear.what_to_do.gone": "Check it went away for a good reason, such as a box being replaced.", |
| + "clear.what_to_do.gone": "Check why the report stopped, including whether the box or service was replaced.", |
| "rule.default.what": "%s needs attention.", |
| - "rule.default.how_long": "It is not known how long you have.", |
| - "rule.default.what_to_do": "Look at it on the fleet page.", |
| + "rule.default.what.none": "Something needs attention.", |
| + "rule.default.how_long": "There is no time estimate.", |
| + "rule.default.what_to_do": "Open the Alerts page and check it.", |
| + "rule.disk_eta.title": alerting.BoxRuleText["disk_eta"].Title, |
| "rule.disk_eta.what": "The disk on %s is filling up.", |
| - "rule.disk_eta.how_long": "At this rate it is full within 3 days.", |
| - "rule.disk_eta.what_to_do": "Free space on it or give it a bigger disk.", |
| + "rule.disk_eta.what.none": "The disk is filling up.", |
| + "rule.disk_eta.how_long": alerting.BoxRuleText["disk_eta"].HowLong, |
| + "rule.disk_eta.what_to_do": alerting.BoxRuleText["disk_eta"].WhatToDo, |
| + "rule.disk.title": alerting.BoxRuleText["disk"].Title, |
| "rule.disk.what": "The disk on %s is nearly full.", |
| - "rule.disk.how_long": "Writes fail once it is full, which may be soon.", |
| - "rule.disk.what_to_do": "Free space on it now, or give it a bigger disk.", |
| + "rule.disk.what.none": "The disk is nearly full.", |
| + "rule.disk.how_long": alerting.BoxRuleText["disk"].HowLong, |
| + "rule.disk.what_to_do": alerting.BoxRuleText["disk"].WhatToDo, |
| + "rule.mem.title": alerting.BoxRuleText["mem"].Title, |
| "rule.mem.what": "Memory on %s is running high.", |
| - "rule.mem.how_long": "Processes may be stopped if it keeps climbing.", |
| - "rule.mem.what_to_do": "Check what is using memory on it.", |
| + "rule.mem.what.none": "Memory is running high.", |
| + "rule.mem.how_long": alerting.BoxRuleText["mem"].HowLong, |
| + "rule.mem.what_to_do": alerting.BoxRuleText["mem"].WhatToDo, |
| + "rule.cpu.title": alerting.BoxRuleText["cpu"].Title, |
| "rule.cpu.what": "The processor on %s has been busy for a while.", |
| - "rule.cpu.how_long": "Requests are slower while it stays this busy.", |
| - "rule.cpu.what_to_do": "Check what is using it, or move apps to another box.", |
| + "rule.cpu.what.none": "The processor has been busy for a while.", |
| + "rule.cpu.how_long": alerting.BoxRuleText["cpu"].HowLong, |
| + "rule.cpu.what_to_do": alerting.BoxRuleText["cpu"].WhatToDo, |
| + "rule.lag.title": alerting.BoxRuleText["lag"].Title, |
| "rule.lag.what": "Mail relay on %s is falling behind.", |
| - "rule.lag.how_long": "Messages are late until it catches up.", |
| - "rule.lag.what_to_do": "Check the relay on that box.", |
| + "rule.lag.what.none": "Mail relay is falling behind.", |
| + "rule.lag.how_long": alerting.BoxRuleText["lag"].HowLong, |
| + "rule.lag.what_to_do": alerting.BoxRuleText["lag"].WhatToDo, |
| + "rule.geoip.title": alerting.BoxRuleText["geoip"].Title, |
| "rule.geoip.what": "The location database on %s needs attention.", |
| - "rule.geoip.how_long": "Location answers go stale, then stop.", |
| - "rule.geoip.what_to_do": "Check the location data feed.", |
| + "rule.geoip.what.none": "The location database needs attention.", |
| + "rule.geoip.how_long": alerting.BoxRuleText["geoip"].HowLong, |
| + "rule.geoip.what_to_do": alerting.BoxRuleText["geoip"].WhatToDo, |
| - "rule.engine_silent.what": "The alert engine on %s stopped writing its beat.", |
| - "rule.engine_silent.how_long": "Until it runs again, no alerts are sent.", |
| - "rule.engine_silent.what_to_do": "Check the console on that box.", |
| + "rule.engine_silent.title": alerting.BoxRuleText["engine_silent"].Title, |
| + "rule.engine_silent.what": "The alert engine on %s has gone quiet.", |
| + "rule.engine_silent.what.none": "The alert engine has gone quiet.", |
| + "rule.engine_silent.how_long": alerting.BoxRuleText["engine_silent"].HowLong, |
| + "rule.engine_silent.what_to_do": alerting.BoxRuleText["engine_silent"].WhatToDo, |
| + "rule.box_silent.title": "Box silent", |
| "rule.box_silent.what": "%s has stopped reporting.", |
| + "rule.box_silent.what.none": "A box has stopped reporting.", |
| "rule.box_silent.how_long": "If it is down, every app on it is down too.", |
| - "rule.box_silent.what_to_do": "Check the box is running and can reach the bucket.", |
| + "rule.box_silent.what_to_do": "Check that the box is running and can reach the bucket.", |
| + "rule.service_down.title": "Service down", |
| "rule.service_down.what": "The %s service is down.", |
| + "rule.service_down.what.none": "A service is down.", |
| "rule.service_down.how_long": "What relies on it fails until it is back.", |
| - "rule.service_down.what_to_do": "Check the service on the fleet page.", |
| + "rule.service_down.what_to_do": "Open the fleet page, find the service and read its status.", |
| + "rule.cert_expiring.title": "Certificate expiring", |
| "rule.cert_expiring.what": "The certificate for %s expires soon.", |
| - "rule.cert_expiring.how_long": "Visitors see a warning once it expires.", |
| - "rule.cert_expiring.what_to_do": "Check why it has not renewed.", |
| + "rule.cert_expiring.what.none": "A certificate expires soon.", |
| + "rule.cert_expiring.how_long": "Visitors see a browser warning once it expires. The days left are in the last reading.", |
| + "rule.cert_expiring.what_to_do": "Find out why it has not renewed. Start with the box that serves the host.", |
| + "rule.alert_delivery_broken.title": "Alert delivery broken", |
| "rule.alert_delivery_broken.what": "%s has nowhere to send its own alerts.", |
| - "rule.alert_delivery_broken.how_long": "If the console stops, nobody hears from that box.", |
| - "rule.alert_delivery_broken.what_to_do": "Set CARLOS_ALERT_WEBHOOK or CARLOS_ALERT_SNS_TOPIC_ARN in its host.env.", |
| + "rule.alert_delivery_broken.what.none": "A box has nowhere to send its own alerts.", |
| + "rule.alert_delivery_broken.how_long": "That box's own alerts reach nobody. If the console stops as well, nobody hears about it.", |
| + "rule.alert_delivery_broken.what_to_do": "Set CARLOS_ALERT_WEBHOOK or CARLOS_ALERT_SNS_TOPIC_ARN in its host.env, then restart the agent.", |
| - "rule.seal_key.what": "The key that opens Slack and webhook connectors on %s cannot be used.", |
| + "rule.seal_key.title": "Connector key", |
| + "rule.seal_key.what": "Slack and webhook connectors cannot send because the %s cannot open the key that protects their addresses.", |
| + "rule.seal_key.what.none": "Slack and webhook connectors cannot send because the key that protects their addresses cannot be opened.", |
| "rule.seal_key.how_long": "Slack and webhook alerts are not sent until it is fixed. Email still works.", |
| - "rule.seal_key.what_to_do": "Restore the key file beside console.db, or enter each Slack and webhook connector again.", |
| + "rule.seal_key.what_to_do": "Put alert-connectors.key back beside console.db and restart the console. If it is lost, enter each Slack and webhook connector again on the Alerts page.", |
| + |
| + // The job-status note while the seal key cannot be used. The key's own |
| + // error is a diagnostic and stays in the journal (ruling R-31). |
| + "jobnote.seal": "Slack and webhook connectors are held.", |
| + |
| + // A failed test send: the sentence, then why in the reason's words. |
| + "test.failed": "The test did not get through: %s.", |
| + "errAlertTestUnconfirmed": "This email address is not confirmed yet. Open the confirm link before testing it.", |
| + "errAlertTestLimit": "At most 10 tests per hour for one connector. Try again later.", |
| + "errAlertTestReentry": "Enter this connector's URL again before testing it.", |
| + |
| + "sendErr.refused": "The address resolves to a private or reserved network.", |
| + "sendErr.timeout": "The address did not respond in time.", |
| + "sendErr.connect": "The connection could not be made.", |
| + "sendErr.tls": "The secure connection failed.", |
| + "sendErr.status": "The address returned HTTP status %d.", |
| + "sendErr.invalid": "The stored URL is no longer valid.", |
| + |
| + "hold.needs_reentry": "The connector needs its URL entered again.", |
| + "hold.suspended": "The connector is suspended.", |
| + "hold.unconfirmed": "The connector is not confirmed.", |
| + "hold.no_seal_key": "The connector key is missing.", |
| + "hold.seal": "The connector key could not open the stored address.", |
| + |
| + "url.kind": "Connector kind %q has no URL.", |
| + "url.parse": "The URL does not parse.", |
| + "url.scheme": "The URL must start with https://.", |
| + "url.userinfo": "The URL must not include a user name or password.", |
| + "url.port": "The URL must use port 443.", |
| + "url.host": "The URL has no host.", |
| + "url.ip": "The URL must name a host, not an IP address.", |
| + "url.slack": "A Slack URL must be on hooks.slack.com.", |
| } |
| +// alertRuleTitle is rule's display name. A rule this console has no name for |
| +// (a newer box's) keeps its ID, which is honest and never empty. |
| +func alertRuleTitle(rule string) string { |
| + if t, ok := alertCopy["rule."+rule+".title"]; ok { |
| + return t |
| + } |
| + return rule |
| +} |
| + |
| +// alertHeadline is n's one line. A message with no member to name uses the |
| +// ".none" variant of its headline, which takes the rule alone; every other |
| +// headline takes the rule and the subject. |
| func alertHeadline(n alertNote) string { |
| - f, ok := alertCopy["head."+n.Kind] |
| - if !ok { |
| - f = alertCopy["head.fire"] |
| + key := "head." + n.Kind |
| + if _, ok := alertCopy[key]; !ok { |
| + key = "head.fire" |
| } |
| - return fmt.Sprintf(f, n.Rule, n.Subject) |
| + if none, ok := alertCopy[key+".none"]; ok && len(n.Members) == 0 { |
| + return fmt.Sprintf(none, alertRuleTitle(n.Rule)) |
| + } |
| + return fmt.Sprintf(alertCopy[key], alertRuleTitle(n.Rule), n.Subject) |
| } |
| // alertSlackEscaper escapes what Slack reads as markup: without it a |
| @@ -209,7 +300,11 @@ func renderSlack(n alertNote) []byte { |
| } |
| var links []string |
| for _, l := range alertNoteLinks(n) { |
| - links = append(links, "<"+l.url+"|"+esc(alertCopy[l.label])+">") |
| + label := l.label |
| + if label == "link.unsubscribe" { |
| + label = "footer.unsubscribe.slack" |
| + } |
| + links = append(links, "<"+l.url+"|"+esc(alertCopy[label])+">") |
| } |
| type text struct { |
| Type string `json:"type"` |
| @@ -706,11 +801,44 @@ func (s *Server) alertScopeOwners(scope string) ([]string, error) { |
| // ---- test sends -------------------------------------------------------------- |
| var ( |
| - errAlertTestUnconfirmed = errors.New("alert test: an email connector is tested only once its address is confirmed") |
| - errAlertTestLimit = errors.New("alert test: at most 10 tests a connector an hour") |
| - errAlertTestReentry = errors.New("alert test: the connector needs re-entering") |
| + errAlertTestUnconfirmed = errors.New(alertCopy["errAlertTestUnconfirmed"]) |
| + errAlertTestLimit = errors.New(alertCopy["errAlertTestLimit"]) |
| + errAlertTestReentry = errors.New(alertCopy["errAlertTestReentry"]) |
| ) |
| +// alertTestFailed is the error of a test that did not get through. why is a |
| +// send category (sendErr) or a hold reason (alertWithheld, alertTarget); the |
| +// stored values stay short codes, and the words are made here. |
| +func alertTestFailed(why string) error { |
| + return fmt.Errorf(alertCopy["test.failed"], strings.TrimSuffix(alertWhyText(why), ".")) |
| +} |
| + |
| +// alertHoldKeys maps a hold reason (alertWithheld, alertTarget) to its copy. |
| +var alertHoldKeys = map[string]string{ |
| + "needs re-entry": "hold.needs_reentry", |
| + "suspended": "hold.suspended", |
| + "unconfirmed": "hold.unconfirmed", |
| + "no seal key": "hold.no_seal_key", |
| + "seal": "hold.seal", |
| +} |
| + |
| +// alertWhyText is the approved words for a send category or hold reason. A |
| +// code with none (a sender the deck has no row for) is shown as it is. |
| +func alertWhyText(code string) string { |
| + if status, ok := strings.CutPrefix(code, "status "); ok { |
| + if n, err := strconv.Atoi(status); err == nil { |
| + return fmt.Sprintf(alertCopy["sendErr.status"], n) |
| + } |
| + } |
| + if key, ok := alertHoldKeys[code]; ok { |
| + return alertCopy[key] |
| + } |
| + if s, ok := alertCopy["sendErr."+code]; ok { |
| + return s |
| + } |
| + return code |
| +} |
| + |
| // sendTestAlert sends connector id in scope a test now, outside the |
| // outbox. An unconfirmed email is refused: its confirmation is its only |
| // message (spec §5.3). An unconfirmed Slack's test carries a one-use |
| @@ -747,7 +875,7 @@ func (s *Server) sendTestAlert(ctx context.Context, scope string, id int64) erro |
| return err |
| } |
| if hold != "" { |
| - return fmt.Errorf("alert test: %s", hold) |
| + return alertTestFailed(hold) |
| } |
| n := alertTestNote(c, s.consoleName(), now) |
| if n.DeliveryID, err = newAlertDeliveryID(); err != nil { |
| @@ -761,7 +889,7 @@ func (s *Server) sendTestAlert(ctx context.Context, scope string, id int64) erro |
| return err |
| } |
| if cat != "" { |
| - return fmt.Errorf("alert test: %s", cat) |
| + return alertTestFailed(string(cat)) |
| } |
| if c.Kind == alertConnectorWebhook && !c.Confirmed { |
| if err := s.store.ConfirmAlertConnector(c.ID); err != nil { |
diff --git a/internal/console/jobs.go b/internal/console/jobs.go| index 0b6bf18e3..d7b753c07 100644 |
| --- a/internal/console/jobs.go |
| +++ b/internal/console/jobs.go |
| @@ -298,7 +298,7 @@ func (j *jobRegistry) Snapshot() []JobStatus { |
| // OPS-102 and OPS-149 to OPS-153), keyed by its claim worker name. |
| var jobLabels = map[string]string{ |
| "StartHostReports": "Host reports", |
| - "StartAlertEngine": "Alert engine", |
| + "StartAlertEngine": "Alert checks", |
| "StartGeoipJobs": "Location data", |
| "PublishMovesConsole": "Move records", |
| "StartClaimRecoveryPass": "Claim recovery", |
diff --git a/internal/console/jobs_test.go b/internal/console/jobs_test.go| index 4d957877f..82bf5a469 100644 |
| --- a/internal/console/jobs_test.go |
| +++ b/internal/console/jobs_test.go |
| @@ -341,7 +341,7 @@ func TestJobNoteIsTheFirstLineCappedAndEscaped(t *testing.T) { |
| for i, r := range rows { |
| labels[i] = r.Label |
| } |
| - if want := []string{"Alert engine", "Architecture", "Host reports", "Virus scan"}; !slices.Equal(labels, want) { |
| + if want := []string{"Alert checks", "Architecture", "Host reports", "Virus scan"}; !slices.Equal(labels, want) { |
| t.Fatalf("labels = %q, want %q (sorted by label)", labels, want) |
| } |
| if rows[0].Note != "<b>disk</b> full" { |
diff --git a/internal/host/alertsink.go b/internal/host/alertsink.go| index 95bd4f682..2aac38bb1 100644 |
| --- a/internal/host/alertsink.go |
| +++ b/internal/host/alertsink.go |
| @@ -346,6 +346,12 @@ func buildAlertSinks(ctx context.Context, getenv func(string) string, counts *al |
| // time, and nothing else, so no URL, ARN or credential can reach the |
| // message. The subject is ASCII with no newline and under 100 characters, |
| // which is what SNS accepts. |
| +// |
| +// The same text goes to Slack when the webhook is a Slack URL, so it names |
| +// no channel. A fire carries the rule's "how long you have" and "what to do" |
| +// lines, the console's own (alerting.BoxRuleText), and the footer names the |
| +// rule by its display name, not its ID (fleet alerts copy review, ruling |
| +// R-31). |
| func formatAlert(rules metrics.Rules, at time.Time, ev metrics.Event) (subject, body string) { |
| subjectLine := alertSubject(ev) |
| var b strings.Builder |
| @@ -362,14 +368,31 @@ func formatAlert(rules metrics.Rules, at time.Time, ev metrics.Event) (subject, |
| fmt.Fprintf(&b, "it stayed above that level for %d minutes before this alert was raised.\n", int(d.Minutes())) |
| } |
| } |
| + if ev.Firing && ev.Rule == "disk_eta" { |
| + // How long you have is "the last reading", so the message has to |
| + // carry it. |
| + fmt.Fprintf(&b, alerting.ReadingFormat+"\n", fmt.Sprintf("%.0f hours to full", ev.Value)) |
| + } |
| fmt.Fprintf(&b, "this was reported at %s.\n", at.UTC().Format("2006-01-02 15:04 MST")) |
| if ev.Firing { |
| - b.WriteString("you will get another email when it clears.\n") |
| + if t, ok := alerting.BoxRuleText[ev.Rule]; ok { |
| + fmt.Fprintf(&b, "%s: %s\n%s: %s\n", alerting.LabelHowLong, t.HowLong, alerting.LabelWhatToDo, t.WhatToDo) |
| + } |
| + b.WriteString("it will be reported again when it clears.\n") |
| } |
| - fmt.Fprintf(&b, "\nrule: %s\nbox: %s\n", ev.Rule, ev.Box) |
| + fmt.Fprintf(&b, "\nrule: %s\nbox: %s\n", alertRuleName(ev.Rule), ev.Box) |
| return subjectLine, b.String() |
| } |
| +// alertRuleName is a rule's display name, or its ID for a rule this agent |
| +// has no name for. |
| +func alertRuleName(rule string) string { |
| + if t, ok := alerting.BoxRuleText[rule]; ok { |
| + return t.Title |
| + } |
| + return rule |
| +} |
| + |
| func alertSubject(ev metrics.Event) string { |
| if ev.Firing { |
| return "carlos alert: " + alertSentence(ev) |
| @@ -391,8 +414,12 @@ func alertSentence(ev metrics.Event) string { |
| return fmt.Sprintf("mail relay lag is %d seconds on %s", int64(ev.Value), ev.Box) |
| case "geoip": |
| return geoipAlertSentence(ev) |
| + case "disk_eta": |
| + return fmt.Sprintf("the disk on %s is filling up", ev.Box) |
| + case "engine_silent": |
| + return fmt.Sprintf("the alert engine on %s has gone quiet", ev.Box) |
| default: |
| - return fmt.Sprintf("%s is firing on %s", ev.Rule, ev.Box) |
| + return fmt.Sprintf("%s is firing on %s", alertRuleName(ev.Rule), ev.Box) |
| } |
| } |
| @@ -407,9 +434,13 @@ func alertClearSentence(ev metrics.Event) string { |
| case "lag": |
| return fmt.Sprintf("mail relay lag is back to %d seconds on %s", int64(ev.Value), ev.Box) |
| case "geoip": |
| - return fmt.Sprintf("geoip on %s is back to normal", ev.Box) |
| + return fmt.Sprintf("location database on %s is back to normal", ev.Box) |
| + case "disk_eta": |
| + return fmt.Sprintf("the disk on %s is no longer filling up", ev.Box) |
| + case "engine_silent": |
| + return fmt.Sprintf("the alert engine on %s is running again", ev.Box) |
| default: |
| - return fmt.Sprintf("%s has cleared on %s", ev.Rule, ev.Box) |
| + return fmt.Sprintf("%s has cleared on %s", alertRuleName(ev.Rule), ev.Box) |
| } |
| } |
| @@ -441,17 +472,17 @@ func alertSustain(r metrics.Rules, rule string) time.Duration { |
| func geoipAlertSentence(ev metrics.Event) string { |
| switch int(ev.Value) { |
| case 1: |
| - return fmt.Sprintf("the geoip database on %s is more than 45 days old", ev.Box) |
| + return fmt.Sprintf("the location database on %s is more than 45 days old", ev.Box) |
| case 2: |
| - return fmt.Sprintf("the GeoLite2 database on %s expires within 9 days", ev.Box) |
| + return fmt.Sprintf("the Location database on %s expires within 9 days", ev.Box) |
| case 3: |
| - return fmt.Sprintf("the geoip database on %s has expired and was removed", ev.Box) |
| + return fmt.Sprintf("the location database on %s has expired and was removed", ev.Box) |
| case 4: |
| - return fmt.Sprintf("%s has not loaded the published geoip database for over an hour", ev.Box) |
| + return fmt.Sprintf("%s has not loaded the published location database for over an hour", ev.Box) |
| case 5: |
| - return fmt.Sprintf("the geoip feed has failed 7 times in a row (seen from %s)", ev.Box) |
| + return fmt.Sprintf("the location data feed has failed 7 times in a row (seen from %s)", ev.Box) |
| } |
| - return fmt.Sprintf("geoip needs attention on %s", ev.Box) |
| + return fmt.Sprintf("the location database needs attention on %s", ev.Box) |
| } |
| // pct renders a 0..1 fraction as a whole percent. |
diff --git a/internal/host/alertsink_geoip_test.go b/internal/host/alertsink_geoip_test.go| index 0025e919b..cfbf94c7f 100644 |
| --- a/internal/host/alertsink_geoip_test.go |
| +++ b/internal/host/alertsink_geoip_test.go |
| @@ -11,13 +11,13 @@ import ( |
| func TestGeoIPAlertSentences(t *testing.T) { |
| for code, want := range map[float64]string{ |
| 1: "more than 45 days old", 2: "expires within 9 days", 3: "has expired and was removed", |
| - 4: "has not loaded the published geoip database", 5: "has failed 7 times in a row", |
| + 4: "has not loaded the published location database", 5: "has failed 7 times in a row", |
| } { |
| if got := alertSentence(metrics.Event{Rule: "geoip", Firing: true, Value: code, Box: "b"}); !strings.Contains(got, want) { |
| t.Errorf("code %v = %q, want %q", code, got, want) |
| } |
| } |
| - if got := alertClearSentence(metrics.Event{Rule: "geoip", Box: "b"}); got != "geoip on b is back to normal" { |
| + if got := alertClearSentence(metrics.Event{Rule: "geoip", Box: "b"}); got != "location database on b is back to normal" { |
| t.Errorf("clear = %q", got) |
| } |
| if geoIPAlertReading(time.Now()) != nil { |
diff --git a/internal/host/alertsink_test.go b/internal/host/alertsink_test.go| index ec0c95dc7..1a14b47da 100644 |
| --- a/internal/host/alertsink_test.go |
| +++ b/internal/host/alertsink_test.go |
| @@ -366,8 +366,8 @@ func TestAlertDeliveryProbeEngineBeat(t *testing.T) { |
| {"unset", nil, 1000, "", ""}, |
| {"unset as root", nil, 0, "", ""}, |
| {"armed", beat, 1000, "on", ""}, |
| - {"root", beat, 0, "off", "agent runs as root"}, |
| - {"relative path", map[string]string{"CARLOS_ALERT_ENGINE_BEAT": "alert-engine.beat"}, 1000, "off", "path is not absolute"}, |
| + {"root", beat, 0, "off", "agent runs as root, so the backstop is off"}, |
| + {"relative path", map[string]string{"CARLOS_ALERT_ENGINE_BEAT": "alert-engine.beat"}, 1000, "off", "CARLOS_ALERT_ENGINE_BEAT is not an absolute path"}, |
| } |
| for _, c := range cases { |
| got := alertDeliveryProbe(env(c.env), &alertCounters{}, c.euid) |
| @@ -557,3 +557,71 @@ func deref(s *string) string { |
| } |
| return *s |
| } |
| + |
| +// The box's own text is the approved copy (fleet alerts copy review, ruling |
| +// R-31): the rule is named by its display name, a fire carries the rule's |
| +// "how long you have" and "what to do" lines, and no line names a channel. |
| +func TestFormatAlertApprovedCopy(t *testing.T) { |
| + rules := metrics.DefaultRules() |
| + fire := func(rule string, v float64) (string, string) { |
| + return formatAlert(rules, sinkTestAt, metrics.Event{Rule: rule, Firing: true, Value: v, Box: "box-a"}) |
| + } |
| + clear := func(rule string) (string, string) { |
| + return formatAlert(rules, sinkTestAt, metrics.Event{Rule: rule, Firing: false, Value: 0.3, Box: "box-a"}) |
| + } |
| + |
| + subject, body := fire("disk_eta", 52) |
| + if want := "carlos alert: the disk on box-a is filling up"; subject != want { |
| + t.Errorf("disk_eta subject = %q, want %q", subject, want) |
| + } |
| + for _, want := range []string{ |
| + "Reading: 52 hours to full.\n", |
| + "How long you have: The last reading gives the time to full.\n", |
| + "What to do: Free space on it, or give it a bigger disk before it is full.\n", |
| + "it will be reported again when it clears.\n", |
| + "\nrule: Disk full forecast\nbox: box-a\n", |
| + } { |
| + if !strings.Contains(body, want) { |
| + t.Errorf("disk_eta body lacks %q:\n%s", want, body) |
| + } |
| + } |
| + subject, body = fire("engine_silent", 0) |
| + if want := "carlos alert: the alert engine on box-a has gone quiet"; subject != want { |
| + t.Errorf("engine_silent subject = %q, want %q", subject, want) |
| + } |
| + for _, want := range []string{ |
| + "How long you have: Until it runs again, the console sends no alerts. Each box still sends its own.\n", |
| + "What to do: Check the console service on that box and restart it if it is stuck.\n", |
| + "\nrule: Alert engine silent\nbox: box-a\n", |
| + } { |
| + if !strings.Contains(body, want) { |
| + t.Errorf("engine_silent body lacks %q:\n%s", want, body) |
| + } |
| + } |
| + if subject, _ = clear("disk_eta"); subject != "carlos alert cleared: the disk on box-a is no longer filling up" { |
| + t.Errorf("disk_eta clear subject = %q", subject) |
| + } |
| + if subject, _ = clear("engine_silent"); subject != "carlos alert cleared: the alert engine on box-a is running again" { |
| + t.Errorf("engine_silent clear subject = %q", subject) |
| + } |
| + // A rule this agent has no name for keeps its ID, and has no lines. |
| + if subject, body = fire("newrule", 0.5); subject != "carlos alert: newrule is firing on box-a" || strings.Contains(body, "How long") { |
| + t.Errorf("unknown rule: %q\n%s", subject, body) |
| + } |
| + |
| + for _, rule := range []string{"disk", "mem", "cpu", "lag", "geoip", "disk_eta", "engine_silent", "newrule"} { |
| + for _, firing := range []bool{true, false} { |
| + for _, v := range []float64{0, 1, 2, 3, 4, 5, 6, 0.5} { |
| + subject, body := formatAlert(rules, sinkTestAt, metrics.Event{Rule: rule, Firing: firing, Value: v, Box: "box-a"}) |
| + for _, s := range []string{subject, body} { |
| + if strings.Contains(s, "%!") || strings.Contains(strings.ToLower(s), "email") || strings.Contains(strings.ToLower(s), "slack") { |
| + t.Errorf("%s firing=%v value=%v: debris, or a channel promised: %q", rule, firing, v, s) |
| + } |
| + if rule != "newrule" && strings.Contains(s, "rule: "+rule+"\n") { |
| + t.Errorf("%s: the footer names the raw rule ID: %q", rule, s) |
| + } |
| + } |
| + } |
| + } |
| + } |
| +} |
diff --git a/internal/host/enginebeat.go b/internal/host/enginebeat.go| index 9c73e3660..6ed821860 100644 |
| --- a/internal/host/enginebeat.go |
| +++ b/internal/host/enginebeat.go |
| @@ -71,7 +71,7 @@ var engineBeatLstat = os.Lstat |
| // engineBeatArmed is whether this agent watches the engine beat, and the |
| // path it watches. Unset (or blank) is unarmed with no reason: the box does |
| // not run a console. euid 0 is refused before anything else is done with |
| -// the path, with why "agent runs as root". A relative path is refused too: |
| +// the path, with why "agent runs as root, so the backstop is off". A relative path is refused too: |
| // the agent's working directory is not where the console writes. |
| func engineBeatArmed(getenv func(string) string, euid int) (path string, armed bool, why string) { |
| raw := strings.TrimSpace(getenv(engineBeatEnv)) |
| @@ -79,10 +79,10 @@ func engineBeatArmed(getenv func(string) string, euid int) (path string, armed b |
| return "", false, "" |
| } |
| if euid == 0 { |
| - return "", false, "agent runs as root" |
| + return "", false, "agent runs as root, so the backstop is off" |
| } |
| if !filepath.IsAbs(raw) { |
| - return "", false, "path is not absolute" |
| + return "", false, engineBeatEnv + " is not an absolute path" |
| } |
| return filepath.Clean(raw), true, "" |
| } |
diff --git a/internal/host/enginebeat_linux_test.go b/internal/host/enginebeat_linux_test.go| index f5420a90d..e528461fe 100644 |
| --- a/internal/host/enginebeat_linux_test.go |
| +++ b/internal/host/enginebeat_linux_test.go |
| @@ -122,8 +122,8 @@ func TestEngineBeatRootRefusedBeforeLookup(t *testing.T) { |
| writeBeat(t, path, beatT0.Add(-time.Hour)) |
| p, armed, why := engineBeatArmed(beatEnv(path), 0) |
| - if armed || p != "" || why != "agent runs as root" { |
| - t.Fatalf("root: engineBeatArmed = %q, %v, %q; want unarmed, no path, agent runs as root", p, armed, why) |
| + if armed || p != "" || why != "agent runs as root, so the backstop is off" { |
| + t.Fatalf("root: engineBeatArmed = %q, %v, %q; want unarmed, no path, agent runs as root, so the backstop is off", p, armed, why) |
| } |
| w := newEngineBeatWatch(beatEnv(path), 0, beatT0) |
| if w != nil { |
| @@ -159,8 +159,8 @@ func TestEngineBeatArmed(t *testing.T) { |
| {"unset as root", "", 0, "", false, ""}, |
| {"armed", "/data/console/alert-engine.beat", 1000, "/data/console/alert-engine.beat", true, ""}, |
| {"cleaned", "/data/console/../console/alert-engine.beat", 1000, "/data/console/alert-engine.beat", true, ""}, |
| - {"root", "/data/console/alert-engine.beat", 0, "", false, "agent runs as root"}, |
| - {"relative", "console/alert-engine.beat", 1000, "", false, "path is not absolute"}, |
| + {"root", "/data/console/alert-engine.beat", 0, "", false, "agent runs as root, so the backstop is off"}, |
| + {"relative", "console/alert-engine.beat", 1000, "", false, "CARLOS_ALERT_ENGINE_BEAT is not an absolute path"}, |
| } |
| for _, c := range cases { |
| p, armed, why := engineBeatArmed(beatEnv(c.env), c.euid) |
diff --git a/statuslive_test.go b/statuslive_test.go| index 62304685c..82b6cb540 100644 |
| --- a/statuslive_test.go |
| +++ b/statuslive_test.go |
| @@ -114,7 +114,7 @@ func TestPrintLiveBoxesSaysWhetherAlertsAreDelivered(t *testing.T) { |
| {GeneratedAt: now, Label: "d-old", Metrics: &status.BoxMetrics{Tunnel: "connected"}}, |
| {GeneratedAt: now, Label: "e-unavail", Metrics: &status.BoxMetrics{Alerts: &status.AlertDelivery{SNSUnavailable: true, SNSFailures: 1}}}, |
| {GeneratedAt: now, Label: "f-hook-unavail", Metrics: &status.BoxMetrics{Alerts: &status.AlertDelivery{Webhook: true, SNSUnavailable: true}}}, |
| - {GeneratedAt: now, Label: "g-beat-root", Metrics: &status.BoxMetrics{Alerts: &status.AlertDelivery{SNS: true, EngineBeat: "off", EngineBeatWhy: "agent runs as root"}}}, |
| + {GeneratedAt: now, Label: "g-beat-root", Metrics: &status.BoxMetrics{Alerts: &status.AlertDelivery{SNS: true, EngineBeat: "off", EngineBeatWhy: "agent runs as root, so the backstop is off"}}}, |
| {GeneratedAt: now, Label: "h-beat-on", Metrics: &status.BoxMetrics{Alerts: &status.AlertDelivery{Webhook: true, EngineBeat: "on"}}}, |
| }, now) |
| want := map[string]string{ |
| @@ -126,7 +126,7 @@ func TestPrintLiveBoxesSaysWhetherAlertsAreDelivered(t *testing.T) { |
| "f-hook-unavail": "alerts=webhook (sns-unavailable)", |
| // The engine-silent backstop (fleet alerts spec §8) beside it, only |
| // on a box where it is configured; b-sns above shows none. |
| - "g-beat-root": "alerts=sns engine-beat=off (agent runs as root)\n", |
| + "g-beat-root": "alerts=sns engine-beat=off (agent runs as root, so the backstop is off)\n", |
| "h-beat-on": "alerts=webhook engine-beat=on\n", |
| } |
| lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") |