Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions pkg/snclient/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/snclient/commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 31 additions & 9 deletions pkg/snclient/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ 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 ...

# naemon/nagios (monitoring-plugins) plugin compatible output and exit code
snclient run ...

# do is an alias for run
snclient do ...

Examples:

# start query prompt:
Expand All @@ -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" {
Expand All @@ -66,6 +71,23 @@ snclient do check_files path=/tmp crit='count > 100'
},
}
rootCmd.AddCommand(testCmd)

runCmd := &cobra.Command{
Use: "run <cmd> [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 <cmd> [args]",
Hidden: true,
Long: runCmd.Long,
Run: runCmd.Run,
})
}

func testPrompt(cmd *cobra.Command, snc *snclient.Agent) {
Expand Down
37 changes: 23 additions & 14 deletions pkg/snclient/snclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
Loading