diff --git a/README.md b/README.md index 4714e576..f91aa8b5 100644 --- a/README.md +++ b/README.md @@ -216,10 +216,12 @@ notify: shell: # EaseProbe set the environment variables - # (see the example: resources/scripts/notify/notify.sh) - name: "shell alert service" - command: "/bin/bash" + cmd: "/bin/bash" args: - "-c" - "/path/to/script.sh" + env: # set the env to the notification command + - "EASEPROBE=1" ``` Check the [Notification Configuration](#38-notification-configuration) to see how to configure it. @@ -532,6 +534,8 @@ shell: - "-h" - "127.0.0.1" - "ping" + clean_env: true # Do not pass the OS environment variables to the command + # default: false env: # set the `REDISCLI_AUTH` environment variable for redis password - "REDISCLI_AUTH=abc123" @@ -799,10 +803,15 @@ notify: # (see the example: resources/scripts/notify/notify.sh) shell: - name: "shell alert service" - command: "/bin/bash" + cmd: "/bin/bash" args: - "-c" - "/path/to/script.sh" + clean_env: true # Do not pass the OS environment variables to the command + # default: false + env: # set the env to the notification command + - "EASEPROBE=1" + - "KEY=Value" ``` **Note**: All of the notifications can have the following optional configuration. diff --git a/notify/shell/shell.go b/notify/shell/shell.go index 6d085960..52416ea5 100644 --- a/notify/shell/shell.go +++ b/notify/shell/shell.go @@ -38,6 +38,9 @@ type NotifyConfig struct { Cmd string `yaml:"cmd"` Args []string `yaml:"args"` + Env []string `yaml:"env"` + + CleanEnv bool `yaml:"clean_env"` } // Config is the config for shell probe @@ -67,7 +70,13 @@ func (c *NotifyConfig) RunShell(title, msg string) error { env = append(env, fmt.Sprintf("%s=%s", k, v)) } cmd.Stdin = strings.NewReader(envMap["EASEPROBE_CSV"]) - cmd.Env = append(os.Environ(), env...) + if c.CleanEnv == false { + cmd.Env = append(os.Environ(), env...) + } else { + log.Infof("[%s / %s] clean the environment variables", c.NotifyKind, c.NotifyName) + cmd.Env = env + } + cmd.Env = append(cmd.Env, c.Env...) output, err := cmd.CombinedOutput() if err != nil { return err diff --git a/probe/shell/shell.go b/probe/shell/shell.go index faaa3b5c..ba8dc13e 100644 --- a/probe/shell/shell.go +++ b/probe/shell/shell.go @@ -36,6 +36,7 @@ type Shell struct { Command string `yaml:"cmd"` Args []string `yaml:"args,omitempty"` Env []string `yaml:"env,omitempty"` + CleanEnv bool `yaml:"clean_env,omitempty"` Contain string `yaml:"contain,omitempty"` NotContain string `yaml:"not_contain,omitempty"` @@ -66,7 +67,12 @@ func (s *Shell) DoProbe() (bool, string) { defer cancel() cmd := exec.CommandContext(ctx, s.Command, s.Args...) - cmd.Env = append(os.Environ(), s.Env...) + if s.CleanEnv == false { + cmd.Env = append(os.Environ(), s.Env...) + } else { + log.Infof("[%s / %s] clean the environment variables", s.ProbeKind, s.ProbeName) + cmd.Env = s.Env + } output, err := cmd.CombinedOutput() status := true diff --git a/probe/shell/shell_test.go b/probe/shell/shell_test.go index 5741792b..09c0f339 100644 --- a/probe/shell/shell_test.go +++ b/probe/shell/shell_test.go @@ -19,6 +19,7 @@ package shell import ( "context" + "os" "os/exec" "reflect" "testing" @@ -78,5 +79,30 @@ func TestShell(t *testing.T) { status, message = s.DoProbe() assert.False(t, status) assert.NotContains(t, message, "ExitCode(null)") +} + +func TestEnv(t *testing.T) { + s := &Shell{ + DefaultProbe: base.DefaultProbe{ProbeName: "dummy shell"}, + Command: "env", + Args: []string{}, + Env: []string{}, + } + s.Config(global.ProbeSettings{}) + + err := os.Setenv("EASEPROBE", "1") + assert.Nil(t, err) + s.Contain = "EASEPROBE=1" + status, message := s.DoProbe() + assert.True(t, status) + assert.Contains(t, message, "Successfully") + + s.CleanEnv = true + s.Env = []string{"env1=value1"} + s.Contain = "env1=value1" + s.NotContain = "EASEPROBE=1" + status, message = s.DoProbe() + assert.True(t, status) + assert.Contains(t, message, "Successfully") } diff --git a/resources/scripts/notify/notify.sh b/resources/scripts/notify/notify.sh index 1ecd41b1..7ecba8e8 100755 --- a/resources/scripts/notify/notify.sh +++ b/resources/scripts/notify/notify.sh @@ -26,4 +26,6 @@ fi echo "${EASEPROBE_JSON}" echo "----------------------------------------------------" echo "${EASEPROBE_CSV}" +echo "----------------------------------------------------" +env echo "----------------------------------------------------" \ No newline at end of file