Added ability to run commands in a shell
Diverges a small amount from the Python version for this.
This commit is contained in:
parent
342b12432e
commit
dd0b8e3f38
12
alert.go
12
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?")
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
21
util.go
21
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:]...)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user