break recovery/client io extraction into two separate functions

This commit is contained in:
Vaibhav Bhembre 2016-02-18 19:59:40 +00:00
parent fc5806e901
commit 8cded5dc62
1 changed files with 103 additions and 90 deletions

View File

@ -550,55 +550,26 @@ func (c *ClusterHealthCollector) collectRecoveryClientIO() error {
return err
}
var matched []string
sc := bufio.NewScanner(bytes.NewReader(buf))
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
switch {
case strings.HasPrefix(line, "recovery io"):
matched = recoveryIORateRegex.FindStringSubmatch(line)
if len(matched) == 3 {
v, err := strconv.Atoi(matched[1])
if err != nil {
if err := c.collectRecoveryIO(line); err != nil {
return err
}
switch strings.ToLower(matched[2]) {
case "gb":
v = v * 1e9
case "mb":
v = v * 1e6
case "kb":
v = v * 1e3
default:
return fmt.Errorf("can't parse units %q", matched[2])
}
c.RecoveryIORate.Set(float64(v))
}
matched = recoveryIOKeysRegex.FindStringSubmatch(line)
if len(matched) == 2 {
v, err := strconv.Atoi(matched[1])
if err != nil {
return err
}
c.RecoveryIOKeys.Set(float64(v))
}
matched = recoveryIOObjectsRegex.FindStringSubmatch(line)
if len(matched) == 2 {
v, err := strconv.Atoi(matched[1])
if err != nil {
return err
}
c.RecoveryIOObjects.Set(float64(v))
}
case strings.HasPrefix(line, "client io"):
matched = clientIOReadRegex.FindStringSubmatch(line)
if err := c.collectClientIO(line); err != nil {
return err
}
}
}
return nil
}
func (c *ClusterHealthCollector) collectClientIO(clientStr string) error {
matched := clientIOReadRegex.FindStringSubmatch(clientStr)
if len(matched) == 3 {
v, err := strconv.Atoi(matched[1])
if err != nil {
@ -619,7 +590,7 @@ func (c *ClusterHealthCollector) collectRecoveryClientIO() error {
c.ClientIORead.Set(float64(v))
}
matched = clientIOWriteRegex.FindStringSubmatch(line)
matched = clientIOWriteRegex.FindStringSubmatch(clientStr)
if len(matched) == 3 {
v, err := strconv.Atoi(matched[1])
if err != nil {
@ -640,7 +611,7 @@ func (c *ClusterHealthCollector) collectRecoveryClientIO() error {
c.ClientIOWrite.Set(float64(v))
}
matched = clientIOOpsRegex.FindStringSubmatch(line)
matched = clientIOOpsRegex.FindStringSubmatch(clientStr)
if len(matched) == 2 {
v, err := strconv.Atoi(matched[1])
if err != nil {
@ -649,7 +620,49 @@ func (c *ClusterHealthCollector) collectRecoveryClientIO() error {
c.ClientIOOps.Set(float64(v))
}
return nil
}
func (c *ClusterHealthCollector) collectRecoveryIO(recoveryStr string) error {
matched := recoveryIORateRegex.FindStringSubmatch(recoveryStr)
if len(matched) == 3 {
v, err := strconv.Atoi(matched[1])
if err != nil {
return err
}
switch strings.ToLower(matched[2]) {
case "gb":
v = v * 1e9
case "mb":
v = v * 1e6
case "kb":
v = v * 1e3
default:
return fmt.Errorf("can't parse units %q", matched[2])
}
c.RecoveryIORate.Set(float64(v))
}
matched = recoveryIOKeysRegex.FindStringSubmatch(recoveryStr)
if len(matched) == 2 {
v, err := strconv.Atoi(matched[1])
if err != nil {
return err
}
c.RecoveryIOKeys.Set(float64(v))
}
matched = recoveryIOObjectsRegex.FindStringSubmatch(recoveryStr)
if len(matched) == 2 {
v, err := strconv.Atoi(matched[1])
if err != nil {
return err
}
c.RecoveryIOObjects.Set(float64(v))
}
return nil
}