Skip to content

Commit

Permalink
fix using arguments in aliased checks (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Dec 17, 2023
1 parent de3d366 commit 75ca6a0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
This file documents the revision history for the SNClient+ agent.

next:
- fix using arguments in aliased checks (#69)

0.15 Fri Dec 15 23:31:08 CET 2023
- improve counter memory consumption
- change internal wmi library
Expand Down
34 changes: 29 additions & 5 deletions pkg/snclient/check_alias.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package snclient

import "context"
import (
"context"
"fmt"
"strings"

"pkg/utils"
)

type CheckAlias struct {
noCopy noCopy
Expand All @@ -15,7 +21,7 @@ func (a *CheckAlias) Build() *CheckData {
}
}

func (a *CheckAlias) Check(ctx context.Context, snc *Agent, check *CheckData, _ []Argument) (*CheckResult, error) {
func (a *CheckAlias) Check(ctx context.Context, snc *Agent, check *CheckData, _ []Argument) (res *CheckResult, err error) {
userArgs := check.rawArgs
var statusResult *CheckResult
switch {
Expand All @@ -30,9 +36,27 @@ func (a *CheckAlias) Check(ctx context.Context, snc *Agent, check *CheckData, _
Output: "Exception processing request: Request contained illegal characters (check the allow nasty characters option).",
}
default:
args := a.args
args = append(args, userArgs...)
statusResult = snc.runCheck(ctx, a.command, args)
cmdArgs := a.args
argStr := strings.Join(a.args, " ")
if strings.Contains(argStr, "$ARG") {
log.Debugf("command before macros expanded: %s %s", a.command, argStr)
macros := map[string]string{
"ARGS": strings.Join(userArgs, " "),
}
for i := range userArgs {
macros[fmt.Sprintf("ARG%d", i+1)] = check.rawArgs[i]
}

replacedStr := ReplaceRuntimeMacros(strings.Join(a.args, " "), macros)
cmdArgs = utils.Tokenize(replacedStr)
cmdArgs, err = utils.TrimQuotesAll(cmdArgs)
if err != nil {
return nil, fmt.Errorf("argument error: %s", err.Error())
}

log.Debugf("command after macros expanded: %s %s", a.command, replacedStr)
}
statusResult = snc.runCheck(ctx, a.command, cmdArgs)
}

statusResult.ParsePerformanceDataFromOutputCond(a.command, a.config)
Expand Down
18 changes: 18 additions & 0 deletions pkg/snclient/check_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ allow arguments = yes
command = check_cpu warn=load=101 crit=load=102
allow arguments = yes
nasty characters = []{}
[/settings/external scripts/alias/alias_dummy]
command = check_dummy $ARG1$ "$ARG2$ $ARG3$"
allow arguments = yes
[/settings/external scripts/alias/alias_dummy2]
command = check_dummy $ARGS$
allow arguments = yes
`
snc := StartTestAgent(t, config)

Expand Down Expand Up @@ -65,5 +73,15 @@ nasty characters = []{}
res = snc.RunCheck("alias_cpu4", []string{"filter=core!=$"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")

// dummy check with arguments
res = snc.RunCheck("alias_dummy", []string{"0", "test 123", "456"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")
assert.Equalf(t, "test 123 456", string(res.BuildPluginOutput()), "plugin output matches")

// dummy check with arguments
res = snc.RunCheck("alias_dummy2", []string{"0", "test 123", "456"})
assert.Equalf(t, CheckExitOK, res.State, "state OK")
assert.Equalf(t, "test 123 456", string(res.BuildPluginOutput()), "plugin output matches")

StopTestAgent(t, snc)
}
2 changes: 1 addition & 1 deletion pkg/snclient/task_external_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (e *ExternalScriptsHandler) registerAliases(conf *Config) error {
cmdConf := conf.Section(sectionName)
if command, ok := cmdConf.GetString("command"); ok {
f := utils.Tokenize(command)
log.Tracef("registered wrapped script: %s -> %s", name, command)
log.Tracef("registered alias script: %s -> %s", name, command)
AvailableChecks[name] = CheckEntry{name, func() CheckHandler { return &CheckAlias{command: f[0], args: f[1:], config: cmdConf} }}
} else {
return fmt.Errorf("missing command in alias script %s", name)
Expand Down

0 comments on commit 75ca6a0

Please sign in to comment.