Skip to content

Commit

Permalink
WIP: restructuring inspector-driver integration
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 16, 2022
1 parent 8316327 commit 4ba9d86
Show file tree
Hide file tree
Showing 24 changed files with 889 additions and 362 deletions.
16 changes: 15 additions & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package driver

// SystemInfo gives more insight into system details
type SystemDetails struct {
IsWindows bool
IsLinux bool
IsDarwin bool
IsWeb bool
Name string
Extra string
}

type fields struct {
// Supported inspector representations for specific driver
Supported []string
// Selected inspector representations
Selected []string
// Polling interval between retrievals
PollInterval int64
Info *SystemDetails
}

// Command represents the two commands ReadFile & RunCommand
type Command func(string) (string, error)

// Driver : specification of functions to be defined by every Driver
type Driver interface {
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() SystemDetails
}
21 changes: 19 additions & 2 deletions driver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@ func (d *Local) RunCommand(command string) (string, error) {
}
}
out, err := cmd.Output()
fmt.Printf("%s", string(out))
if err != nil {
return ``, err
}
return string(out), nil
}

func (d *Local) GetDetails() string {
return fmt.Sprintf(`Local - %s`, runtime.GOOS)
func (d *Local) GetDetails() SystemDetails {
if d.Info == nil {
details := &SystemDetails{}
details.Name = runtime.GOOS
switch details.Name {
case "windows":
details.IsWindows = true
case "linux":
details.IsLinux = true
case "darwin":
details.IsDarwin = true
default:
details.IsLinux = true
}
details.Extra = runtime.GOARCH
d.Info = details
}
return *d.Info
}
8 changes: 8 additions & 0 deletions driver/local_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func TestUnixLocalRunCommand(t *testing.T) {
t.Error(err)
}
}

func TestUnixLocalSystemDetails(t *testing.T) {
d := Local{}
details := d.GetDetails()
if !(details.IsLinux || details.IsDarwin) {
t.Errorf("Expected Darwin or Linux on unix test, got %s", details.Name)
}
}
8 changes: 4 additions & 4 deletions driver/local_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func TestWindowsRunCommand(t *testing.T) {
}
}

func TestWindowsLocalGetDetails(t *testing.T) {
func TestWindowsLocalSystemDetails(t *testing.T) {
d := Local{}
output := d.GetDetails()
if output != "Local - windows" {
t.Error(output)
details := d.GetDetails()
if !details.IsWindows {
t.Errorf("Expected windows got %s", details.Name)
}
}
41 changes: 26 additions & 15 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,31 @@ func (d *SSH) RunCommand(command string) (string, error) {
return string(out), nil
}

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,
},
func (d *SSH) GetDetails() SystemDetails {
if d.Info == nil {
uname, err := d.RunCommand(`uname`)
// try windows command
if err != nil {
windowsName, err := d.RunCommand(`systeminfo | findstr /B /C:"OS Name"`)
if err == nil {
if strings.Contains(strings.ToLower(windowsName), "windows") {
uname = "windows"
}
}
}
details := &SystemDetails{}
details.Name = uname
switch details.Name {
case "windows":
details.IsWindows = true
case "linux":
details.IsLinux = true
case "darwin":
details.IsDarwin = true
default:
details.IsLinux = true
}
d.Info = details
}
return *d.Info
}
22 changes: 22 additions & 0 deletions driver/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@ import (
"testing"
)

func NewSSHForTest() Driver {
return &SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
KeyFile: "/home/diretnan/.ssh/id_rsa",
KeyPass: "",
CheckKnownHosts: false,
fields: fields{
PollInterval: 5,
},
}
}

func TestSSHRunCommand(t *testing.T) {
d := NewSSHForTest()
output, err := d.RunCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
}
}

func TestSSHSystemDetails(t *testing.T) {
d := NewSSHForTest()
details := d.GetDetails()
if !details.IsLinux {
t.Errorf("Expected linux server for ssh test got %s", details.Name)
}
}
20 changes: 9 additions & 11 deletions driver/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,14 @@ func (d *Web) RunCommand(command string) (string, error) {
return ``, errors.New("Cannot read file on web driver")
}

func (d *Web) GetDetails() string {
return fmt.Sprintf(`Web - %s`, d.String())
}

func NewWebForTest() *Web {
return &Web{
URL: "https://duckduckgo.com",
Method: GET,
fields: fields{
PollInterval: 5,
},
func (d *Web) GetDetails() SystemDetails {
if d.Info == nil {
details := &SystemDetails{
Name: "web",
Extra: d.URL,
IsWeb: true,
}
d.Info = details
}
return *d.Info
}
34 changes: 34 additions & 0 deletions driver/web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package driver

import (
"testing"
)

func NewWebForTest() *Web {
return &Web{
URL: "https://duckduckgo.com",
Method: GET,
fields: fields{
PollInterval: 5,
},
}
}

func TestWebRunCommand(t *testing.T) {
d := NewWebForTest()
output, err := d.RunCommand(`response`)
if err != nil {
t.Error(err)
}
if output == "" {
t.Error("Could not parse response time")
}
}

func TestWebSystemDetails(t *testing.T) {
d := NewWebForTest()
details := d.GetDetails()
if !details.IsWeb {
t.Errorf("Expected web driver for web test got %s", details.Name)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/kr/pretty v0.2.0 // indirect
github.com/melbahja/goph v1.2.1
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.4.3
github.com/mum4k/termdash v0.16.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
Expand Down
44 changes: 35 additions & 9 deletions inspector/custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package inspector

import (
"fmt"

"github.com/bisohns/saido/driver"
log "github.com/sirupsen/logrus"
)

Expand All @@ -11,8 +14,9 @@ type CustomMetrics struct {

// Custom : Parsing the custom command output for disk monitoring
type Custom struct {
fields
Values CustomMetrics
Driver *driver.Driver
Values CustomMetrics
Command string
}

// Parse : run custom parsing on output of the command
Expand All @@ -27,13 +31,35 @@ func (i Custom) createMetric(output string) CustomMetrics {
}
}

// NewCustom : Initialize a new Custom instance
func NewCustom(custom string) *Custom {
return &Custom{
fields: fields{
Type: Command,
Command: custom,
},
func (i *Custom) SetDriver(driver *driver.Driver) {
details := (*driver).GetDetails()
if details.IsWeb {
panic(fmt.Sprintf("Cannot use Custom(%s) on web", i.Command))
}
i.Driver = driver
}

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

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

// NewCustom : Initialize a new Custom instance
func NewCustom(custom string, driver *driver.Driver) Inspector {
var customInspector Inspector
details := (*driver).GetDetails()
if details.IsWeb {
panic(fmt.Sprintf("Cannot use Custom(%s) on web", custom))
}
customInspector = &Custom{
Command: custom,
}
customInspector.SetDriver(driver)
return customInspector
}
Loading

0 comments on commit 4ba9d86

Please sign in to comment.