Skip to content

Commit

Permalink
Merge branch 'master' of github.com:oscp/openshift-monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
u220374 committed Jun 30, 2017
2 parents 6aac723 + f087224 commit ae90828
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
19 changes: 14 additions & 5 deletions daemon/client/checks/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
kubernetesIP = "172.30.0.1"
)

var num = regexp.MustCompile(`\d+(?:\.\d+)?`)

func CheckExternalSystem(url string) (error) {
if err := checkHttp(url); err != nil {
Expand Down Expand Up @@ -94,12 +95,19 @@ func getEndpoint(slow bool) string {
}
}

// isVgSizeOk returns true if vgs output in stdOut indicates that the volume
// group free space is equal or above the percentage treshold okSize, which is
// expected to be in the range [0, 100].
func isVgSizeOk(stdOut string, okSize int) bool {
// Example
// 5.37 26.84 vg_fast_registry
// 5.37 26.84 vg_slow
num := regexp.MustCompile("(\\d+\\.\\d+)")
nums := num.FindAllString(stdOut, -1)
nums := num.FindAllString(stdOut, 2)

if len(nums) != 2 {
log.Println("Unable to parse vgs output:", stdOut)
return false
}

free, err := strconv.ParseFloat(nums[0], 64)
if (err != nil) {
Expand All @@ -114,20 +122,21 @@ func isVgSizeOk(stdOut string, okSize int) bool {

// calculate usage
if (100 / size * free < float64(okSize)) {
msg := fmt.Sprintf("VG free size is below treshold. Size: %v, free: %v, treshold: %v %", size, free, okSize)
msg := fmt.Sprintf("VG free size is below treshold. Size: %v, free: %v, treshold: %v %%", size, free, okSize)
log.Println(msg)
return false
}

return true
}

// isLvsSizeOk returns true if lvs output in stdOut indicates that the logical
// volume percentage full for data and metadata are both below the threshold
// okSize, which is expected to be in the range [0, 100].
func isLvsSizeOk(stdOut string, okSize int) bool {
// Examples
// 42.10 8.86 docker-pool
// 13.63 8.93 lv_fast_registry_pool
num := regexp.MustCompile("(\\d+\\.\\d+)")

checksOk := 0
for _, nr := range num.FindAllString(stdOut, -1) {
i, err := strconv.ParseFloat(nr, 64)
Expand Down
32 changes: 32 additions & 0 deletions daemon/client/checks/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package checks

import (
"io/ioutil"
"log"
"testing"
)

func init() {
// Omit standard log output when running tests to allow one to focus on
// actual test results.
log.SetOutput(ioutil.Discard)
}

func TestIsVgSizeOk(t *testing.T) {
tests := []struct {
line string
okSize int
want bool
}{
{"invalid input", 99, false},
{"5.37 26.84 vg_slow", 5, true},
{"5.37 26.84 vg_slow", 25, false},
{" 0 511.03 fedora", 10, false},
{"\t25\t250 test", 10, true},
}
for _, tt := range tests {
if got := isVgSizeOk(tt.line, tt.okSize); got != tt.want {
t.Errorf("isVgSizeOk(%q, %v) = %v, want %v", tt.line, tt.okSize, got, tt.want)
}
}
}

0 comments on commit ae90828

Please sign in to comment.