Skip to content

Commit

Permalink
Modified DF to support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 17, 2022
1 parent 654167a commit 935de09
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
95 changes: 88 additions & 7 deletions inspector/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inspector

import (
"errors"
"fmt"
"strconv"
"strings"

Expand All @@ -11,10 +12,13 @@ import (

// DFMetrics : Metrics used by DF
type DFMetrics struct {
FileSystem string
Size float64
Used float64
Available float64
PercentFull int
// Optional Volume Name that may be available on Windows
VolumeName string
}

// DF : Parsing the `df` output for disk monitoring
Expand All @@ -27,8 +31,6 @@ type DF struct {
DisplayByteSize string
// Parse only device that start with this e.g /dev/sd
DeviceStartsWith string
// Mount point to examine
MountPoint string
// Values of metrics being read
Values []DFMetrics
}
Expand All @@ -55,10 +57,9 @@ func (i *DF) Parse(output string) {
if err != nil {
log.Fatalf(`Error Parsing Percent Full: %s `, err)
}
if columns[len(columns)-1] == i.MountPoint {
if strings.HasPrefix(columns[0], i.DeviceStartsWith) {
values = append(values, i.createMetric(columns, percentInt))
} else if strings.HasPrefix(columns[0], i.DeviceStartsWith) &&
i.MountPoint == "" {
} else {
values = append(values, i.createMetric(columns, percentInt))
}
}
Expand All @@ -68,6 +69,7 @@ func (i *DF) Parse(output string) {

func (i DF) createMetric(columns []string, percent int) DFMetrics {
return DFMetrics{
FileSystem: columns[0],
Size: NewByteSize(columns[1], i.RawByteSize).format(i.DisplayByteSize),
Used: NewByteSize(columns[2], i.RawByteSize).format(i.DisplayByteSize),
Available: NewByteSize(columns[3], i.RawByteSize).format(i.DisplayByteSize),
Expand All @@ -92,9 +94,81 @@ func (i *DF) Execute() {

// TODO: Implement DF for windows using
// `wmic logicaldisk` to satisfy Inspector interface
type WMIC struct {
type DFWin struct {
Driver *driver.Driver
Command string
// The values read from the command output string are defaultly in KB
RawByteSize string
// We want do display disk values in GB
DisplayByteSize string
// Parse only device that start with this e.g /dev/sd
DeviceStartsWith string
// Values of metrics being read
Values []DFMetrics
}

func (i *DFWin) Parse(output string) {
var values []DFMetrics
log.Debug("Parsing ouput string in DF inspector")
lineChar := "\r"
output = strings.TrimPrefix(output, lineChar)
output = strings.TrimSuffix(output, lineChar)
lines := strings.Split(output, lineChar)
for index, line := range lines {
// skip title line
if index == 0 || index == 1 {
continue
}
columns := strings.Split(line, ",")
if len(columns) >= 7 {
available, err := strconv.Atoi(columns[3])
size, err := strconv.Atoi(columns[5])
if err != nil {
panic("Could not parse sizes for DFWin")
}
used := size - available
percentInt := int((float64(used) / float64(size)) * 100)
cols := []string{
columns[1],
fmt.Sprintf("%d", size),
fmt.Sprintf("%d", used),
fmt.Sprintf("%d", available),
columns[6],
}
if strings.HasPrefix(columns[1], i.DeviceStartsWith) {
values = append(values, i.createMetric(cols, percentInt))
} else {
values = append(values, i.createMetric(cols, percentInt))
}
}
}
i.Values = values
}

func (i DFWin) createMetric(columns []string, percent int) DFMetrics {
return DFMetrics{
FileSystem: columns[0],
Size: NewByteSize(columns[1], i.RawByteSize).format(i.DisplayByteSize),
Used: NewByteSize(columns[2], i.RawByteSize).format(i.DisplayByteSize),
Available: NewByteSize(columns[3], i.RawByteSize).format(i.DisplayByteSize),
VolumeName: columns[4],
PercentFull: percent,
}
}

func (i *DFWin) SetDriver(driver *driver.Driver) {
i.Driver = driver
}

func (i DFWin) driverExec() driver.Command {
return (*i.Driver).RunCommand
}

func (i *DFWin) Execute() {
output, err := i.driverExec()(i.Command)
if err == nil {
i.Parse(output)
}
}

// NewDF : Initialize a new DF instance
Expand All @@ -111,7 +185,14 @@ func NewDF(driver *driver.Driver, _ ...string) (Inspector, error) {
Command: `df -a -k`,
RawByteSize: `KB`,
DisplayByteSize: `GB`,
MountPoint: `/`,
}
} else {
df = &DFWin{
// Using format to account for weird spacing
// issues that arise on windows
Command: `wmic logicaldisk list brief /format:csv`,
RawByteSize: `B`,
DisplayByteSize: `GB`,
}
}
df.SetDriver(driver)
Expand Down
14 changes: 14 additions & 0 deletions integration/integration_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -41,3 +42,16 @@ func TestCustomonLocal(t *testing.T) {
}
}
}

func TestDFonLocal(t *testing.T) {
d := NewLocalForTest()
i, _ := inspector.Init(`disk`, &d)
i.Execute()
iConcrete, ok := i.(*inspector.DFWin)
if ok {
fmt.Printf("%#v", iConcrete.Values)
if len(iConcrete.Values) < 1 {
t.Error("DFWin not showing at least one drive")
}
}
}

0 comments on commit 935de09

Please sign in to comment.