Skip to content

Commit

Permalink
added loadavg inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 27, 2021
1 parent 962cfd7 commit 3bdc5ab
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
4 changes: 2 additions & 2 deletions inspector/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
log "github.com/sirupsen/logrus"
)

// CustomMetrics : Metrics used by DF
// CustomMetrics : Metrics used by Custom
type CustomMetrics struct {
Output string
}

// Custom : Parsing the `df` output for disk monitoring
// Custom : Parsing the custom command output for disk monitoring
type Custom struct {
fields
Values CustomMetrics
Expand Down
51 changes: 51 additions & 0 deletions inspector/loadavg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package inspector

import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"
)

// LoadAvgMetrics : Metrics used by LoadAvg
type LoadAvgMetrics struct {
Load1M float64
Load5M float64
Load15M float64
}

// LoadAvg : Parsing the /proc/loadavg output for disk monitoring
type LoadAvg struct {
fields
Values LoadAvgMetrics
}

// Parse : run custom parsing on output of the command
func (i *LoadAvg) Parse(output string) {
var err error
log.Debug("Parsing ouput string in LoadAvg inspector")
columns := strings.Fields(output)
Load1M, err := strconv.ParseFloat(columns[0], 64)
Load5M, err := strconv.ParseFloat(columns[1], 64)
Load15M, err := strconv.ParseFloat(columns[2], 64)
if err != nil {
log.Fatalf(`Error Parsing LoadAvg: %s `, err)
}

i.Values = LoadAvgMetrics{
Load1M,
Load5M,
Load15M,
}
}

// NewLoadAvg : Initialize a new LoadAvg instance
func NewLoadAvg() *LoadAvg {
return &LoadAvg{
fields: fields{
Type: File,
FilePath: `/proc/loadavg`,
},
}

}
14 changes: 14 additions & 0 deletions inspector/loadavg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !windows

package inspector

import (
"testing"
)

func TestLoadAvg(t *testing.T) {
d := NewLoadAvg()
if d.Type != File || d.FilePath != `/proc/loadavg` {
t.Error("Initialized loadavg wrongly")
}
}
13 changes: 13 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ func TestCustomonSSH(t *testing.T) {
t.Errorf("%s", i.Values.Output)
}
}

func TestLoadAvgonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewLoadAvg()
output, err := d.ReadFile(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
if i.Values.Load1M == 0 {
t.Errorf("%f", i.Values.Load1M)
}
}
14 changes: 14 additions & 0 deletions integration/integration_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ func TestCustomonLocal(t *testing.T) {
t.Errorf("%s", i.Values.Output)
}
}

func TestLoadAvgonLocal(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewLoadAvg()
output, err := d.ReadFile(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
if i.Values.Load1M == 0 {
t.Errorf("%f", i.Values.Load1M)
}
fmt.Printf("%#v", i.Values)
}

0 comments on commit 3bdc5ab

Please sign in to comment.