This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from jaxxstorm/api_support
Api support
- Loading branch information
Showing
4 changed files
with
123 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package api | ||
|
||
import "github.com/parnurzeal/gorequest" | ||
|
||
func SendResult(url string, json string, username string, password string) (statusCode int, body string, status string) { | ||
|
||
// set up a new request | ||
request := gorequest.New() | ||
|
||
if username != "" && password != "" { | ||
request.SetBasicAuth(username, password) | ||
} | ||
|
||
// set up the url to post to | ||
resp, body, _ := request.Post(url). | ||
Send(json). | ||
End() | ||
|
||
return resp.StatusCode, body, resp.Status | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"os/exec" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func RunCommand(cmdName string, cmdArgs []string, timeout int) (int, string) { | ||
|
||
// the command we're going to run | ||
cmd := exec.Command(cmdName, cmdArgs...) | ||
|
||
// assign vars for output and stderr | ||
var output bytes.Buffer | ||
var stderr bytes.Buffer | ||
|
||
// get the stdout and stderr and assign to pointers | ||
cmd.Stderr = &stderr | ||
cmd.Stdout = &output | ||
|
||
// Start the command | ||
if err := cmd.Start(); err != nil { | ||
log.Fatalf("Command not found: %s", cmdName) | ||
} | ||
|
||
timer := time.AfterFunc(time.Second*time.Duration(timeout), func() { | ||
if timeout > 0 { | ||
err := cmd.Process.Kill() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
}) | ||
|
||
// Here's the good stuff | ||
if err := cmd.Wait(); err != nil { | ||
if exiterr, ok := err.(*exec.ExitError); ok { | ||
// Command ! exit 0, capture it | ||
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { | ||
// Check it's nagios compliant | ||
if status.ExitStatus() == 1 || status.ExitStatus() == 2 || status.ExitStatus() == 3 { | ||
return status.ExitStatus(), stderr.String() | ||
} else { | ||
// If not, force an exit code 2 | ||
return 2, stderr.String() | ||
} | ||
} | ||
} else { | ||
log.Fatalf("cmd.Wait: %v", err) | ||
} | ||
timer.Stop() | ||
} | ||
// We didn't get captured, continue! | ||
return 0, output.String() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters