Skip to content

Commit

Permalink
add the shell output check feature
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 18, 2022
1 parent b630482 commit 471aa97
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 30 additions & 3 deletions probe/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion resources/scripts/proxy.curl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

export http_proxy=${1} https_proxy=${1} all_proxy=${1}

curl ${2}
curl -sI ${2}

0 comments on commit 471aa97

Please sign in to comment.