Skip to content

Commit

Permalink
Merge pull request #99 from NETWAYS/feature/health-crit
Browse files Browse the repository at this point in the history
Add CLI flag to override exit code if API is unreachable
  • Loading branch information
martialblog authored Feb 23, 2024
2 parents 376bacd + a45255a commit d9a57cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Flags:
--heap-usage-threshold-crit string The percentage relative to the heap size limit on which to be a critical result (default "80")
--cpu-usage-threshold-warn string The percentage of CPU usage on which to be a warning result (default "100")
--cpu-usage-threshold-crit string The percentage of CPU usage on which to be a critical result (default "100")
--unreachable-state int Exit with specified code if unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default 3)
-h, --help help for health
```
Expand Down
6 changes: 5 additions & 1 deletion cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type HealthConfig struct {
HeapUseThresCritical string
CPUUseThresWarning string
CPUUseThresCritical string
UnreachableExitCode int
}

// To store the parsed CLI parameters.
Expand Down Expand Up @@ -164,7 +165,7 @@ var healthCmd = &cobra.Command{
resp, err := c.Client.Get(u)

if err != nil {
check.ExitError(err)
check.ExitRaw(cliHealthConfig.UnreachableExitCode, err.Error())
}

if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -284,5 +285,8 @@ func init() {
fs.StringVarP(&cliHealthConfig.CPUUseThresCritical, "cpu-usage-threshold-crit", "", "100",
"The percentage of CPU usage on which to be a critical result")

fs.IntVarP(&cliHealthConfig.UnreachableExitCode, "unreachable-state", "", 3,
"Exit with specified code if unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown")

fs.SortFlags = false
}
23 changes: 22 additions & 1 deletion cmd/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestHealth_ConnectionRefused(t *testing.T) {

cmd := exec.Command("go", "run", "../main.go", "health", "--port", "9999")
out, _ := cmd.CombinedOutput()

Expand All @@ -22,6 +21,28 @@ func TestHealth_ConnectionRefused(t *testing.T) {
}
}

func TestHealth_ConnectionRefusedCritical(t *testing.T) {
cmd := exec.Command("go", "run", "../main.go", "health", "--port", "9999", "--unreachable-state", "2")
out, _ := cmd.CombinedOutput()

actual := string(out)
expected := "[CRITICAL] - Get \"http://localhost:9999/"

if !strings.Contains(actual, expected) {
t.Error("\nActual: ", actual, "\nExpected: ", expected)
}

cmd = exec.Command("go", "run", "../main.go", "health", "--port", "9999", "--unreachable-state", "-123")
out, _ = cmd.CombinedOutput()

actual = string(out)
expected = "[UNKNOWN] - Get \"http://localhost:9999/"

if !strings.Contains(actual, expected) {
t.Error("\nActual: ", actual, "\nExpected: ", expected)
}
}

type HealthTest struct {
name string
server *httptest.Server
Expand Down

0 comments on commit d9a57cc

Please sign in to comment.