new device detection engine (OS aware). Uses smartctl --scan to detect drives (and conditionally uses jaypipes/ghw). WWN is calculated from smartctl data, then retrieved from GHW, and fallsback to serial number. WWN calcuation code is based on IEEE spec, for "Registered" IEEE format - NAA5. TODO: support NAA6 and other formats?
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
)
|
||||
|
||||
func ExecCmd(cmdName string, cmdArgs []string, workingDir string, environ []string) (string, error) {
|
||||
|
||||
cmd := exec.Command(cmdName, cmdArgs...)
|
||||
var stdBuffer bytes.Buffer
|
||||
mw := io.MultiWriter(os.Stdout, &stdBuffer)
|
||||
|
||||
cmd.Stdout = mw
|
||||
cmd.Stderr = mw
|
||||
|
||||
if environ != nil {
|
||||
cmd.Env = environ
|
||||
}
|
||||
if workingDir != "" && path.IsAbs(workingDir) {
|
||||
cmd.Dir = workingDir
|
||||
} else if workingDir != "" {
|
||||
return "", errors.New("Working Directory must be an absolute path")
|
||||
}
|
||||
|
||||
err := cmd.Run()
|
||||
return stdBuffer.String(), err
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package common_test
|
||||
|
||||
import (
|
||||
"github.com/analogj/scrutiny/collector/pkg/common"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExecCmd(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
//setup
|
||||
|
||||
//test
|
||||
result, err := common.ExecCmd("echo", []string{"hello world"}, "", nil)
|
||||
|
||||
//assert
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello world\n", result)
|
||||
}
|
||||
|
||||
func TestExecCmd_Date(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
//setup
|
||||
|
||||
//test
|
||||
_, err := common.ExecCmd("date", []string{}, "", nil)
|
||||
|
||||
//assert
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
//
|
||||
//func TestExecCmd_Error(t *testing.T) {
|
||||
// t.Parallel()
|
||||
//
|
||||
// //setup
|
||||
// bc := collector.BaseCollector {}
|
||||
//
|
||||
// //test
|
||||
// _, err := bc.ExecCmd("smartctl", []string{"-a", "/dev/doesnotexist"}, "", nil)
|
||||
//
|
||||
// //assert
|
||||
// exitError, castOk := err.(*exec.ExitError);
|
||||
// require.True(t, castOk)
|
||||
// require.Equal(t, 1, exitError.ExitCode())
|
||||
//
|
||||
//}
|
||||
//
|
||||
|
||||
func TestExecCmd_InvalidCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
//setup
|
||||
|
||||
//test
|
||||
_, err := common.ExecCmd("invalid_binary", []string{}, "", nil)
|
||||
|
||||
//assert
|
||||
_, castOk := err.(*exec.ExitError)
|
||||
require.False(t, castOk)
|
||||
}
|
||||
Reference in New Issue
Block a user