Skip to content

Commit

Permalink
Added command output regexp check
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
langston-barrett committed Jun 19, 2015
1 parent 183c565 commit 8bd74d0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions samples/misc-fail.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
{
"Check" : "cpuUsage",
"Parameters" : ["1"]
},
{
"Check" : "command",
"Parameters" : ["sleep fail"]
},
{
"Check" : "commandOutputMatches",
"Parameters" : ["echo doesntwork", "\\d+oesnt\\work"]
}
]
}
8 changes: 8 additions & 0 deletions samples/misc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
{
"Check" : "cpuUsage",
"Parameters" : ["95"]
},
{
"Check" : "command",
"Parameters" : ["sleep .00001"]
},
{
"Check" : "commandOutputMatches",
"Parameters" : ["echo works", "\\w[aeiou]+(y)*r[kl]s"]
}
]
}

0 comments on commit 8bd74d0

Please sign in to comment.