mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
"Health is unhealthy" sends the owner to the host to find out why, and
on a default install Patrol cannot do better: it reads no logs and no
probe output. On the fault set a default agent could only call the dead
web worker's cause unconfirmed.
Docker's inspect result, which the agent already reads every cycle,
holds the last probe results. The agent now classifies the newest
failure on the host and reports a reason code from a closed set:
connection refused, timed out, name not resolved, an HTTP status, TLS
error, command not found, or other. It sends the exit code and the
failing streak with it and never the output, which can hold credentials
like the health-check command the agent already keeps on the host. The
server maps any reason outside the set to "other", so the field cannot
carry text.
The reason is named only when the output states it in the words curl,
wget and the engine print. Anything else is "other" with the exit code,
because a wrong reason sends the owner the wrong way.
It shows in the alert ("health is unhealthy (health check: connection
refused)"), in the container's Health check row and State tooltip, and
in what Patrol reads: the scoped seed table, the unhealthy line of a
full run and the container payload. The start of the alert message is
unchanged and the streak stays out of it, so the text is stable while
the reason is.
An agent that does not report the reason leaves everything as it was.
Scored on the fault set before merging, both profiles, one run per
cell. On a default agent Patrol's finding on the dead web worker now
names the cause ("health check sustained failure (connection refused)").
The rubric accepted only the spaced spelling, and the conclusion wrote
the code and "refusing connections", so the decisive terms gain those
spellings and the run regrades to "Handed over". That is a rubric
changed after seeing a transcript. Default agent 4 of 7, commands on 6
of 7, no cell worse except the stopped service on a default agent,
whose conclusion did not say the exit was clean.
1725 lines
61 KiB
Go
1725 lines
61 KiB
Go
package alerts
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/alerts/reducer"
|
|
alertspecs "github.com/rcourtman/pulse-go-rewrite/internal/alerts/specs"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type dockerRestartRecord struct {
|
|
count int
|
|
lastCount int
|
|
times []time.Time // Track restart times for loop detection
|
|
lastChecked time.Time
|
|
}
|
|
|
|
// dockerInstanceName returns the logical instance name used for Docker alerts.
|
|
func dockerInstanceName(host models.DockerHost) string {
|
|
name := strings.TrimSpace(host.DisplayName)
|
|
if name == "" {
|
|
name = strings.TrimSpace(host.Hostname)
|
|
}
|
|
if name == "" {
|
|
return "Docker"
|
|
}
|
|
return fmt.Sprintf("Docker:%s", name)
|
|
}
|
|
|
|
// dockerContainerDisplayName normalizes the container name for alert readability.
|
|
func dockerContainerDisplayName(container models.DockerContainer) string {
|
|
name := strings.TrimSpace(container.Name)
|
|
if strings.HasPrefix(name, "/") {
|
|
name = strings.TrimLeft(name, "/")
|
|
}
|
|
if name == "" {
|
|
containerID := strings.TrimSpace(container.ID)
|
|
if len(containerID) > 12 {
|
|
containerID = containerID[:12]
|
|
}
|
|
return containerID
|
|
}
|
|
return name
|
|
}
|
|
|
|
// dockerContainerOverrideKey builds the durable override key for a container:
|
|
// host + container *name*. Container IDs change on every recreate (image
|
|
// update, compose up), so ID-keyed overrides orphan the moment a container is
|
|
// updated; the name is the identity that survives (#1601). Returns "" when
|
|
// either part is missing so callers fall back to the legacy ID key.
|
|
func dockerContainerOverrideKey(hostID, containerName string) string {
|
|
hostID = strings.TrimSpace(hostID)
|
|
name := strings.TrimLeft(strings.TrimSpace(containerName), "/")
|
|
if hostID == "" || name == "" {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("docker:%s/%s", hostID, name)
|
|
}
|
|
|
|
// lookupDockerContainerOverrideNoLock resolves a container override,
|
|
// preferring the stable host+name key and falling back to the legacy
|
|
// container-ID resource ID so pre-migration entries keep working.
|
|
// Callers must hold m.mu.
|
|
func (m *Manager) lookupDockerContainerOverrideNoLock(hostID, containerName, legacyResourceID string) (ThresholdConfig, bool) {
|
|
if stableKey := dockerContainerOverrideKey(hostID, containerName); stableKey != "" {
|
|
if override, ok := m.config.Overrides[stableKey]; ok {
|
|
return override, true
|
|
}
|
|
}
|
|
if legacyResourceID != "" {
|
|
if override, ok := m.config.Overrides[legacyResourceID]; ok {
|
|
return override, true
|
|
}
|
|
}
|
|
return ThresholdConfig{}, false
|
|
}
|
|
|
|
// dockerNameReferencePrefix marks the name of a Docker container or Swarm
|
|
// service that has no ID in the slot of an alert reference that otherwise
|
|
// holds the ID. Docker and Swarm IDs never contain a colon, so a name
|
|
// reference can never equal an ID reference or the host's own reference.
|
|
const dockerNameReferencePrefix = "name:"
|
|
|
|
// DockerHostResourceID builds the reference of a Docker host's own alerts
|
|
// ("docker:<host ID>"). Only the host's connectivity alert uses it. Returns ""
|
|
// without a host ID.
|
|
func DockerHostResourceID(hostID string) string {
|
|
hostID = strings.TrimSpace(hostID)
|
|
if hostID == "" {
|
|
return ""
|
|
}
|
|
return "docker:" + hostID
|
|
}
|
|
|
|
// DockerContainerResourceID builds the reference of a Docker container's
|
|
// alerts: "docker:<host ID>/<container ID>". A container reported without an
|
|
// ID is referenced by its name, "docker:<host ID>/name:<name>", so it never
|
|
// shares its host's reference or another container's. Returns "" for a
|
|
// container with neither, which has no identity to alert under. Patrol scope
|
|
// resolution registers the same forms as aliases of the collected container.
|
|
func DockerContainerResourceID(hostID, containerID, containerName string) string {
|
|
hostID = strings.TrimSpace(hostID)
|
|
ref := strings.TrimSpace(containerID)
|
|
if ref == "" {
|
|
name := strings.TrimLeft(strings.TrimSpace(containerName), "/")
|
|
if hostID == "" || name == "" {
|
|
return ""
|
|
}
|
|
ref = dockerNameReferencePrefix + name
|
|
}
|
|
if hostID == "" {
|
|
return fmt.Sprintf("docker:container/%s", ref)
|
|
}
|
|
return fmt.Sprintf("docker:%s/%s", hostID, ref)
|
|
}
|
|
|
|
func normalizeDockerUpdateTrackingPart(part string) string {
|
|
return strings.ToLower(strings.TrimSpace(part))
|
|
}
|
|
|
|
// dockerUpdateTrackingHostKey builds a stable host identity for Docker update timing.
|
|
func dockerUpdateTrackingHostKey(host models.DockerHost) string {
|
|
switch {
|
|
case normalizeDockerUpdateTrackingPart(host.AgentID) != "":
|
|
return "agent:" + normalizeDockerUpdateTrackingPart(host.AgentID)
|
|
case normalizeDockerUpdateTrackingPart(host.TokenID) != "":
|
|
return "token:" + normalizeDockerUpdateTrackingPart(host.TokenID)
|
|
case normalizeDockerUpdateTrackingPart(host.MachineID) != "":
|
|
return "machine:" + normalizeDockerUpdateTrackingPart(host.MachineID)
|
|
case normalizeDockerUpdateTrackingPart(host.Hostname) != "":
|
|
return "hostname:" + normalizeDockerUpdateTrackingPart(host.Hostname)
|
|
case normalizeDockerUpdateTrackingPart(host.ID) != "":
|
|
return "id:" + normalizeDockerUpdateTrackingPart(host.ID)
|
|
case normalizeDockerUpdateTrackingPart(host.DisplayName) != "":
|
|
return "name:" + normalizeDockerUpdateTrackingPart(host.DisplayName)
|
|
default:
|
|
return "unknown-host"
|
|
}
|
|
}
|
|
|
|
func dockerUpdateTrackingContainerKey(container models.DockerContainer) string {
|
|
if containerID := normalizeDockerUpdateTrackingPart(container.ID); containerID != "" {
|
|
return "id:" + containerID
|
|
}
|
|
|
|
name := normalizeDockerUpdateTrackingPart(container.Name)
|
|
name = strings.TrimPrefix(name, "/")
|
|
if name != "" {
|
|
return "name:" + name
|
|
}
|
|
|
|
if image := normalizeDockerUpdateTrackingPart(container.Image); image != "" {
|
|
return "image:" + image
|
|
}
|
|
|
|
return "unknown-container"
|
|
}
|
|
|
|
func dockerUpdateTrackingKey(host models.DockerHost, container models.DockerContainer) string {
|
|
return fmt.Sprintf("docker-update:%s/%s", dockerUpdateTrackingHostKey(host), dockerUpdateTrackingContainerKey(container))
|
|
}
|
|
|
|
func dockerUpdateTrackingHostPrefix(host models.DockerHost) string {
|
|
return fmt.Sprintf("docker-update:%s/", dockerUpdateTrackingHostKey(host))
|
|
}
|
|
|
|
// dockerServiceDisplayName normalizes the service name for alert readability.
|
|
func dockerServiceDisplayName(service models.DockerService) string {
|
|
name := strings.TrimSpace(service.Name)
|
|
if name != "" {
|
|
return name
|
|
}
|
|
serviceID := strings.TrimSpace(service.ID)
|
|
if len(serviceID) > 12 {
|
|
serviceID = serviceID[:12]
|
|
}
|
|
if serviceID == "" {
|
|
return "service"
|
|
}
|
|
return serviceID
|
|
}
|
|
|
|
// DockerServiceResourceID builds the reference of a Docker Swarm service's
|
|
// alerts: "docker:<host ID>/service/<service ID>". A service reported without
|
|
// an ID is referenced by its normalized name, "docker:<host ID>/service/name:<name>",
|
|
// so it never shares another service's ID reference. Returns "" for a service
|
|
// with neither, which has no identity to alert under. Patrol scope resolution
|
|
// registers the same forms as aliases on the owning Docker host record so a
|
|
// service alert's resource ID resolves there.
|
|
func DockerServiceResourceID(hostID, serviceID, serviceName string) string {
|
|
hostID = strings.TrimSpace(hostID)
|
|
normalizedServiceID := strings.TrimSpace(serviceID)
|
|
if normalizedServiceID == "" {
|
|
name := strings.TrimSpace(serviceName)
|
|
builder := strings.Builder{}
|
|
for _, r := range strings.ToLower(name) {
|
|
switch {
|
|
case r >= 'a' && r <= 'z':
|
|
builder.WriteRune(r)
|
|
case r >= '0' && r <= '9':
|
|
builder.WriteRune(r)
|
|
case r == '-', r == '_':
|
|
builder.WriteRune(r)
|
|
case r == ' ' || r == '/' || r == '\\' || r == ':' || r == '.':
|
|
builder.WriteRune('-')
|
|
}
|
|
}
|
|
normalizedServiceID = strings.Trim(builder.String(), "-_")
|
|
if normalizedServiceID == "" {
|
|
return ""
|
|
}
|
|
if len(normalizedServiceID) > 32 {
|
|
normalizedServiceID = normalizedServiceID[:32]
|
|
}
|
|
normalizedServiceID = dockerNameReferencePrefix + normalizedServiceID
|
|
}
|
|
if hostID == "" {
|
|
return fmt.Sprintf("docker-service:%s", normalizedServiceID)
|
|
}
|
|
return fmt.Sprintf("docker:%s/service/%s", hostID, normalizedServiceID)
|
|
}
|
|
|
|
// matchesDockerIgnoredPrefix reports whether a container name or ID matches
|
|
// any entry in the ignore list. A bare entry keeps its historical
|
|
// prefix-match semantics. Entries may also use the wildcard forms shared
|
|
// with the other ignore lists: "*token" matches a suffix, "*token*" a
|
|
// substring, "token*" an explicit prefix.
|
|
func matchesDockerIgnoredPrefix(name, id string, prefixes []string) bool {
|
|
if len(prefixes) == 0 {
|
|
return false
|
|
}
|
|
|
|
name = strings.ToLower(strings.TrimSpace(name))
|
|
id = strings.ToLower(strings.TrimSpace(id))
|
|
|
|
for _, raw := range prefixes {
|
|
pattern := strings.ToLower(strings.TrimSpace(raw))
|
|
// A pattern that is nothing but wildcards would ignore every
|
|
// container, which is what DisableAllDockerContainers is for.
|
|
if pattern == "" || strings.Trim(pattern, "*") == "" {
|
|
continue
|
|
}
|
|
if matchesIgnoredContainerPattern(name, pattern) || matchesIgnoredContainerPattern(id, pattern) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func matchesIgnoredContainerPattern(value, pattern string) bool {
|
|
if value == "" {
|
|
return false
|
|
}
|
|
leading := strings.HasPrefix(pattern, "*")
|
|
trailing := strings.HasSuffix(pattern, "*")
|
|
switch {
|
|
case leading && trailing:
|
|
return strings.Contains(value, pattern[1:len(pattern)-1])
|
|
case leading:
|
|
return strings.HasSuffix(value, pattern[1:])
|
|
case trailing:
|
|
return strings.HasPrefix(value, pattern[:len(pattern)-1])
|
|
default:
|
|
return strings.HasPrefix(value, pattern)
|
|
}
|
|
}
|
|
|
|
// CheckDockerHost evaluates Docker host telemetry and container metrics for alerts.
|
|
func (m *Manager) CheckDockerHost(host models.DockerHost) {
|
|
if host.ID == "" {
|
|
return
|
|
}
|
|
|
|
// Fresh telemetry marks the host as online and clears any offline alert.
|
|
m.HandleDockerHostOnline(host)
|
|
|
|
m.mu.RLock()
|
|
alertsEnabled := m.config.Enabled
|
|
disableAllDockerHosts, _ := m.alertPolicyTypeSwitchesNoLock("docker-host")
|
|
ignoredPrefixes := append([]string(nil), m.config.DockerIgnoredContainerPrefixes...)
|
|
m.mu.RUnlock()
|
|
if !alertsEnabled {
|
|
return
|
|
}
|
|
if disableAllDockerHosts {
|
|
return
|
|
}
|
|
|
|
seen := make(map[string]struct{}, len(host.Containers)+len(host.Services))
|
|
seenUpdateTracking := make(map[string]struct{}, len(host.Containers))
|
|
for _, container := range host.Containers {
|
|
resourceID := DockerContainerResourceID(host.ID, container.ID, container.Name)
|
|
if resourceID == "" {
|
|
// Neither an ID nor a name: nothing identifies its alerts.
|
|
continue
|
|
}
|
|
containerName := dockerContainerDisplayName(container)
|
|
updateTrackingKey := dockerUpdateTrackingKey(host, container)
|
|
|
|
if matchesDockerIgnoredPrefix(containerName, container.ID, ignoredPrefixes) {
|
|
log.Debug().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Msg("Skipping Docker container alert evaluation due to ignored prefix")
|
|
m.clearDockerContainerStateAlert(resourceID)
|
|
m.clearDockerContainerHealthAlert(resourceID)
|
|
m.clearDockerContainerMetricAlerts(resourceID)
|
|
m.clearAlert(fmt.Sprintf("docker-container-restart-loop-%s", resourceID))
|
|
m.clearAlert(fmt.Sprintf("docker-container-oom-%s", resourceID))
|
|
m.clearAlert(fmt.Sprintf("docker-container-memory-limit-%s", resourceID))
|
|
m.mu.Lock()
|
|
delete(m.dockerRestartTracking, resourceID)
|
|
m.mu.Unlock()
|
|
m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)
|
|
continue
|
|
}
|
|
|
|
seen[resourceID] = struct{}{}
|
|
seenUpdateTracking[updateTrackingKey] = struct{}{}
|
|
m.evaluateDockerContainer(host, container, resourceID)
|
|
}
|
|
|
|
for _, service := range host.Services {
|
|
resourceID := DockerServiceResourceID(host.ID, service.ID, service.Name)
|
|
if resourceID == "" {
|
|
continue
|
|
}
|
|
seen[resourceID] = struct{}{}
|
|
m.evaluateDockerService(host, service, resourceID)
|
|
}
|
|
|
|
m.cleanupDockerContainerAlertsWithTracking(host, seen, seenUpdateTracking)
|
|
}
|
|
|
|
func (m *Manager) evaluateDockerContainer(host models.DockerHost, container models.DockerContainer, resourceID string) {
|
|
m.mu.RLock()
|
|
disableAllContainers, _ := m.alertPolicyTypeSwitchesNoLock("app-container")
|
|
m.mu.RUnlock()
|
|
if disableAllContainers {
|
|
return
|
|
}
|
|
|
|
containerName := dockerContainerDisplayName(container)
|
|
nodeName := strings.TrimSpace(host.Hostname)
|
|
instanceName := dockerInstanceName(host)
|
|
resourceType := "app-container"
|
|
containerTags := dockerLabelTags(container.Labels)
|
|
|
|
// Resolve the metric thresholds while the lock is held: the Docker defaults
|
|
// are fields of m.config, which UpdateConfig replaces, and the override
|
|
// still points into it. applyThresholdOverride clones what it keeps.
|
|
m.mu.RLock()
|
|
overrideConfig, hasOverride := m.lookupDockerContainerOverrideNoLock(host.ID, container.Name, resourceID)
|
|
thresholds := ThresholdConfig{
|
|
CPU: cloneThreshold(&m.config.DockerDefaults.CPU),
|
|
Memory: cloneThreshold(&m.config.DockerDefaults.Memory),
|
|
Disk: cloneThreshold(&m.config.DockerDefaults.Disk),
|
|
}
|
|
if hasOverride {
|
|
thresholds = m.applyThresholdOverride(thresholds, overrideConfig)
|
|
}
|
|
m.mu.RUnlock()
|
|
if hasOverride && overrideConfig.Disabled {
|
|
// Alerts disabled via override; clear any existing alerts and skip evaluation.
|
|
m.clearDockerContainerStateAlert(resourceID)
|
|
m.clearDockerContainerHealthAlert(resourceID)
|
|
m.clearDockerContainerMetricAlerts(resourceID)
|
|
m.clearAlert(fmt.Sprintf("docker-container-update-%s", resourceID))
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-image-update"))
|
|
m.clearDockerContainerUpdateTracking(resourceID, dockerUpdateTrackingKey(host, container))
|
|
return
|
|
}
|
|
|
|
state := strings.ToLower(strings.TrimSpace(container.State))
|
|
if state == "" {
|
|
state = strings.ToLower(strings.TrimSpace(container.Status))
|
|
}
|
|
|
|
if state != "running" {
|
|
m.checkDockerContainerState(host, container, resourceID, containerName, instanceName, nodeName)
|
|
m.clearDockerContainerMetricAlerts(resourceID, "cpu", "memory", "disk")
|
|
} else {
|
|
m.clearDockerContainerStateAlert(resourceID)
|
|
|
|
if thresholds.CPU != nil {
|
|
cpuCapacityPercent := models.DockerContainerCPUCapacityPercent(container, host.CPUs)
|
|
cpuMetadata := map[string]interface{}{
|
|
"resourceType": resourceType,
|
|
"hostId": host.ID,
|
|
"hostName": host.DisplayName,
|
|
"hostHostname": host.Hostname,
|
|
"containerId": container.ID,
|
|
"containerName": containerName,
|
|
"image": container.Image,
|
|
"state": container.State,
|
|
"status": container.Status,
|
|
"restartCount": container.RestartCount,
|
|
"metric": "cpu",
|
|
"cpuPercent": cpuCapacityPercent,
|
|
"cpuCapacityPercent": cpuCapacityPercent,
|
|
"cpuRawPercent": container.CPUPercent,
|
|
"cpuCapacityCores": host.CPUs,
|
|
"tags": containerTags,
|
|
}
|
|
spec, err := buildCanonicalMetricSpec(resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "cpu", thresholds.CPU)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container CPU metric spec")
|
|
} else {
|
|
m.checkMetricWithCanonicalSpec(spec, containerName, nodeName, instanceName, resourceType, cpuCapacityPercent, thresholds.CPU, &metricOptions{Metadata: cpuMetadata})
|
|
}
|
|
}
|
|
|
|
if thresholds.Memory != nil {
|
|
memMetadata := map[string]interface{}{
|
|
"resourceType": resourceType,
|
|
"hostId": host.ID,
|
|
"hostName": host.DisplayName,
|
|
"hostHostname": host.Hostname,
|
|
"containerId": container.ID,
|
|
"containerName": containerName,
|
|
"image": container.Image,
|
|
"state": container.State,
|
|
"status": container.Status,
|
|
"restartCount": container.RestartCount,
|
|
"metric": "memory",
|
|
"memoryPercent": container.MemoryPercent,
|
|
"memoryUsageBytes": container.MemoryUsage,
|
|
"tags": containerTags,
|
|
}
|
|
if container.MemoryLimit > 0 {
|
|
memMetadata["memoryLimitBytes"] = container.MemoryLimit
|
|
}
|
|
spec, err := buildCanonicalMetricSpec(resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "memory", thresholds.Memory)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container memory metric spec")
|
|
} else {
|
|
m.checkMetricWithCanonicalSpec(spec, containerName, nodeName, instanceName, resourceType, container.MemoryPercent, thresholds.Memory, &metricOptions{Metadata: memMetadata})
|
|
}
|
|
}
|
|
|
|
if thresholds.Disk != nil {
|
|
totalBytes := container.RootFilesystemBytes
|
|
usedBytes := container.WritableLayerBytes
|
|
if totalBytes > 0 && usedBytes >= 0 {
|
|
diskPercent := (float64(usedBytes) / float64(totalBytes)) * 100
|
|
diskMetadata := map[string]interface{}{
|
|
"resourceType": resourceType,
|
|
"hostId": host.ID,
|
|
"hostName": host.DisplayName,
|
|
"hostHostname": host.Hostname,
|
|
"containerId": container.ID,
|
|
"containerName": containerName,
|
|
"image": container.Image,
|
|
"state": container.State,
|
|
"status": container.Status,
|
|
"restartCount": container.RestartCount,
|
|
"metric": "disk",
|
|
"diskPercent": diskPercent,
|
|
"writableLayerBytes": usedBytes,
|
|
"rootFilesystemBytes": totalBytes,
|
|
"mountCount": len(container.Mounts),
|
|
"tags": containerTags,
|
|
}
|
|
if container.BlockIO != nil {
|
|
diskMetadata["blockIoReadBytes"] = container.BlockIO.ReadBytes
|
|
diskMetadata["blockIoWriteBytes"] = container.BlockIO.WriteBytes
|
|
}
|
|
spec, err := buildCanonicalMetricSpec(resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "disk", thresholds.Disk)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container disk metric spec")
|
|
} else {
|
|
m.checkMetricWithCanonicalSpec(spec, containerName, nodeName, instanceName, resourceType, diskPercent, thresholds.Disk, &metricOptions{Metadata: diskMetadata})
|
|
}
|
|
} else {
|
|
m.clearDockerContainerMetricAlerts(resourceID, "disk")
|
|
}
|
|
}
|
|
}
|
|
|
|
m.checkDockerContainerHealth(host, container, resourceID, containerName, instanceName, nodeName)
|
|
|
|
// Docker-specific checks
|
|
m.checkDockerContainerRestartLoop(host, container, resourceID, containerName, instanceName, nodeName)
|
|
m.checkDockerContainerOOMKill(host, container, resourceID, containerName, instanceName, nodeName)
|
|
m.checkDockerContainerMemoryLimit(host, container, resourceID, containerName, instanceName, nodeName)
|
|
m.checkDockerContainerImageUpdate(host, container, resourceID, containerName, instanceName, nodeName)
|
|
}
|
|
|
|
func (m *Manager) evaluateDockerService(host models.DockerHost, service models.DockerService, resourceID string) {
|
|
m.mu.RLock()
|
|
disableAllServices, _ := m.alertPolicyTypeSwitchesNoLock("docker-service")
|
|
warnPct := m.config.DockerDefaults.ServiceWarnGapPct
|
|
critPct := m.config.DockerDefaults.ServiceCritGapPct
|
|
overrideConfig, hasOverride := m.config.Overrides[resourceID]
|
|
m.mu.RUnlock()
|
|
|
|
if disableAllServices {
|
|
m.clearDockerServiceAlert(resourceID)
|
|
return
|
|
}
|
|
if hasOverride && overrideConfig.Disabled {
|
|
m.clearDockerServiceAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
desired := service.DesiredTasks
|
|
running := service.RunningTasks
|
|
if desired <= 0 {
|
|
m.clearDockerServiceAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
missing := desired - running
|
|
if missing < 0 {
|
|
missing = 0
|
|
}
|
|
|
|
percentMissing := 0.0
|
|
if desired > 0 {
|
|
percentMissing = (float64(missing) / float64(desired)) * 100.0
|
|
}
|
|
|
|
thresholdValue := 0.0
|
|
serviceName := dockerServiceDisplayName(service)
|
|
instanceName := dockerInstanceName(host)
|
|
nodeName := strings.TrimSpace(host.Hostname)
|
|
|
|
metadata := map[string]interface{}{
|
|
"resourceType": "docker-service",
|
|
"hostId": host.ID,
|
|
"hostName": host.DisplayName,
|
|
"hostHostname": host.Hostname,
|
|
"serviceId": service.ID,
|
|
"serviceName": service.Name,
|
|
"stack": service.Stack,
|
|
"mode": service.Mode,
|
|
"desiredTasks": service.DesiredTasks,
|
|
"runningTasks": service.RunningTasks,
|
|
"completedTasks": service.CompletedTasks,
|
|
"missingTasks": missing,
|
|
"percentMissing": percentMissing,
|
|
"tags": dockerLabelTags(service.Labels),
|
|
}
|
|
alertID := fmt.Sprintf("docker-service-health-%s", resourceID)
|
|
|
|
if critPct > 0 && percentMissing >= float64(critPct) {
|
|
thresholdValue = float64(critPct)
|
|
} else if warnPct > 0 && percentMissing >= float64(warnPct) {
|
|
thresholdValue = float64(warnPct)
|
|
}
|
|
|
|
updateState := ""
|
|
updateMessage := ""
|
|
updateSeverity := AlertLevel("")
|
|
if service.UpdateStatus != nil {
|
|
updateState = strings.ToLower(strings.TrimSpace(service.UpdateStatus.State))
|
|
updateMessage = strings.TrimSpace(service.UpdateStatus.Message)
|
|
switch updateState {
|
|
case "paused", "rollback_started", "rollback_paused":
|
|
updateSeverity = AlertLevelWarning
|
|
case "rollback_failed":
|
|
updateSeverity = AlertLevelCritical
|
|
}
|
|
if service.UpdateStatus.CompletedAt != nil && !service.UpdateStatus.CompletedAt.IsZero() {
|
|
metadata["updateCompletedAt"] = service.UpdateStatus.CompletedAt.UTC()
|
|
}
|
|
if updateState != "" {
|
|
metadata["updateState"] = service.UpdateStatus.State
|
|
}
|
|
if updateMessage != "" {
|
|
metadata["updateMessage"] = updateMessage
|
|
}
|
|
}
|
|
|
|
if thresholdValue == 0 && updateSeverity != "" {
|
|
spec, err := buildCanonicalDiscreteStateSpec(resourceID, serviceName, unifiedresources.ResourceTypeDockerService, updateSeverity, 1, false, "update-state",
|
|
[]string{"paused", "rollback_started", "rollback_paused", "rollback_failed"})
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("service", serviceName).
|
|
Str("resourceID", resourceID).
|
|
Msg("Skipping invalid canonical docker service update-state spec")
|
|
return
|
|
}
|
|
|
|
message := fmt.Sprintf("Docker service '%s' update state: %s", serviceName, service.UpdateStatus.State)
|
|
summary := fmt.Sprintf("Update state: %s", service.UpdateStatus.State)
|
|
if updateMessage != "" {
|
|
message = fmt.Sprintf("%s (%s)", message, updateMessage)
|
|
summary = fmt.Sprintf("%s (%s)", summary, updateMessage)
|
|
}
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{ObservedAt: time.Now(), DiscreteState: &alertspecs.DiscreteStateEvidence{StateKey: "update-state", Observed: updateState}},
|
|
AlertID: alertID,
|
|
AlertType: "docker-service-health",
|
|
ResourceID: resourceID,
|
|
ResourceName: serviceName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: message,
|
|
Summary: summary,
|
|
Value: percentMissing,
|
|
Threshold: 0,
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
RateLimit: true,
|
|
NotifyOnSeverityChange: true,
|
|
AddToHistoryOnSeverityChange: true,
|
|
DispatchAsync: true,
|
|
})
|
|
return
|
|
}
|
|
|
|
if thresholdValue == 0 {
|
|
m.clearDockerServiceAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
spec, err := buildCanonicalServiceGapSpec(resourceID, serviceName, unifiedresources.ResourceTypeDockerService, serviceName, float64(warnPct), float64(critPct), false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("service", serviceName).
|
|
Str("resourceID", resourceID).
|
|
Msg("Skipping invalid canonical docker service gap spec")
|
|
m.clearDockerServiceAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
message := fmt.Sprintf("Docker service '%s' is running %d of %d desired tasks", serviceName, service.RunningTasks, service.DesiredTasks)
|
|
summary := fmt.Sprintf("Running %d of %d desired tasks", service.RunningTasks, service.DesiredTasks)
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
ServiceGap: &alertspecs.ServiceGapEvidence{
|
|
Service: serviceName,
|
|
Desired: desired,
|
|
Running: running,
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-service-health",
|
|
ResourceID: resourceID,
|
|
ResourceName: serviceName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: message,
|
|
Summary: summary,
|
|
Value: percentMissing,
|
|
Threshold: thresholdValue,
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
RateLimit: true,
|
|
NotifyOnSeverityChange: true,
|
|
AddToHistoryOnSeverityChange: true,
|
|
DispatchAsync: true,
|
|
})
|
|
}
|
|
|
|
func (m *Manager) clearDockerServiceAlert(resourceID string) {
|
|
m.clearAlert(canonicalServiceGapStateID(resourceID))
|
|
m.clearAlert(canonicalDiscreteStateStateID(resourceID, "update-state"))
|
|
}
|
|
|
|
// HandleDockerHostOnline clears offline tracking and alerts for a Docker host.
|
|
func (m *Manager) HandleDockerHostOnline(host models.DockerHost) {
|
|
if host.ID == "" {
|
|
return
|
|
}
|
|
|
|
resourceID := DockerHostResourceID(host.ID)
|
|
alertID := canonicalConnectivityStateID(resourceID)
|
|
|
|
m.mu.Lock()
|
|
exists := m.hasActiveAlertNoLock(alertID)
|
|
// The host was observed online: a healthy observation ends any
|
|
// in-flight offline confirmation run in the core.
|
|
m.core.ApplyDiscrete(reducer.DiscreteSignal{ResourceID: resourceID, Key: canonicalConnectivitySpecID(resourceID), Matched: false, ObservedAt: m.policyNow()}, reducer.DiscreteRule{})
|
|
m.mu.Unlock()
|
|
|
|
if exists {
|
|
m.clearAlert(alertID)
|
|
}
|
|
}
|
|
|
|
// HandleDockerHostRemoved clears all alerts and tracking when a Docker host is deleted.
|
|
func (m *Manager) HandleDockerHostRemoved(host models.DockerHost) {
|
|
if host.ID == "" {
|
|
return
|
|
}
|
|
|
|
// Reuse the online handler to clear offline alerts and tracking.
|
|
m.HandleDockerHostOnline(host)
|
|
// Drop any container alerts and host-scoped tracking entries.
|
|
m.clearDockerHostContainerAlerts(host)
|
|
}
|
|
|
|
// HandleDockerHostOffline raises an alert when a Docker host stops reporting.
|
|
func (m *Manager) HandleDockerHostOffline(host models.DockerHost) {
|
|
if host.ID == "" {
|
|
return
|
|
}
|
|
|
|
m.mu.RLock()
|
|
if !m.config.Enabled {
|
|
m.mu.RUnlock()
|
|
return
|
|
}
|
|
_, disableDockerHostsOffline := m.alertPolicyTypeSwitchesNoLock("docker-host")
|
|
m.mu.RUnlock()
|
|
|
|
resourceID := DockerHostResourceID(host.ID)
|
|
alertID := canonicalConnectivityStateID(resourceID)
|
|
instanceName := dockerInstanceName(host)
|
|
nodeName := strings.TrimSpace(host.Hostname)
|
|
|
|
if disableDockerHostsOffline {
|
|
m.mu.Lock()
|
|
m.mu.Unlock()
|
|
m.clearAlert(alertID)
|
|
return
|
|
}
|
|
|
|
var disableConnectivity bool
|
|
m.mu.RLock()
|
|
if override, exists := m.config.Overrides[host.ID]; exists {
|
|
disableConnectivity = override.DisableConnectivity
|
|
}
|
|
m.mu.RUnlock()
|
|
|
|
if disableConnectivity {
|
|
m.clearAlert(alertID)
|
|
m.mu.Lock()
|
|
m.mu.Unlock()
|
|
return
|
|
}
|
|
|
|
spec, err := buildCanonicalConnectivitySpec(resourceID, host.DisplayName, unifiedresources.ResourceType("docker-host"), AlertLevelCritical, 3, false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("dockerHost", host.DisplayName).
|
|
Str("hostID", host.ID).
|
|
Msg("Skipping invalid canonical docker host connectivity spec")
|
|
return
|
|
}
|
|
|
|
result, ok := m.evaluateCanonicalLifecycleAlert(canonicalLifecycleAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{ObservedAt: time.Now(), Connectivity: &alertspecs.ConnectivityEvidence{Signal: "status", Connected: false}},
|
|
AlertID: alertID,
|
|
AlertType: "docker-host-offline",
|
|
ResourceID: resourceID,
|
|
ResourceName: host.DisplayName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker host '%s' is offline", host.DisplayName),
|
|
Summary: "Offline",
|
|
Metadata: map[string]interface{}{
|
|
"resourceType": "docker-host",
|
|
"hostId": host.ID,
|
|
"hostname": host.Hostname,
|
|
"agentId": host.AgentID,
|
|
"displayName": host.DisplayName,
|
|
},
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
RateLimit: true,
|
|
DispatchAsync: false,
|
|
})
|
|
if !ok || result.Transition == nil || result.Transition.Kind != alertspecs.EvaluationTransitionActivated {
|
|
return
|
|
}
|
|
|
|
m.mu.RLock()
|
|
alert, _ := m.getActiveAlertNoLock(alertID)
|
|
m.mu.RUnlock()
|
|
if alert != nil {
|
|
if callbacks := m.getAlertForAICallbacks(); len(callbacks) > 0 {
|
|
alertCopy := cloneAlertForOutput(alert)
|
|
m.dispatchCallback(true, func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Error().Interface("panic", r).Str("alertID", alertCopy.ID).Msg("panic in AI alert callback")
|
|
}
|
|
}()
|
|
for _, callback := range callbacks {
|
|
callback(alertCopy)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
m.clearDockerHostContainerAlerts(host)
|
|
}
|
|
|
|
func (m *Manager) checkDockerContainerState(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
alertID := fmt.Sprintf("docker-container-state-%s", resourceID)
|
|
|
|
m.mu.RLock()
|
|
override, hasOverride := m.lookupDockerContainerOverrideNoLock(host.ID, container.Name, resourceID)
|
|
defaultDisable := m.config.DockerDefaults.StateDisableConnectivity
|
|
defaultSeverity := NormalizePoweredOffSeverity(m.config.DockerDefaults.StatePoweredOffSeverity)
|
|
m.mu.RUnlock()
|
|
|
|
disableConnectivity := defaultDisable
|
|
severity := defaultSeverity
|
|
if hasOverride {
|
|
// Same rule as applyThresholdOverride: an override turns state
|
|
// alerts back on over a disabled default only when it carries the
|
|
// row's own severity. DisableConnectivity is a plain bool, so an
|
|
// override saved for a CPU or memory threshold alone reads false
|
|
// here, and used to re-enable state alerts the operator had turned
|
|
// off for every container.
|
|
if override.DisableConnectivity {
|
|
disableConnectivity = true
|
|
} else if override.PoweredOffSeverity != "" {
|
|
disableConnectivity = false
|
|
}
|
|
|
|
if override.PoweredOffSeverity != "" {
|
|
severity = NormalizePoweredOffSeverity(override.PoweredOffSeverity)
|
|
}
|
|
}
|
|
|
|
if disableConnectivity {
|
|
m.clearDockerContainerStateAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
// A container that finished its work is not a stopped service. The
|
|
// row's own severity still wins: an operator who chose one for this
|
|
// container asked to hear whenever it is not running.
|
|
ownSeverity := hasOverride && override.PoweredOffSeverity != ""
|
|
if !ownSeverity && DockerContainerFinishedJob(container) {
|
|
m.clearDockerContainerStateAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
observedState := strings.ToLower(strings.TrimSpace(container.State))
|
|
if observedState == "" {
|
|
observedState = "unknown"
|
|
}
|
|
|
|
spec, err := buildCanonicalDiscreteStateSpec(resourceID, containerName, unifiedresources.ResourceTypeAppContainer, severity, 2, false, "runtime-state",
|
|
[]string{"created", "restarting", "removing", "paused", "exited", "dead", "unknown"})
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container state spec")
|
|
return
|
|
}
|
|
|
|
_, _ = m.evaluateCanonicalLifecycleAlert(canonicalLifecycleAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
DiscreteState: &alertspecs.DiscreteStateEvidence{
|
|
StateKey: "runtime-state",
|
|
Observed: observedState,
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-state",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker container '%s' is %s", containerName, strings.TrimSpace(container.Status)),
|
|
Summary: capitalizeFirst(strings.TrimSpace(container.Status)),
|
|
Metadata: dockerContainerAlertMetadata(host, container, containerName),
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
DispatchAsync: true,
|
|
})
|
|
}
|
|
|
|
// dockerFinishedJobMaxRun is the longest a container may have run and still
|
|
// read as a finished job. Jobs run for seconds or minutes and services for
|
|
// days, so the exact value matters little; it has to sit between the two.
|
|
const dockerFinishedJobMaxRun = time.Hour
|
|
|
|
// DockerContainerFinishedJob reports whether an exited container ended
|
|
// its own work and is meant to stay down: it exited with code 0, was not
|
|
// OOM killed, ran for less than dockerFinishedJobMaxRun, and has a restart
|
|
// policy that leaves a clean exit alone ("no" or "on-failure"). Migrations,
|
|
// init steps and scheduled jobs end this way.
|
|
//
|
|
// Under "always" or "unless-stopped" Docker restarts a clean exit, so an
|
|
// exited container there was stopped by someone and still alerts. An empty
|
|
// policy means the agent did not report one (an agent older than the field),
|
|
// and that container keeps alerting too: exit code 0 alone cannot tell a
|
|
// finished job from a service that was stopped gracefully.
|
|
//
|
|
// The run time is what separates a job from a service that has no restart
|
|
// policy. Such a service exits 0 when Docker shuts down for a host reboot and
|
|
// then stays down, which is an outage the operator needs to hear about. A
|
|
// container whose start or finish time is unknown is not called finished.
|
|
//
|
|
// It is exported because the answer means no state alert will ever fire for
|
|
// the container, and Patrol must know that before it treats the stopped state
|
|
// as something an alert already covers.
|
|
func DockerContainerFinishedJob(container models.DockerContainer) bool {
|
|
if strings.ToLower(strings.TrimSpace(container.State)) != "exited" {
|
|
return false
|
|
}
|
|
if container.ExitCode != 0 {
|
|
return false
|
|
}
|
|
if container.OOMKilled != nil && *container.OOMKilled {
|
|
return false
|
|
}
|
|
switch strings.ToLower(strings.TrimSpace(container.RestartPolicy)) {
|
|
case "no", "on-failure":
|
|
default:
|
|
return false
|
|
}
|
|
if container.StartedAt == nil || container.FinishedAt == nil ||
|
|
container.StartedAt.IsZero() || container.FinishedAt.IsZero() {
|
|
return false
|
|
}
|
|
ran := container.FinishedAt.Sub(*container.StartedAt)
|
|
return ran >= 0 && ran < dockerFinishedJobMaxRun
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerStateAlert(resourceID string) {
|
|
m.mu.Lock()
|
|
m.mu.Unlock()
|
|
m.clearAlert(canonicalDiscreteStateStateID(resourceID, "runtime-state"))
|
|
}
|
|
|
|
func dockerContainerAlertMetadata(host models.DockerHost, container models.DockerContainer, containerName string) map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"resourceType": "app-container",
|
|
"hostId": host.ID,
|
|
"hostName": host.DisplayName,
|
|
"hostHostname": host.Hostname,
|
|
"containerId": container.ID,
|
|
"containerName": containerName,
|
|
"image": container.Image,
|
|
"state": container.State,
|
|
"status": container.Status,
|
|
"tags": dockerLabelTags(container.Labels),
|
|
}
|
|
}
|
|
|
|
func dockerLabelTags(labels map[string]string) []string {
|
|
if len(labels) == 0 {
|
|
return nil
|
|
}
|
|
|
|
tags := make([]string, 0, len(labels))
|
|
for rawKey, rawValue := range labels {
|
|
key := strings.TrimSpace(rawKey)
|
|
if key == "" {
|
|
continue
|
|
}
|
|
value := strings.TrimSpace(rawValue)
|
|
if value == "" {
|
|
tags = append(tags, key)
|
|
continue
|
|
}
|
|
tags = append(tags, key+":"+value)
|
|
}
|
|
sort.Strings(tags)
|
|
return tags
|
|
}
|
|
|
|
func (m *Manager) checkDockerContainerHealth(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
// Docker retains the last health-check result after a container stops (and
|
|
// while health checks are suspended in other non-running states). That
|
|
// persisted value is no longer live health evidence; the runtime-state alert
|
|
// owns the non-running condition.
|
|
state := strings.ToLower(strings.TrimSpace(container.State))
|
|
if state != "" && state != "running" {
|
|
m.clearDockerContainerHealthAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
health := strings.ToLower(strings.TrimSpace(container.Health))
|
|
if health == "" || health == "none" || health == "healthy" || health == "starting" {
|
|
m.clearDockerContainerHealthAlert(resourceID)
|
|
return
|
|
}
|
|
|
|
alertID := fmt.Sprintf("docker-container-health-%s", resourceID)
|
|
spec, err := buildCanonicalHealthAssessmentSpec(resourceID+"-health", resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "docker-container-health", nil, false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container health spec")
|
|
return
|
|
}
|
|
|
|
severity := alertspecs.AlertSeverityWarning
|
|
if health == "unhealthy" {
|
|
severity = alertspecs.AlertSeverityCritical
|
|
}
|
|
|
|
metadata := dockerContainerAlertMetadata(host, container, containerName)
|
|
metadata["health"] = container.Health
|
|
|
|
// The agent says why the last health check failed, as a reason code. The
|
|
// text is stable while the reason is, so the alert does not change on
|
|
// every report; the failing streak stays out of it for that reason.
|
|
message := fmt.Sprintf("Docker container '%s' health is %s", containerName, container.Health)
|
|
summary := fmt.Sprintf("Health is %s", container.Health)
|
|
if why := container.HealthProbe.Describe(); why != "" && health == "unhealthy" {
|
|
message += " (health check: " + why + ")"
|
|
summary += " (" + why + ")"
|
|
metadata["healthProbeReason"] = container.HealthProbe.Reason
|
|
}
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
HealthAssessment: &alertspecs.HealthAssessmentEvidence{
|
|
Signal: "docker-container-health",
|
|
Severity: severity,
|
|
Codes: []string{health},
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-health",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: message,
|
|
Summary: summary,
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
DispatchAsync: false,
|
|
NotifyOnSeverityChange: true,
|
|
AddToHistoryOnSeverityChange: true,
|
|
})
|
|
|
|
log.Warn().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Str("health", container.Health).
|
|
Msg("Docker container health alert raised")
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerHealthAlert(resourceID string) {
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-health"))
|
|
}
|
|
|
|
// checkDockerContainerRestartLoop detects containers stuck in a restart loop
|
|
func (m *Manager) checkDockerContainerRestartLoop(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
alertID := fmt.Sprintf("docker-container-restart-loop-%s", resourceID)
|
|
now := time.Now()
|
|
|
|
m.mu.Lock()
|
|
|
|
// NormalizeDockerDefaults owns the factory values; 0 here is the
|
|
// operator's Off, so either one set to 0 turns restart-loop alerts off.
|
|
restartThreshold := m.config.DockerDefaults.RestartCount
|
|
timeWindow := m.config.DockerDefaults.RestartWindow
|
|
if restartThreshold <= 0 || timeWindow <= 0 {
|
|
delete(m.dockerRestartTracking, resourceID)
|
|
m.mu.Unlock()
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-restart-loop"))
|
|
return
|
|
}
|
|
|
|
record, exists := m.dockerRestartTracking[resourceID]
|
|
if !exists {
|
|
record = &dockerRestartRecord{
|
|
count: container.RestartCount,
|
|
lastCount: container.RestartCount,
|
|
times: []time.Time{},
|
|
lastChecked: now,
|
|
}
|
|
m.dockerRestartTracking[resourceID] = record
|
|
m.mu.Unlock()
|
|
return
|
|
}
|
|
|
|
// If restart count increased, track it
|
|
if container.RestartCount > record.lastCount {
|
|
newRestarts := container.RestartCount - record.lastCount
|
|
for i := 0; i < newRestarts; i++ {
|
|
record.times = append(record.times, now)
|
|
}
|
|
record.lastCount = container.RestartCount
|
|
}
|
|
|
|
// Clean up old restart times outside the window
|
|
cutoff := now.Add(-time.Duration(timeWindow) * time.Second)
|
|
var recentRestarts []time.Time
|
|
for _, t := range record.times {
|
|
if t.After(cutoff) {
|
|
recentRestarts = append(recentRestarts, t)
|
|
}
|
|
}
|
|
record.times = recentRestarts
|
|
record.lastChecked = now
|
|
|
|
recentCount := len(record.times)
|
|
m.mu.Unlock()
|
|
|
|
// Check if we have a restart loop
|
|
if recentCount > restartThreshold {
|
|
spec, err := buildCanonicalSeverityThresholdSpec(resourceID+"-restart-loop", resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "restart-count-window", 0, float64(restartThreshold+1), false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container restart loop spec")
|
|
return
|
|
}
|
|
|
|
metadata := dockerContainerAlertMetadata(host, container, containerName)
|
|
metadata["restartCount"] = container.RestartCount
|
|
metadata["recentRestarts"] = recentCount
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: now,
|
|
SeverityThreshold: &alertspecs.SeverityThresholdEvidence{
|
|
Metric: "restart-count-window",
|
|
Direction: alertspecs.ThresholdDirectionAbove,
|
|
Observed: float64(recentCount),
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-restart-loop",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker container '%s' has restarted %d times in the last %d minutes (restart loop detected)", containerName, recentCount, timeWindow/60),
|
|
Summary: fmt.Sprintf("Restarted %d times in the last %d minutes (restart loop detected)", recentCount, timeWindow/60),
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
})
|
|
|
|
log.Warn().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Int("restarts", recentCount).
|
|
Msg("Docker container restart loop detected")
|
|
} else {
|
|
// Clear alert if restart loop has stopped
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-restart-loop"))
|
|
}
|
|
}
|
|
|
|
// checkDockerContainerOOMKill detects when the runtime explicitly reports that
|
|
// a container was killed due to out of memory.
|
|
func (m *Manager) checkDockerContainerOOMKill(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
alertID := fmt.Sprintf("docker-container-oom-%s", resourceID)
|
|
|
|
state := strings.ToLower(strings.TrimSpace(container.State))
|
|
if (state == "exited" || state == "dead") && container.OOMKilled != nil && *container.OOMKilled {
|
|
spec, err := buildCanonicalHealthAssessmentSpec(resourceID+"-oom-kill", resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "docker-container-exit", []string{"oom-kill"}, false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container OOM spec")
|
|
return
|
|
}
|
|
|
|
metadata := dockerContainerAlertMetadata(host, container, containerName)
|
|
metadata["exitCode"] = container.ExitCode
|
|
metadata["oomKilled"] = true
|
|
metadata["memoryUsageBytes"] = container.MemoryUsage
|
|
metadata["memoryLimitBytes"] = container.MemoryLimit
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
HealthAssessment: &alertspecs.HealthAssessmentEvidence{
|
|
Signal: "docker-container-exit",
|
|
Severity: alertspecs.AlertSeverityCritical,
|
|
Codes: []string{"oom-kill"},
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-oom-kill",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker container '%s' was killed due to out of memory (OOM)", containerName),
|
|
Summary: "Killed due to out of memory (OOM)",
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
})
|
|
|
|
log.Error().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Int64("memoryUsage", container.MemoryUsage).
|
|
Int64("memoryLimit", container.MemoryLimit).
|
|
Msg("Docker container OOM killed")
|
|
} else {
|
|
// Unknown reports from older agents and explicit false reports both fail
|
|
// closed. Exit code 137 proves SIGKILL only; it is not OOM evidence.
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-oom-kill"))
|
|
}
|
|
}
|
|
|
|
// checkDockerContainerMemoryLimit alerts when container approaches its memory limit
|
|
func (m *Manager) checkDockerContainerMemoryLimit(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
// Only check if container is running and has a memory limit
|
|
state := strings.ToLower(strings.TrimSpace(container.State))
|
|
if state != "running" || container.MemoryLimit <= 0 {
|
|
return
|
|
}
|
|
|
|
alertID := fmt.Sprintf("docker-container-memory-limit-%s", resourceID)
|
|
|
|
// Get config values with defaults
|
|
m.mu.RLock()
|
|
warnThreshold := float64(m.config.DockerDefaults.MemoryWarnPct)
|
|
criticalThreshold := float64(m.config.DockerDefaults.MemoryCriticalPct)
|
|
m.mu.RUnlock()
|
|
// NormalizeDockerDefaults owns the factory values; 0 here is the
|
|
// operator's Off for that level.
|
|
if warnThreshold <= 0 && criticalThreshold <= 0 {
|
|
m.clearAlert(buildCanonicalStateID(resourceID, resourceID+"-memory-limit"))
|
|
return
|
|
}
|
|
|
|
// Calculate percentage of limit used
|
|
limitPercent := (float64(container.MemoryUsage) / float64(container.MemoryLimit)) * 100
|
|
|
|
activation := warnThreshold
|
|
if activation <= 0 {
|
|
activation = criticalThreshold
|
|
}
|
|
recovery := activation - 5
|
|
spec, err := buildCanonicalSeverityThresholdSpecWithRecovery(resourceID+"-memory-limit", resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "memory-limit-percent", warnThreshold, criticalThreshold, &recovery, false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container memory limit spec")
|
|
return
|
|
}
|
|
|
|
metadata := dockerContainerAlertMetadata(host, container, containerName)
|
|
metadata["memoryUsageBytes"] = container.MemoryUsage
|
|
metadata["memoryLimitBytes"] = container.MemoryLimit
|
|
metadata["limitPercent"] = limitPercent
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
SeverityThreshold: &alertspecs.SeverityThresholdEvidence{
|
|
Metric: "memory-limit-percent",
|
|
Direction: alertspecs.ThresholdDirectionAbove,
|
|
Observed: limitPercent,
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-memory-limit",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker container '%s' is using %.1f%% of its memory limit (%d MB / %d MB)", containerName, limitPercent, container.MemoryUsage/(1024*1024), container.MemoryLimit/(1024*1024)),
|
|
Summary: fmt.Sprintf("Using %.1f%% of its memory limit (%d MB / %d MB)", limitPercent, container.MemoryUsage/(1024*1024), container.MemoryLimit/(1024*1024)),
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
})
|
|
|
|
if limitPercent >= warnThreshold {
|
|
log.Warn().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Float64("limitPercent", limitPercent).
|
|
Msg("Docker container approaching memory limit")
|
|
}
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerMetricAlerts(resourceID string, metrics ...string) {
|
|
if len(metrics) == 0 {
|
|
metrics = []string{"cpu", "memory", "disk"}
|
|
}
|
|
for _, metric := range metrics {
|
|
m.clearAlert(canonicalMetricStateID(resourceID, metric))
|
|
}
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerUpdateTracking(resourceID, trackingKey string) {
|
|
m.mu.Lock()
|
|
delete(m.dockerUpdateFirstSeen, resourceID)
|
|
delete(m.dockerUpdateLastObserved, resourceID)
|
|
if trackingKey != "" {
|
|
delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)
|
|
delete(m.dockerUpdateLastObserved, trackingKey)
|
|
}
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func dockerUpdateTrackingKeyFromAlert(alert *Alert) string {
|
|
if alert == nil || alert.Metadata == nil {
|
|
return ""
|
|
}
|
|
|
|
metadataString := func(key string) string {
|
|
value, ok := alert.Metadata[key]
|
|
if !ok || value == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(fmt.Sprint(value))
|
|
}
|
|
|
|
host := models.DockerHost{
|
|
ID: metadataString("hostId"),
|
|
DisplayName: metadataString("hostName"),
|
|
Hostname: metadataString("hostHostname"),
|
|
}
|
|
container := models.DockerContainer{
|
|
ID: metadataString("containerId"),
|
|
Name: metadataString("containerName"),
|
|
Image: metadataString("image"),
|
|
}
|
|
|
|
if host.ID == "" && host.DisplayName == "" && host.Hostname == "" &&
|
|
container.ID == "" && container.Name == "" && container.Image == "" {
|
|
return ""
|
|
}
|
|
|
|
return dockerUpdateTrackingKey(host, container)
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerUpdateStateLocked(alert *Alert) {
|
|
if alert == nil {
|
|
return
|
|
}
|
|
|
|
if alert.ResourceID != "" {
|
|
delete(m.dockerUpdateFirstSeen, alert.ResourceID)
|
|
delete(m.dockerUpdateLastObserved, alert.ResourceID)
|
|
}
|
|
if trackingKey := dockerUpdateTrackingKeyFromAlert(alert); trackingKey != "" {
|
|
delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)
|
|
delete(m.dockerUpdateLastObserved, trackingKey)
|
|
}
|
|
}
|
|
|
|
func (m *Manager) clearDockerContainerUpdateAlertsLocked() {
|
|
toClear := make([]string, 0)
|
|
for storageKey, alert := range m.activeAlerts {
|
|
if alert == nil {
|
|
continue
|
|
}
|
|
alertID := effectiveAlertID(alert, storageKey)
|
|
if alert.Type != "docker-container-update" && !strings.HasPrefix(alertID, "docker-container-update-") {
|
|
continue
|
|
}
|
|
m.clearDockerContainerUpdateStateLocked(alert)
|
|
toClear = append(toClear, alertID)
|
|
}
|
|
for _, alertID := range toClear {
|
|
m.clearAlertNoLock(alertID)
|
|
}
|
|
}
|
|
|
|
func (m *Manager) shouldResolveDockerContainerUpdateAlertLocked(alert *Alert) bool {
|
|
if alert == nil {
|
|
return false
|
|
}
|
|
|
|
if allDisabled, _ := m.alertPolicyTypeSwitchesNoLock("app-container"); allDisabled || m.config.DockerDefaults.UpdateAlertDelayHours < 0 {
|
|
m.clearDockerContainerUpdateStateLocked(alert)
|
|
return true
|
|
}
|
|
|
|
containerName := strings.TrimSpace(alert.ResourceName)
|
|
containerID := ""
|
|
hostID := ""
|
|
if alert.Metadata != nil {
|
|
if value, ok := alert.Metadata["containerName"].(string); ok && containerName == "" {
|
|
containerName = value
|
|
}
|
|
if value, ok := alert.Metadata["containerId"].(string); ok {
|
|
containerID = value
|
|
}
|
|
if value, ok := alert.Metadata["hostId"].(string); ok {
|
|
hostID = value
|
|
}
|
|
}
|
|
|
|
if override, exists := m.lookupDockerContainerOverrideNoLock(hostID, containerName, alert.ResourceID); exists && override.Disabled {
|
|
m.clearDockerContainerUpdateStateLocked(alert)
|
|
return true
|
|
}
|
|
if matchesDockerIgnoredPrefix(containerName, containerID, m.config.DockerIgnoredContainerPrefixes) {
|
|
m.clearDockerContainerUpdateStateLocked(alert)
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (m *Manager) touchDockerContainerUpdateAlert(alertID string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if alert, exists := m.getActiveAlertNoLock(alertID); exists && alert != nil {
|
|
alert.LastSeen = time.Now()
|
|
}
|
|
}
|
|
|
|
// checkDockerContainerImageUpdate checks if an image update has been pending for too long
|
|
func (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {
|
|
alertID := fmt.Sprintf("docker-container-update-%s", resourceID)
|
|
canonicalAlertID := buildCanonicalStateID(resourceID, resourceID+"-image-update")
|
|
updateTrackingKey := dockerUpdateTrackingKey(host, container)
|
|
|
|
// Check if update detection is enabled
|
|
m.mu.RLock()
|
|
delayHours := m.config.DockerDefaults.UpdateAlertDelayHours
|
|
m.mu.RUnlock()
|
|
|
|
// Negative value means disabled
|
|
if delayHours < 0 {
|
|
m.clearAlert(canonicalAlertID)
|
|
m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)
|
|
return
|
|
}
|
|
|
|
// Reports refresh tracking activity even when the registry result is cached
|
|
// or unavailable. Do not create a pending condition from unknown evidence.
|
|
m.mu.Lock()
|
|
if _, exists := m.dockerUpdateFirstSeen[resourceID]; exists {
|
|
m.dockerUpdateLastObserved[resourceID] = time.Now()
|
|
}
|
|
if _, exists := m.dockerUpdateFirstSeenByIdentity[updateTrackingKey]; exists {
|
|
m.dockerUpdateLastObserved[updateTrackingKey] = time.Now()
|
|
}
|
|
m.mu.Unlock()
|
|
|
|
// Check if this container has an update status reported
|
|
if container.UpdateStatus == nil {
|
|
// Missing update status means the condition is unknown, not resolved.
|
|
// Preserve any active alert and first-seen tracking until we see an affirmative clear.
|
|
m.touchDockerContainerUpdateAlert(canonicalAlertID)
|
|
return
|
|
}
|
|
|
|
// Check for errors in update detection (don't alert on errors)
|
|
if container.UpdateStatus.Error != "" {
|
|
// A failed update check cannot confirm the pending update has been resolved.
|
|
m.touchDockerContainerUpdateAlert(canonicalAlertID)
|
|
return
|
|
}
|
|
|
|
// "No update" is a comparison result only when the agent knew the local
|
|
// digest. Agents before #2353 compared an empty digest after a failed image
|
|
// inspect and reported no update, which resolved a pending alert and
|
|
// restarted its delay. Without a current digest the result is unknown.
|
|
if !container.UpdateStatus.UpdateAvailable && strings.TrimSpace(container.UpdateStatus.CurrentDigest) == "" {
|
|
m.touchDockerContainerUpdateAlert(canonicalAlertID)
|
|
return
|
|
}
|
|
|
|
// Check if an update is available
|
|
if !container.UpdateStatus.UpdateAvailable {
|
|
// No update available - clear tracking and alert
|
|
m.clearAlert(canonicalAlertID)
|
|
m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)
|
|
return
|
|
}
|
|
|
|
// Update is available - track when we first saw it
|
|
m.mu.Lock()
|
|
firstSeen, exists := m.dockerUpdateFirstSeenByIdentity[updateTrackingKey]
|
|
if !exists {
|
|
firstSeen, exists = m.dockerUpdateFirstSeen[resourceID]
|
|
}
|
|
// Tracking maps are process-local, but an active occurrence survives a
|
|
// restart. Its start is the original pending age, not a new delay window.
|
|
// Otherwise positive reports return below-threshold without refreshing
|
|
// LastSeen, and hourly cleanup can resolve a still-asserted update.
|
|
if active, ok := m.getActiveAlertNoLock(canonicalAlertID); ok && active != nil &&
|
|
active.Type == "docker-container-update" && active.ResourceID == resourceID &&
|
|
!active.StartTime.IsZero() {
|
|
firstSeen, exists = active.StartTime, true
|
|
} else if strings.TrimSpace(container.ID) == "" {
|
|
// A container without an ID used to record its pending update under
|
|
// its host's reference. Keep that age; the host's report cleanup then
|
|
// clears the old occurrence.
|
|
legacyRef := DockerHostResourceID(host.ID)
|
|
if legacy, ok := m.getActiveAlertNoLock(buildCanonicalStateID(legacyRef, legacyRef+"-image-update")); ok &&
|
|
isLegacyDockerHostContainerAlert(legacy, host) && legacy.Type == "docker-container-update" &&
|
|
alertMetadataString(legacy, "containerName") == containerName && !legacy.StartTime.IsZero() {
|
|
firstSeen, exists = legacy.StartTime, true
|
|
}
|
|
}
|
|
if !exists {
|
|
firstSeen = time.Now()
|
|
}
|
|
m.dockerUpdateFirstSeen[resourceID] = firstSeen
|
|
m.dockerUpdateFirstSeenByIdentity[updateTrackingKey] = firstSeen
|
|
m.dockerUpdateLastObserved[resourceID] = time.Now()
|
|
m.dockerUpdateLastObserved[updateTrackingKey] = time.Now()
|
|
m.mu.Unlock()
|
|
|
|
// Check if we've exceeded the delay threshold
|
|
pendingDuration := time.Since(firstSeen)
|
|
threshold := time.Duration(delayHours) * time.Hour
|
|
if pendingDuration < threshold {
|
|
// Not yet time to alert
|
|
log.Debug().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Str("image", container.Image).
|
|
Dur("pending", pendingDuration).
|
|
Dur("threshold", threshold).
|
|
Msg("Container update pending but below alert threshold")
|
|
return
|
|
}
|
|
|
|
// Create or update the alert
|
|
pendingHours := int(pendingDuration.Hours())
|
|
spec, err := buildCanonicalSeverityThresholdSpec(resourceID+"-image-update", resourceID, containerName, unifiedresources.ResourceTypeAppContainer, "image-update-hours", float64(delayHours), 0, false)
|
|
if err != nil {
|
|
log.Warn().
|
|
Err(err).
|
|
Str("resourceID", resourceID).
|
|
Str("container", containerName).
|
|
Msg("Skipping invalid canonical docker container update spec")
|
|
return
|
|
}
|
|
|
|
metadata := dockerContainerAlertMetadata(host, container, containerName)
|
|
metadata["currentDigest"] = container.UpdateStatus.CurrentDigest
|
|
metadata["latestDigest"] = container.UpdateStatus.LatestDigest
|
|
metadata["lastChecked"] = container.UpdateStatus.LastChecked
|
|
metadata["firstSeen"] = firstSeen
|
|
metadata["pendingHours"] = pendingHours
|
|
metadata["thresholdHours"] = delayHours
|
|
|
|
_, _ = m.evaluateCanonicalStatefulAlert(canonicalStatefulAlertParams{
|
|
Spec: spec,
|
|
Evidence: alertspecs.AlertEvidence{
|
|
ObservedAt: time.Now(),
|
|
SeverityThreshold: &alertspecs.SeverityThresholdEvidence{
|
|
Metric: "image-update-hours",
|
|
Direction: alertspecs.ThresholdDirectionAbove,
|
|
Observed: pendingDuration.Hours(),
|
|
},
|
|
},
|
|
AlertID: alertID,
|
|
AlertType: "docker-container-update",
|
|
ResourceID: resourceID,
|
|
ResourceName: containerName,
|
|
Node: nodeName,
|
|
Instance: instanceName,
|
|
Message: fmt.Sprintf("Docker container '%s' has an image update available for %d hours", containerName, pendingHours),
|
|
Summary: fmt.Sprintf("Image update available for %d hours", pendingHours),
|
|
StartTimeOverride: firstSeen,
|
|
Metadata: metadata,
|
|
AddToRecent: true,
|
|
AddToHistory: true,
|
|
})
|
|
|
|
log.Warn().
|
|
Str("container", containerName).
|
|
Str("host", host.DisplayName).
|
|
Str("image", container.Image).
|
|
Int("pendingHours", pendingHours).
|
|
Msg("Docker container has pending image update")
|
|
}
|
|
|
|
func (m *Manager) cleanupDockerContainerAlerts(host models.DockerHost, seen map[string]struct{}) {
|
|
m.cleanupDockerContainerAlertsWithTracking(host, seen, nil)
|
|
}
|
|
|
|
// isLegacyDockerHostContainerAlert reports an alert that a container without
|
|
// an ID on this host raised under the host's own reference, before such
|
|
// containers had a reference of their own. Only the host's connectivity alert
|
|
// belongs there, so no producer refreshes these. The recorded host and empty
|
|
// container ID must match too: an alert restored with a resource ID cut short
|
|
// at a "::" in another host's ID ("<base>::<suffix>") can carry this host's
|
|
// reference.
|
|
func isLegacyDockerHostContainerAlert(alert *Alert, host models.DockerHost) bool {
|
|
hostRef := DockerHostResourceID(host.ID)
|
|
return alert != nil && hostRef != "" && alert.ResourceID == hostRef && alert.Type != "docker-host-offline" &&
|
|
alertMetadataString(alert, "hostId") == strings.TrimSpace(host.ID) && alertMetadataString(alert, "containerId") == ""
|
|
}
|
|
|
|
func (m *Manager) cleanupDockerContainerAlertsWithTracking(host models.DockerHost, seen map[string]struct{}, seenUpdateTracking map[string]struct{}) {
|
|
prefix := fmt.Sprintf("docker:%s/", strings.TrimSpace(host.ID))
|
|
updateTrackingPrefix := dockerUpdateTrackingHostPrefix(host)
|
|
|
|
m.mu.Lock()
|
|
toClear := make([]string, 0)
|
|
for storageKey, alert := range m.activeAlerts {
|
|
alertID := effectiveAlertID(alert, storageKey)
|
|
if isLegacyDockerHostContainerAlert(alert, host) {
|
|
toClear = append(toClear, alertID)
|
|
continue
|
|
}
|
|
if !strings.HasPrefix(alert.ResourceID, prefix) {
|
|
continue
|
|
}
|
|
if _, exists := seen[alert.ResourceID]; exists {
|
|
continue
|
|
}
|
|
toClear = append(toClear, alertID)
|
|
}
|
|
// Removed containers stop being observed, so their in-flight
|
|
// confirmation runs in the core would never see a closing observation.
|
|
for _, resourceID := range m.core.PendingResourceIDs() {
|
|
if strings.HasPrefix(resourceID, prefix) {
|
|
if _, exists := seen[resourceID]; !exists {
|
|
m.core.DropPendingForResource(resourceID)
|
|
}
|
|
}
|
|
}
|
|
// Cleanup update tracking for removed containers
|
|
for resourceID := range m.dockerUpdateFirstSeen {
|
|
if strings.HasPrefix(resourceID, prefix) {
|
|
if _, exists := seen[resourceID]; !exists {
|
|
delete(m.dockerUpdateFirstSeen, resourceID)
|
|
delete(m.dockerUpdateLastObserved, resourceID)
|
|
}
|
|
}
|
|
}
|
|
if seenUpdateTracking != nil {
|
|
for trackingKey := range m.dockerUpdateFirstSeenByIdentity {
|
|
if !strings.HasPrefix(trackingKey, updateTrackingPrefix) {
|
|
continue
|
|
}
|
|
if _, exists := seenUpdateTracking[trackingKey]; !exists {
|
|
delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)
|
|
delete(m.dockerUpdateLastObserved, trackingKey)
|
|
}
|
|
}
|
|
}
|
|
m.mu.Unlock()
|
|
|
|
for _, alertID := range toClear {
|
|
m.clearAlert(alertID)
|
|
}
|
|
}
|
|
|
|
func (m *Manager) clearDockerHostContainerAlerts(host models.DockerHost) {
|
|
prefix := fmt.Sprintf("docker:%s/", strings.TrimSpace(host.ID))
|
|
updateTrackingPrefix := dockerUpdateTrackingHostPrefix(host)
|
|
|
|
m.mu.Lock()
|
|
toClear := make([]string, 0)
|
|
for storageKey, alert := range m.activeAlerts {
|
|
alertID := effectiveAlertID(alert, storageKey)
|
|
if strings.HasPrefix(alert.ResourceID, prefix) || isLegacyDockerHostContainerAlert(alert, host) {
|
|
toClear = append(toClear, alertID)
|
|
}
|
|
}
|
|
for _, resourceID := range m.core.PendingResourceIDs() {
|
|
if strings.HasPrefix(resourceID, prefix) {
|
|
m.core.DropPendingForResource(resourceID)
|
|
}
|
|
}
|
|
for resourceID := range m.dockerRestartTracking {
|
|
if strings.HasPrefix(resourceID, prefix) {
|
|
delete(m.dockerRestartTracking, resourceID)
|
|
}
|
|
}
|
|
for resourceID := range m.dockerUpdateFirstSeen {
|
|
if strings.HasPrefix(resourceID, prefix) {
|
|
delete(m.dockerUpdateFirstSeen, resourceID)
|
|
delete(m.dockerUpdateLastObserved, resourceID)
|
|
}
|
|
}
|
|
for trackingKey := range m.dockerUpdateFirstSeenByIdentity {
|
|
if strings.HasPrefix(trackingKey, updateTrackingPrefix) {
|
|
delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)
|
|
delete(m.dockerUpdateLastObserved, trackingKey)
|
|
}
|
|
}
|
|
m.mu.Unlock()
|
|
|
|
for _, alertID := range toClear {
|
|
m.clearAlert(alertID)
|
|
}
|
|
}
|