Skip to content

Commit

Permalink
attempting DF inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 24, 2021
1 parent 03512c0 commit f6e127b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 22 deletions.
16 changes: 9 additions & 7 deletions driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package driver

import "github.com/bisoncorps/saido/inspector"

type fields struct {
// Supported inspectors for specific driver
Supported []inspector.Inspector
// Supported inspector representations for specific driver
Supported []string
// Selected inspector representations
Selected []string
// Polling interval between retrievals
PollInterval int64
}

// Driver : specification of functions to be defined by every Driver
type Driver interface {
readFile(path string) (string, error)
runCommand(command string) (string, error)
ReadFile(path string) (string, error)
RunCommand(command string) (string, error)
// shows the driver details, not sure if we should be showing OS name
getDetails() string
GetDetails() string
}
6 changes: 3 additions & 3 deletions driver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Local struct {
fields
}

func (d *Local) readFile(path string) (string, error) {
func (d *Local) ReadFile(path string) (string, error) {
log.Debugf("Reading content from %s", path)
content, err := ioutil.ReadFile(path)
if err != nil {
Expand All @@ -23,7 +23,7 @@ func (d *Local) readFile(path string) (string, error) {
return string(content), nil
}

func (d *Local) runCommand(command string) (string, error) {
func (d *Local) RunCommand(command string) (string, error) {
cmdArgs := strings.Fields(command)
log.Debugf("Running command `%s` ", command)
out, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
Expand All @@ -33,6 +33,6 @@ func (d *Local) runCommand(command string) (string, error) {
return string(out), nil
}

func (d *Local) getDetails() string {
func (d *Local) GetDetails() string {
return fmt.Sprintf(`Local - %s`, runtime.GOOS)
}
5 changes: 2 additions & 3 deletions driver/local_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
package driver

import (
"github.com/bisoncorps/saido/inspector"
"strings"
"testing"
)

func TestUnixLocalRunCommand(t *testing.T) {
d := Local{}
d.Supported = []inspector.Inspector{}
output, err := d.runCommand(`ps -A`)
d.Supported = []string{}
output, err := d.RunCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions driver/local_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

func TestWindowsRunCommand(t *testing.T) {
d := Local{}
output, err := d.runCommand(`tasklist`)
output, err := d.RunCommand(`tasklist`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
}

func TestWindowsLocalGetDetails(t *testing.T) {
d := Local{}
output := d.getDetails()
output := d.GetDetails()
if output != "Local - windows" {
t.Error(output)
}
Expand Down
8 changes: 4 additions & 4 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (d *SSH) Client() (*goph.Client, error) {
return client, err
}

func (d *SSH) readFile(path string) (string, error) {
func (d *SSH) ReadFile(path string) (string, error) {
log.Debugf("Reading remote content %s", path)
command := fmt.Sprintf(`cat %s`, path)
return d.runCommand(command)
return d.RunCommand(command)
}

func (d *SSH) runCommand(command string) (string, error) {
func (d *SSH) RunCommand(command string) (string, error) {
// FIXME: Do we retain client across all command runs?
log.Debugf("Running remote command %s", command)
client, err := d.Client()
Expand All @@ -82,6 +82,6 @@ func (d *SSH) runCommand(command string) (string, error) {
return string(out), nil
}

func (d *SSH) getDetails() string {
func (d *SSH) GetDetails() string {
return fmt.Sprintf(`SSH - %s`, d.String())
}
7 changes: 5 additions & 2 deletions driver/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ func TestSSHRunCommand(t *testing.T) {
User: "dev",
Host: "127.0.0.1",
Port: 2222,
PubKeyFile: "/home/runner/.ssh/id_rsa",
PubKeyFile: "/home/deven/.ssh/id_rsa",
PubKeyPass: "",
CheckKnownHosts: false,
fields: fields{
PollInterval: 5,
},
}
output, err := d.runCommand(`ps -A`)
output, err := d.RunCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
Expand Down
25 changes: 25 additions & 0 deletions inspector/df.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package inspector

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

// DF : Parsing the `df` output for memory monitoring
type DF struct {
fields
}

func (i *DF) Parse(output string) {
log.Debug(output)
}

// NewDF : Initialize a new DF instance
func NewDF() *DF {
return &DF{
fields: fields{
Type: Command,
Command: `df -a`,
},
}

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

package inspector

import (
"testing"
)

func TestDF(t *testing.T) {
d := NewDF()
if d.Type != Command || d.Command != `df -a` {
t.Error("Initialized df wrongly")
}
}
50 changes: 49 additions & 1 deletion inspector/inspector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
package inspector

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

// Mode : This specifies whether an Inspector is a command or a file
type Mode int

const (
// Command : Inspector is a command to be executes
Command Mode = iota
// File : Inspector is a file to be read
File
)

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

type fields struct {
// Specify a mode for the Inspector
Type Mode
// File path to read
FilePath string
// Command to execute
Command string
}

func (f *fields) String() string {
value := `None`
if f.Type == Command {
value = f.Command
} else if f.Type == File {
value = f.FilePath
}
return value
}

// Inspector : defines a particular metric supported by a driver
type Inspector interface{}
type Inspector interface {
Parse(output string)
}

// GetInspector : obtain an initialized inspector using name
func GetInspector(name string) Inspector {
val, ok := inspectorMap[name]
if !ok {
log.Fatalf(`%s inspector not found`, name)
}
return val
}
19 changes: 19 additions & 0 deletions integration/integration_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build !windows

package integration

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

func TestDFonLocal(t *testing.T) {
d := driver.Local{}
i := inspector.NewDF()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
}

0 comments on commit f6e127b

Please sign in to comment.