Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env and clean_env for shell notification and probe #136

Merged
merged 3 commits into from
Jun 9, 2022
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion notify/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion probe/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions probe/shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package shell

import (
"context"
"os"
"os/exec"
"reflect"
"testing"
Expand Down Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions resources/scripts/notify/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ fi
echo "${EASEPROBE_JSON}"
echo "----------------------------------------------------"
echo "${EASEPROBE_CSV}"
echo "----------------------------------------------------"
env
echo "----------------------------------------------------"