From b5b68c2a8a68ac07c8f309050b488c7388a29205 Mon Sep 17 00:00:00 2001 From: lorenzg Date: Tue, 17 Nov 2026 13:38:48 +0100 Subject: [PATCH] skip background tasks in run mode --- pkg/snclient/commands/root.go | 19 ++++++++++++---- pkg/snclient/commands/server.go | 3 ++- pkg/snclient/commands/test.go | 40 +++++++++++++++++++++++++-------- pkg/snclient/snclient.go | 37 ++++++++++++++++++------------ 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/pkg/snclient/commands/root.go b/pkg/snclient/commands/root.go index 16466e05..f9cd64d0 100644 --- a/pkg/snclient/commands/root.go +++ b/pkg/snclient/commands/root.go @@ -20,13 +20,23 @@ func NewRootCmd() *cobra.Command { Use: "snclient [global flags] [command]", Short: "Multi-platform monitoring agent for Naemon and Prometheus.", Long: `SNClient is a general-purpose monitoring agent for multiple platforms. -It aims to provide basic fault monitoring and metrics. Add your own scripts and checks to extend it.`, - Example: ` * Start the server +It provides monitoring and metrics. Add your own scripts and checks to extend it. + +Without a command, SNClient starts in 'server' mode. +Use 'snclient test' for a query prompt or a single check with human-readable output. +Use 'snclient run' for a single check without any background tasks.`, + Example: ` * Start the server (default) %> snclient server * Start as a background daemon %> snclient daemon + * Open the test query prompt + %> snclient test + + * Run a single check without background tasks + %> snclient run check_http -H localhost -p 443 -S + * Show the SNClient version and compiled-in plugins: %> snclient -vV @@ -240,8 +250,9 @@ func injectDoubleSlashAfterRunCmd(rootCmd *cobra.Command) { return } - // only used in the "test" command - if cmd.Name() != "test" { + switch cmd.Name() { + case "test", "run", "do": + default: return } diff --git a/pkg/snclient/commands/server.go b/pkg/snclient/commands/server.go index e71548ab..7a84a691 100644 --- a/pkg/snclient/commands/server.go +++ b/pkg/snclient/commands/server.go @@ -8,9 +8,10 @@ import ( func init() { serverCmd := &cobra.Command{ Use: "server", - Short: "Start the agent", + Short: "Start the agent (default)", Long: `Server mode starts the agent in the foreground, connected to the terminal. The agent prints all logs to stdout unless flags specify another destination. +This is the default mode when you omit the command. `, GroupID: "daemon", Run: func(_ *cobra.Command, _ []string) { diff --git a/pkg/snclient/commands/test.go b/pkg/snclient/commands/test.go index 4b846212..875b06f9 100644 --- a/pkg/snclient/commands/test.go +++ b/pkg/snclient/commands/test.go @@ -13,14 +13,16 @@ import ( func init() { testCmd := &cobra.Command{ - Use: "test [cmd]", - Aliases: []string{"run", "do"}, - Short: "Start test mode or run a query", + Use: "test [cmd]", + Short: "Start test mode or run a query", Long: `Use test mode to test queries manually. If you provide a query, SNClient prints its result once. Without a command, SNClient starts a query prompt. -The command aliases change the output format and exit code: +test and run use different startup paths and output formats. +Both load configuration, aliases, and external scripts. Neither starts listeners. +test initializes configured tasks and starts the memory watcher if enabled. +run executes one check without background task initialization or the memory watcher. # human readable output snclient test ... @@ -28,9 +30,6 @@ snclient test ... # naemon/nagios (monitoring-plugins) plugin compatible output and exit code snclient run ... -# do is an alias for run -snclient do ... - Examples: # start query prompt: @@ -43,13 +42,19 @@ snclient test check_load filter=none crit=none warn=none snclient test -vv check_memory # run check_files directly as naemon check -snclient do check_files path=/tmp crit='count > 100' +snclient run check_files path=/tmp crit='count > 100' + +# run an HTTP check without background tasks +snclient run check_http -H localhost -p 443 -S `, Run: func(cmd *cobra.Command, args []string) { agentFlags.Mode = snclient.ModeOneShot + agentFlags.SkipBackgroundTasks = cmd.CalledAs() != "test" setInteractiveStdoutLogger() snc := snclient.NewAgent(agentFlags) - _ = snc.StartTask("ProcessMemoryWatcher") + if !agentFlags.SkipBackgroundTasks { + _ = snc.StartTask("ProcessMemoryWatcher") + } if len(args) == 0 { if cmd.CalledAs() != "test" { @@ -66,6 +71,23 @@ snclient do check_files path=/tmp crit='count > 100' }, } rootCmd.AddCommand(testCmd) + + runCmd := &cobra.Command{ + Use: "run [args]", + Short: "Run one check without background tasks", + Long: `Run one check and return plugin output and its exit code. +SNClient loads configuration, aliases, and external scripts. +It skips background task initialization and the memory watcher. It does not start listeners.`, + Example: " snclient run check_http -H localhost -p 443 -S", + Run: testCmd.Run, + } + rootCmd.AddCommand(runCmd) + rootCmd.AddCommand(&cobra.Command{ + Use: "do [args]", + Hidden: true, + Long: runCmd.Long, + Run: runCmd.Run, + }) } func testPrompt(cmd *cobra.Command, snc *snclient.Agent) { diff --git a/pkg/snclient/snclient.go b/pkg/snclient/snclient.go index b0b3a7e3..70809745 100644 --- a/pkg/snclient/snclient.go +++ b/pkg/snclient/snclient.go @@ -171,20 +171,21 @@ func (*noCopy) Lock() {} func (*noCopy) Unlock() {} type AgentFlags struct { - ConfigFiles []string - Verbose int - Quiet bool - Help bool - Version bool - Mode MainRunMode - LogLevel string - LogFormat string - LogFile string - Pidfile string - ProfilePort string - ProfileCPU string - ProfileMem string - DeadlockTimeout int + ConfigFiles []string + Verbose int + Quiet bool + Help bool + Version bool + Mode MainRunMode + SkipBackgroundTasks bool + LogLevel string + LogFormat string + LogFile string + Pidfile string + ProfilePort string + ProfileCPU string + ProfileMem string + DeadlockTimeout int } type Agent struct { @@ -650,6 +651,14 @@ func (snc *Agent) initModules(name string, loadable []*LoadableModule, runSet *A conf := runSet.config for _, entry := range loadable { + // Task initialization can collect metrics. Keep only check registration for run. + if snc.Flags.SkipBackgroundTasks && name == "tasks" { + switch entry.ModuleKey { + case "CheckAlias", "CheckExternalScripts": + default: + continue + } + } enabled, ok, err := entry.IsEnabled(conf) switch { case err != nil: