From 2a24abaa31dce2104eac6e18774c0ad23adc1ef6 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Mon, 6 Feb 2023 09:18:37 -0500 Subject: [PATCH] Make health check interval configurable --- cmd/certspotter/main.go | 3 +++ monitor/config.go | 5 +++++ monitor/daemon.go | 5 ++--- monitor/healthcheck.go | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/certspotter/main.go b/cmd/certspotter/main.go index cadcd02..879bef5 100644 --- a/cmd/certspotter/main.go +++ b/cmd/certspotter/main.go @@ -110,6 +110,7 @@ func main() { var flags struct { batchSize int // TODO-4: respect this option email []string + healthcheck time.Duration logs string noSave bool script string @@ -122,6 +123,7 @@ func main() { } flag.IntVar(&flags.batchSize, "batch_size", 1000, "Max number of entries to request per call to get-entries (advanced)") flag.Func("email", "Email address to contact when matching certificate is discovered (repeatable)", appendFunc(&flags.email)) + flag.DurationVar(&flags.healthcheck, "healthcheck", 24*time.Hour, "How frequently to perform a healt check") flag.StringVar(&flags.logs, "logs", defaultLogList, "File path or URL of JSON list of logs to monitor") flag.BoolVar(&flags.noSave, "no_save", false, "Do not save a copy of matching certificates in state directory") flag.StringVar(&flags.script, "script", "", "Program to execute when a matching certificate is discovered") @@ -152,6 +154,7 @@ func main() { Script: flags.script, Email: flags.email, Stdout: flags.stdout, + HealthCheckInterval: flags.healthcheck, } if flags.watchlist == "-" { diff --git a/monitor/config.go b/monitor/config.go index 46eb100..47b404c 100644 --- a/monitor/config.go +++ b/monitor/config.go @@ -9,6 +9,10 @@ package monitor +import ( + "time" +) + type Config struct { LogListSource string StateDir string @@ -19,4 +23,5 @@ type Config struct { Script string Email []string Stdout bool + HealthCheckInterval time.Duration } diff --git a/monitor/daemon.go b/monitor/daemon.go index e1bff0c..4673efc 100644 --- a/monitor/daemon.go +++ b/monitor/daemon.go @@ -23,7 +23,6 @@ import ( const ( reloadLogListIntervalMin = 30 * time.Minute reloadLogListIntervalMax = 90 * time.Minute - healthCheckInterval = 24 * time.Hour ) func randomDuration(min, max time.Duration) time.Duration { @@ -50,7 +49,7 @@ type daemon struct { } func (daemon *daemon) healthCheck(ctx context.Context) error { - if time.Since(daemon.logsLoadedAt) >= healthCheckInterval { + if time.Since(daemon.logsLoadedAt) >= daemon.config.HealthCheckInterval { if err := notify(ctx, daemon.config, &staleLogListEvent{ Source: daemon.config.LogListSource, LastSuccess: daemon.logsLoadedAt, @@ -135,7 +134,7 @@ func (daemon *daemon) run(ctx context.Context) error { reloadLogListTicker := time.NewTicker(reloadLogListInterval()) defer reloadLogListTicker.Stop() - healthCheckTicker := time.NewTicker(healthCheckInterval) + healthCheckTicker := time.NewTicker(daemon.config.HealthCheckInterval) defer healthCheckTicker.Stop() for ctx.Err() == nil { diff --git a/monitor/healthcheck.go b/monitor/healthcheck.go index 7b83194..7645798 100644 --- a/monitor/healthcheck.go +++ b/monitor/healthcheck.go @@ -35,7 +35,7 @@ func healthCheckLog(ctx context.Context, config *Config, ctlog *loglist.Log) err return fmt.Errorf("error loading state file: %w", err) } - if time.Since(state.LastSuccess) < healthCheckInterval { + if time.Since(state.LastSuccess) < config.HealthCheckInterval { return nil }