diff --git a/cmd/root.go b/cmd/root.go index 00b8749..3b5d5af 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,14 +22,16 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/bisohns/saido/charts" "github.com/bisohns/saido/config" ) var ( cfgFile string + cfg *config.Config + // Verbose : Should display verbose logs - verbose bool + verbose bool + dashboardInfo *config.DashboardInfo ) const appName = "saido" @@ -54,9 +56,7 @@ var rootCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { log.Info("Saido is running ...") - _ = config.GetConfig() - // log.Infof("%v", cfg) - // charts.Main() + // charts.Main(cfg) }, } @@ -70,7 +70,6 @@ func Execute() { } func init() { - charts.Main() cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Path to config file") @@ -79,5 +78,5 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { - _ = config.LoadConfig(cfgFile) + cfg = config.LoadConfig(cfgFile) } diff --git a/config/config.go b/config/config.go index 1afe901..fe56bac 100644 --- a/config/config.go +++ b/config/config.go @@ -10,13 +10,16 @@ import ( "gopkg.in/yaml.v2" ) -var config = &Config{} -var allHosts []Host +type DashboardInfo struct { + Hosts []Host + Metrics []string + Title string +} type Connection struct { Type string `mapstructure:"type"` - Username string `mapstructure:"type"` - Password string `mapstructure:"type"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` PrivateKeyPath string `mapstructure:"private_key_path"` Port int32 `mapstructure:"port"` } @@ -30,9 +33,11 @@ type Host struct { type Config struct { Hosts map[interface{}]interface{} `yaml:"hosts"` Metrics []string `yaml:"metrics"` + Title string `yaml:"title"` } func LoadConfig(configPath string) *Config { + var config = &Config{} confYaml, err := ioutil.ReadFile(configPath) if err != nil { log.Errorf("yamlFile.Get err %v ", err) @@ -41,32 +46,37 @@ func LoadConfig(configPath string) *Config { if err != nil { log.Fatalf("error: %v", err) } - - parseConfig("root", "", config.Hosts, &Connection{}) - - for _, i := range allHosts { - log.Infof("Name: %s, Connection: %+v", i.Address, i.Connection) - } - return config } -func (cf *Config) parse() { - -} +func GetDashboardInfoConfig(config *Config) *DashboardInfo { + dashboardInfo := &DashboardInfo{ + Title: "Saido", + } + if config.Title != "" { + dashboardInfo.Title = config.Title + } -func GetConfig() *Config { - return config + dashboardInfo.Hosts = parseConfig("root", "", config.Hosts, &Connection{}) + dashboardInfo.Metrics = config.Metrics + for _, host := range dashboardInfo.Hosts { + log.Debugf("%s: %v", host.Address, host.Connection) + } + return dashboardInfo } func parseConnection(conn map[interface{}]interface{}) *Connection { var c Connection mapstructure.Decode(conn, &c) + if c.Type == "ssh" && c.Port == 0 { + c.Port = 22 + } return &c } -func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) { +func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) []Host { currentConn := currentConnection + allHosts := []Host{} log.Infof("Loading config for %s and host: %s with Connection: %+v", name, host, currentConn) isParent := false // Set to true for groups that contain just children data i.e children if conn, ok := group["connection"]; ok { @@ -83,16 +93,16 @@ func parseConfig(name string, host string, group map[interface{}]interface{}, cu parsedChildren, ok := children.(map[interface{}]interface{}) if !ok { log.Fatalf("Failed to parse children of %s", name) - return + return nil } for k, v := range parsedChildren { host := make(map[interface{}]interface{}) host, ok := v.(map[interface{}]interface{}) if !ok && v != nil { // some leaf nodes do not contain extra data under - log.Errorf("Failed to parse children of %s", name) + log.Errorf("Faled to parse children of %s", name) } - parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn) + allHosts = append(allHosts, parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)...) } } @@ -101,7 +111,11 @@ func parseConfig(name string, host string, group map[interface{}]interface{}, cu Address: host, Connection: currentConn, } + if alias, ok := group["alias"]; ok { + newHost.Alias = alias.(string) + } allHosts = append(allHosts, newHost) } + return allHosts } diff --git a/driver/local_unix_test.go b/driver/local_unix_test.go index 5bf3074..119671b 100644 --- a/driver/local_unix_test.go +++ b/driver/local_unix_test.go @@ -9,7 +9,6 @@ import ( func TestUnixLocalRunCommand(t *testing.T) { d := Local{} - d.Supported = []string{} output, err := d.RunCommand(`ps -A`) if err != nil || !strings.Contains(output, "PID") { t.Error(err) diff --git a/driver/ssh_test.go b/driver/ssh_test.go index a8a8378..216da41 100644 --- a/driver/ssh_test.go +++ b/driver/ssh_test.go @@ -1,21 +1,28 @@ package driver import ( + "fmt" + "os" + "path/filepath" "strings" "testing" + + "github.com/bisohns/saido/config" ) func NewSSHForTest() Driver { + workingDir, _ := os.Getwd() + workingDir = filepath.Dir(workingDir) + yamlPath := fmt.Sprintf("%s/%s", workingDir, "config-test.yaml") + conf := config.LoadConfig(yamlPath) + dashboardInfo := config.GetDashboardInfoConfig(conf) return &SSH{ - User: "dev", - Host: "127.0.0.1", - Port: 2222, - KeyFile: "/home/diretnan/.ssh/id_rsa", + User: dashboardInfo.Hosts[0].Connection.Username, + Host: dashboardInfo.Hosts[0].Address, + Port: int(dashboardInfo.Hosts[0].Connection.Port), + KeyFile: dashboardInfo.Hosts[0].Connection.PrivateKeyPath, KeyPass: "", CheckKnownHosts: false, - driverBase: driverBase{ - PollInterval: 5, - }, } } diff --git a/inspector/disk.go b/inspector/disk.go index 7da2d3e..cb8c09f 100644 --- a/inspector/disk.go +++ b/inspector/disk.go @@ -77,6 +77,15 @@ func (i *DF) Parse(output string) { if err != nil { log.Fatalf(`Error Parsing Percent Full: %s `, err) } + // find size + for index := range columns { + _, err := strconv.ParseInt(columns[index], 0, 64) + if err == nil { + columns[0] = strings.Join(columns[:index], " ") + columns[1] = columns[index] + break + } + } if strings.HasPrefix(columns[0], i.DeviceStartsWith) { values = append(values, i.createMetric(columns, percentInt)) } else { diff --git a/inspector/disk_test.go b/inspector/disk_test.go index 7fb6b12..ee3c9a4 100644 --- a/inspector/disk_test.go +++ b/inspector/disk_test.go @@ -3,17 +3,26 @@ package inspector import ( + "fmt" + "os" + "path/filepath" "testing" + "github.com/bisohns/saido/config" "github.com/bisohns/saido/driver" ) func NewSSHForTest() driver.Driver { + workingDir, _ := os.Getwd() + workingDir = filepath.Dir(workingDir) + yamlPath := fmt.Sprintf("%s/%s", workingDir, "config-test.yaml") + conf := config.LoadConfig(yamlPath) + dashboardInfo := config.GetDashboardInfoConfig(conf) return &driver.SSH{ - User: "dev", - Host: "127.0.0.1", - Port: 2222, - KeyFile: "/home/diretnan/.ssh/id_rsa", + User: dashboardInfo.Hosts[0].Connection.Username, + Host: dashboardInfo.Hosts[0].Address, + Port: int(dashboardInfo.Hosts[0].Connection.Port), + KeyFile: dashboardInfo.Hosts[0].Connection.PrivateKeyPath, KeyPass: "", CheckKnownHosts: false, } diff --git a/scripts/prep-test-ssh.sh b/scripts/prep-test-ssh.sh index 2bd0f24..6c3599e 100755 --- a/scripts/prep-test-ssh.sh +++ b/scripts/prep-test-ssh.sh @@ -7,20 +7,18 @@ rm -rf $SSH_KEY_PATH mkdir -p $SSH_KEY_PATH ssh-keygen -f "$SSH_KEY_PATH/${SSH_KEY_NAME}" -N "" docker kill saido-linux-sshserver | true +docker rm saido-linux-sshserver || echo 'could not remove container' docker run -d -p 2222:2222 -e USER_NAME=$SSH_USER --name saido-linux-sshserver -v ${SSH_KEY_PATH}/${SSH_KEY_NAME}.pub:/config/.ssh/authorized_keys linuxserver/openssh-server +SSH_HOST="$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" saido-linux-sshserver)" cat < config-test.yaml hosts: children: - "$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" saido-linux-sshserver)": + "${SSH_HOST}": connection: type: ssh - port: 2222 username: ${SSH_USER} - private_key_path: $(pwd)/${SSH_KEY_NAME} - "127.0.0.1": - connection: - type: local - + port: 2222 + private_key_path: "$SSH_KEY_PATH/${SSH_KEY_NAME}" metrics: - memory - cpu