From dd0b8e3f38b2dc9f05296e7fe5c612d9e601c03f Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 1 Oct 2019 08:26:07 -0700 Subject: [PATCH] Added ability to run commands in a shell Diverges a small amount from the Python version for this. --- alert.go | 12 ++++++++++-- config.go | 2 ++ monitor.go | 6 ++++-- util.go | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/alert.go b/alert.go index 3f998c6..04c63a0 100644 --- a/alert.go +++ b/alert.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "fmt" + "log" "os/exec" "text/template" "time" @@ -12,7 +14,7 @@ type Alert struct { Command []string CommandShell string `yaml:"command_shell"` commandTemplate []template.Template - commandShellTemplate template.Template + commandShellTemplate *template.Template } func (alert Alert) IsValid() bool { @@ -43,8 +45,14 @@ func (alert Alert) Send(notice AlertNotice) { } else if alert.commandShellTemplate != nil { var commandBuffer bytes.Buffer err := alert.commandShellTemplate.Execute(&commandBuffer, notice) - // TODO handle error + if err != nil { + panic(err) + } cmd = exec.Command(commandBuffer.String()) + + output, err := cmd.CombinedOutput() + log.Printf("Check %s\n---\n%s\n---", alert.Name, string(output)) + } else { panic("No template?") } diff --git a/config.go b/config.go index b22bf10..9356a63 100644 --- a/config.go +++ b/config.go @@ -18,6 +18,8 @@ func LoadConfig(filePath string) (config Config) { if err != nil { panic(err) } + + // TODO: Decide if this is better expanded here, or only when executing env_expanded := os.ExpandEnv(string(data)) err = yaml.Unmarshal([]byte(env_expanded), &config) diff --git a/monitor.go b/monitor.go index 9874994..cfa2546 100644 --- a/monitor.go +++ b/monitor.go @@ -10,7 +10,7 @@ type Monitor struct { // Config values Name string Command []string - CommandShell string `yaml:"command_shell` + CommandShell string `yaml:"command_shell"` AlertDown []string `yaml:"alert_down"` AlertUp []string `yaml:"alert_up"` CheckInterval float64 `yaml:"check_interval"` @@ -45,7 +45,9 @@ func (monitor *Monitor) Check() bool { cmd = exec.Command(monitor.Command[0], monitor.Command[1:]...) } else { // TODO: Handle a command shell as well. This is untested - cmd = exec.Command(monitor.CommandShell) + + //cmd = exec.Command("sh", "-c", "echo \"This is a test of the command system\"") + cmd = ShellCommand(monitor.CommandShell) } output, err := cmd.CombinedOutput() diff --git a/util.go b/util.go index 1110061..787ac43 100644 --- a/util.go +++ b/util.go @@ -1,3 +1,24 @@ package main +import ( + "log" + "os/exec" + "strings" +) +/// escapeCommandShell accepts a command to be executed by a shell and escapes it +func escapeCommandShell(command string) string { + // Remove extra spaces and newlines from ends + command = strings.TrimSpace(command) + // TODO: Not sure if this part is actually needed. Should verify + // Escape double quotes since this will be passed in as an argument + command = strings.Replace(command, `"`, `\"`, -1) + return command +} + +/// ShellCommand takes a string and executes it as a command using `sh` +func ShellCommand(command string) *exec.Cmd { + shellCommand := []string{"sh", "-c", escapeCommandShell(command)} + log.Printf("Command: %v", shellCommand) + return exec.Command(shellCommand[0], shellCommand[1:]...) +}