diff --git a/misc.go b/misc.go index f63ac83..7a746f9 100644 --- a/misc.go +++ b/misc.go @@ -29,6 +29,23 @@ func command(parameters []string) (exitCode int, exitMessage string) { return 1, exitMessage } +// commandOutputMatches checks to see if a command's combined output matches a +// given regexp +func commandOutputMatches(parameters []string) (exitCode int, exitMessage string) { + toExec := parameters[0] + re := parseUserRegex(parameters[1]) + cmd := exec.Command("bash", "-c", toExec) + out, err := cmd.CombinedOutput() + if err != nil { + execError(cmd, string(out), err) + } + if re.Match(out) { + return 0, "" + } + msg := "Command output did not match regexp" + return genericError(msg, re.String(), []string{string(out)}) +} + // running checks if a process is running using `ps aux`, and searching for the // process name, excluding this process (in case the process name is in the JSON // file name) diff --git a/register.go b/register.go index fe0bce6..d050bfa 100644 --- a/register.go +++ b/register.go @@ -7,6 +7,7 @@ func registerCheck(name string, work Worker, numParams int) { func registerChecks() { registerCheck("command", command, 1) + registerCheck("commandoutputmatches", commandOutputMatches, 2) registerCheck("running", running, 1) registerCheck("phpconfig", phpConfig, 2) registerCheck("file", file, 1) diff --git a/samples/misc-fail.json b/samples/misc-fail.json index 5db00ba..1416924 100644 --- a/samples/misc-fail.json +++ b/samples/misc-fail.json @@ -39,6 +39,14 @@ { "Check" : "cpuUsage", "Parameters" : ["1"] + }, + { + "Check" : "command", + "Parameters" : ["sleep fail"] + }, + { + "Check" : "commandOutputMatches", + "Parameters" : ["echo doesntwork", "\\d+oesnt\\work"] } ] } diff --git a/samples/misc.json b/samples/misc.json index dd786b1..69921dd 100644 --- a/samples/misc.json +++ b/samples/misc.json @@ -38,6 +38,14 @@ { "Check" : "cpuUsage", "Parameters" : ["95"] + }, + { + "Check" : "command", + "Parameters" : ["sleep .00001"] + }, + { + "Check" : "commandOutputMatches", + "Parameters" : ["echo works", "\\w[aeiou]+(y)*r[kl]s"] } ] }