From 471aa97eba5241a7169e8390eaecd04545e25ced Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Fri, 18 Mar 2022 12:28:49 +0800 Subject: [PATCH] add the shell output check feature --- probe/shell/shell.go | 33 ++++++++++++++++++++++++++++++--- resources/scripts/proxy.curl.sh | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/probe/shell/shell.go b/probe/shell/shell.go index 34de11a3..706597cf 100644 --- a/probe/shell/shell.go +++ b/probe/shell/shell.go @@ -17,9 +17,11 @@ const Kind string = "shell" // Shell implements a config for shell command (os.Exec) type Shell struct { - Name string `yaml:"name"` - Command string `yaml:"cmd"` - Args []string `yaml:"args,omitempty"` + Name string `yaml:"name"` + Command string `yaml:"cmd"` + Args []string `yaml:"args,omitempty"` + Contain string `yaml:"contain,omitempty"` + NotContain string `yaml:"not_contain,omitempty"` //Control Options Timeout time.Duration `yaml:"timeout,omitempty"` @@ -103,6 +105,13 @@ func (s *Shell) Probe() probe.Result { log.Errorf(s.result.Message) status = probe.StatusDown } + log.Debugf("[%s] - %s", s.Kind(), s.CommandLine()) + log.Debugf("[%s] - %s", s.Kind(), outputFmt(output)) + + if err := s.CheckOutput(output); err != nil { + log.Errorf("[%s] - %v", s.Kind(), err) + status = probe.StatusDown + } s.result.PreStatus = s.result.Status s.result.Status = status @@ -111,6 +120,24 @@ func (s *Shell) Probe() probe.Result { return *s.result } +// CheckOutput checks the output text, +// - if it contains a configured string then return nil +// - if it does not contain a configured string then return nil +func (s *Shell) CheckOutput(output []byte) error { + + str := string(output) + if len(s.Contain) > 0 && !strings.Contains(str, s.Contain) { + + return fmt.Errorf("the output does not contain [%s]", s.Contain) + } + + if len(s.NotContain) > 0 && strings.Contains(str, s.NotContain) { + return fmt.Errorf("the output contains [%s]", s.NotContain) + + } + return nil +} + // CommandLine will return the whole command line which includes command and all arguments func (s *Shell) CommandLine() string { result := s.Command diff --git a/resources/scripts/proxy.curl.sh b/resources/scripts/proxy.curl.sh index 5e17cc9e..627fe610 100755 --- a/resources/scripts/proxy.curl.sh +++ b/resources/scripts/proxy.curl.sh @@ -2,4 +2,4 @@ export http_proxy=${1} https_proxy=${1} all_proxy=${1} -curl ${2} +curl -sI ${2}