Skip to content

Commit

Permalink
completed df parser
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 26, 2021
1 parent f6e127b commit 0e59091
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 21 deletions.
27 changes: 21 additions & 6 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"golang.org/x/crypto/ssh"
)

var port = 22

// SSH : Driver for handling ssh executions
type SSH struct {
fields
Expand All @@ -17,10 +19,10 @@ type SSH struct {
Host string
// port default is 22
Port int
// Path to public key file
PubKeyFile string
// Pass key for public key file
PubKeyPass string
// Path to private key file
KeyFile string
// Pass key for key file
KeyPass string
// Check known hosts (only disable for tests
CheckKnownHosts bool
}
Expand All @@ -34,11 +36,10 @@ func (d *SSH) Client() (*goph.Client, error) {
var err error
var client *goph.Client
var callback ssh.HostKeyCallback
port := 22
if d.Port != 0 {
port = d.Port
}
auth, err := goph.Key(d.PubKeyFile, d.PubKeyPass)
auth, err := goph.Key(d.KeyFile, d.KeyPass)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -85,3 +86,17 @@ func (d *SSH) RunCommand(command string) (string, error) {
func (d *SSH) GetDetails() string {
return fmt.Sprintf(`SSH - %s`, d.String())
}

func NewSSHForTest() *SSH {
return &SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
KeyFile: "/home/deven/.ssh/id_rsa",
KeyPass: "",
CheckKnownHosts: false,
fields: fields{
PollInterval: 5,
},
}
}
12 changes: 1 addition & 11 deletions driver/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ import (
)

func TestSSHRunCommand(t *testing.T) {
d := SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
PubKeyFile: "/home/deven/.ssh/id_rsa",
PubKeyPass: "",
CheckKnownHosts: false,
fields: fields{
PollInterval: 5,
},
}
d := NewSSHForTest()
output, err := d.RunCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
Expand Down
62 changes: 61 additions & 1 deletion inspector/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,72 @@ package inspector

import (
log "github.com/sirupsen/logrus"
"strconv"
"strings"
)

type DFMetrics struct {
size float64
used float64
available float64
percentFull int
}

// DF : Parsing the `df` output for memory monitoring
type DF struct {
fields
// 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
// Mount point to examine
MountPoint string
// Values of metrics being read
Values []DFMetrics
}

// Parse : run custom parsing on output of the command
func (i *DF) Parse(output string) {
log.Debug(output)
var values []DFMetrics
log.Debug("Parsing ouput string in DF inspector")
lines := strings.Split(output, "\n")
for index, line := range lines {
// skip title line
if index == 0 {
continue
}
columns := strings.Fields(line)
if len(columns) == 6 {
percent := columns[4]
if len(percent) > 1 {
percent = percent[:len(percent)-1]
} else if percent == `-` {
percent = `0`
}
percentInt, err := strconv.Atoi(percent)
if err != nil {
log.Fatalf(`Error Parsing Percent Full: %s `, err)
}
if columns[5] == i.MountPoint {
values = append(values, i.createMetric(columns, percentInt))
} else if strings.HasPrefix(columns[0], i.DeviceStartsWith) &&
i.MountPoint == "" {
values = append(values, i.createMetric(columns, percentInt))
}
}
}
i.Values = values
}

func (i DF) createMetric(columns []string, percent int) DFMetrics {
return DFMetrics{
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),
percentFull: percent,
}
}

// NewDF : Initialize a new DF instance
Expand All @@ -20,6 +77,9 @@ func NewDF() *DF {
Type: Command,
Command: `df -a`,
},
RawByteSize: `KB`,
DisplayByteSize: `GB`,
MountPoint: `/`,
}

}
2 changes: 1 addition & 1 deletion inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

var inspectorMap = map[string]Inspector{
`df`: NewDF(),
`disk`: NewDF(),
}

type fields struct {
Expand Down
43 changes: 43 additions & 0 deletions inspector/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package inspector

import (
log "github.com/sirupsen/logrus"
"math"
"strconv"
)

var byteMap = []string{"B", "KB", "MB", "GB", "TB"}

// ByteSize : helps parse string into individual byte values
type ByteSize struct {
value float64
}

func (b *ByteSize) format(unit string) float64 {
return b.value / math.Pow(1024, index(byteMap, unit))
}

// Initialize a NewByteSize
func NewByteSize(byteCount string, unit string) *ByteSize {
if byteCount == `-` {
byteCount = "0"
}
byteCountInt, err := strconv.ParseFloat(byteCount, 64)
byteCountInt = byteCountInt * math.Pow(1024, index(byteMap, unit))
if err != nil {
log.Fatal(err)
}
return &ByteSize{
value: byteCountInt,
}
}

func index(arr []string, str string) float64 {
for index, a := range arr {
if a == str {
return float64(index)
}
}
// defaults to byte if not found in array
return 0
}
21 changes: 21 additions & 0 deletions inspector/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package inspector

import (
"fmt"
"testing"
)

func TestByteSize(t *testing.T) {
d := NewByteSize(`1000`, `KB`)
if d.value != 1024000 {
t.Error("Did not set byte value correctly")
}
second := NewByteSize(`0.9765625`, `MB`)
if second.value != d.value {
fmt.Println(second.value, d.value)
t.Error("MB value not equivalent to KB value")
}
if second.format(`KB`) != 1000 {
t.Error("Could not convert MB back to KB value")
}
}
20 changes: 20 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package integration

import (
"fmt"
"testing"

"github.com/bisoncorps/saido/driver"
"github.com/bisoncorps/saido/inspector"
)

func TestDFonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewDF()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
fmt.Printf(`%#v`, i.Values)
}
8 changes: 6 additions & 2 deletions integration/integration_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
package integration

import (
"fmt"
"testing"

"github.com/bisoncorps/saido/driver"
"github.com/bisoncorps/saido/inspector"
"testing"
)

func TestDFonLocal(t *testing.T) {
d := driver.Local{}
i := inspector.NewDF()
// can either use NewDF() or get the interface and perform type assertion
i := (inspector.GetInspector(`disk`)).(*inspector.DF)
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
fmt.Printf(`%#v`, i.Values)
}

0 comments on commit 0e59091

Please sign in to comment.