From 3bdc5abfd667bb123d26ffc5a0fb93e411fa87c6 Mon Sep 17 00:00:00 2001 From: Diretnan Domnan Date: Wed, 27 Oct 2021 22:00:08 +0100 Subject: [PATCH] added loadavg inspector --- inspector/custom.go | 4 +-- inspector/loadavg.go | 51 ++++++++++++++++++++++++++++ inspector/loadavg_test.go | 14 ++++++++ integration/integration_test.go | 13 +++++++ integration/integration_unix_test.go | 14 ++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 inspector/loadavg.go create mode 100644 inspector/loadavg_test.go diff --git a/inspector/custom.go b/inspector/custom.go index c10c877..60365c2 100644 --- a/inspector/custom.go +++ b/inspector/custom.go @@ -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 diff --git a/inspector/loadavg.go b/inspector/loadavg.go new file mode 100644 index 0000000..616141d --- /dev/null +++ b/inspector/loadavg.go @@ -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`, + }, + } + +} diff --git a/inspector/loadavg_test.go b/inspector/loadavg_test.go new file mode 100644 index 0000000..a54b748 --- /dev/null +++ b/inspector/loadavg_test.go @@ -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") + } +} diff --git a/integration/integration_test.go b/integration/integration_test.go index 6924a8f..9e960b8 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -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) + } +} diff --git a/integration/integration_unix_test.go b/integration/integration_unix_test.go index b8e15ad..d5b032b 100644 --- a/integration/integration_unix_test.go +++ b/integration/integration_unix_test.go @@ -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) +}