diff --git a/client/controller.go b/client/controller.go index d91af8a..a581fb5 100644 --- a/client/controller.go +++ b/client/controller.go @@ -73,9 +73,9 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) { if hosts.getDriver(host.Address) == nil { hosts.resetDriver(host) } - for _, metric := range hosts.Info.Metrics { + for metric, custom := range hosts.Info.Metrics { driver := hosts.getDriver(host.Address) - initializedMetric, err := inspector.Init(metric, driver) + initializedMetric, err := inspector.Init(metric, driver, custom) data, err := initializedMetric.Execute() if err == nil { var unmarsh interface{} diff --git a/config.example.yaml b/config.example.yaml index 01a168d..9e9723e 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,6 +1,7 @@ # Ansible inspired Config hosts: connection: + # password logging ssh driver type: ssh username: root password: somethingSecret @@ -16,9 +17,11 @@ hosts: password: somethingSecret port: 2222 "192.0.1.4": + # local driver connection: type: local eu-west1: + # key file logging ssh driver connection: type: ssh private_key_path: /path/to/private/key @@ -26,8 +29,17 @@ hosts: children: "192.0.10.3": "192.0.10.5": + eu-west2: + # passworded key file logging ssh driver + connection: + type: ssh + private_key_path: /path/to/private/key + private_key_passphrase: "hexadecimal" + port: 3333 metrics: -- memory -- tcp + memory: + tcp: + # custom command to show uptime + custom: "cat /proc/uptime" poll-interval: 30 diff --git a/config/config.go b/config/config.go index e37d097..4e185c8 100644 --- a/config/config.go +++ b/config/config.go @@ -13,7 +13,7 @@ import ( type DashboardInfo struct { Hosts []Host - Metrics []string + Metrics map[string]string Title string PollInterval int } @@ -39,12 +39,13 @@ func (dashboardInfo *DashboardInfo) GetAllHostAddresses() (addresses HostList) { } type Connection struct { - Type string `mapstructure:"type"` - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - PrivateKeyPath string `mapstructure:"private_key_path"` - Port int32 `mapstructure:"port"` - Host string + Type string `mapstructure:"type"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + PrivateKeyPath string `mapstructure:"private_key_path"` + PrivateKeyPassPhrase string `mapstructure:"private_key_passphrase"` + Port int32 `mapstructure:"port"` + Host string } func (conn *Connection) ToDriver() driver.Driver { @@ -55,6 +56,8 @@ func (conn *Connection) ToDriver() driver.Driver { Host: conn.Host, Port: int(conn.Port), KeyFile: conn.PrivateKeyPath, + KeyPass: conn.PrivateKeyPassPhrase, + Password: conn.Password, CheckKnownHosts: false, } default: @@ -70,7 +73,7 @@ type Host struct { type Config struct { Hosts map[interface{}]interface{} `yaml:"hosts"` - Metrics []string `yaml:"metrics"` + Metrics map[interface{}]interface{} `yaml:"metrics"` Title string `yaml:"title"` PollInterval int `yaml:"poll-interval"` } @@ -95,9 +98,13 @@ func GetDashboardInfoConfig(config *Config) *DashboardInfo { if config.Title != "" { dashboardInfo.Title = config.Title } + metrics := make(map[string]string) dashboardInfo.Hosts = parseConfig("root", "", config.Hosts, &Connection{}) - dashboardInfo.Metrics = config.Metrics + for metric, customCommand := range config.Metrics { + metrics[fmt.Sprintf("%v", metric)] = fmt.Sprintf("%v", customCommand) + } + dashboardInfo.Metrics = metrics for _, host := range dashboardInfo.Hosts { log.Debugf("%s: %v", host.Address, host.Connection) } @@ -114,6 +121,9 @@ func parseConnection(conn map[interface{}]interface{}) *Connection { if c.Type == "ssh" && c.Port == 0 { c.Port = 22 } + if c.Password != "" && c.PrivateKeyPath != "" { + log.Fatal("Cannot specify both password login and private key login on same connection") + } return &c } diff --git a/driver/ssh.go b/driver/ssh.go index c6cc4e8..7e22ffc 100644 --- a/driver/ssh.go +++ b/driver/ssh.go @@ -24,6 +24,8 @@ type SSH struct { KeyFile string // Pass key for key file KeyPass string + // Password based login + Password string // Check known hosts (only disable for tests CheckKnownHosts bool // set environmental vars for server e.g []string{"DEBUG=1", "FAKE=echo"} @@ -40,13 +42,18 @@ func (d *SSH) Client() (*goph.Client, error) { if d.SessionClient == nil { var err error var client *goph.Client + var auth goph.Auth var callback ssh.HostKeyCallback if d.Port != 0 { port = d.Port } - auth, err := goph.Key(d.KeyFile, d.KeyPass) - if err != nil { - return nil, err + if d.Password != "" { + auth = goph.Password(d.Password) + } else { + auth, err = goph.Key(d.KeyFile, d.KeyPass) + if err != nil { + return nil, err + } } if d.CheckKnownHosts { callback, err = goph.DefaultKnownHosts() diff --git a/inspector/inspector.go b/inspector/inspector.go index 0a9d86b..5f87a02 100644 --- a/inspector/inspector.go +++ b/inspector/inspector.go @@ -25,8 +25,8 @@ var inspectorMap = map[string]NewInspector{ `process`: NewProcess, `loadavg`: NewLoadAvg, `tcp`: NewTcp, + `custom`: NewCustom, // NOTE: Inactive for now - `custom`: NewCustom, `responsetime`: NewResponseTime, } diff --git a/scripts/config.local.yaml b/scripts/config.local.yaml index ef39bc1..bc53452 100644 --- a/scripts/config.local.yaml +++ b/scripts/config.local.yaml @@ -6,7 +6,7 @@ hosts: type: local metrics: -- memory -- tcp + memory: + tcp: poll-interval: 30 diff --git a/scripts/prep-test-ssh.sh b/scripts/prep-test-ssh.sh index c202884..300af7b 100755 --- a/scripts/prep-test-ssh.sh +++ b/scripts/prep-test-ssh.sh @@ -24,9 +24,9 @@ hosts: port: 2222 private_key_path: "$SSH_KEY_PATH/${SSH_KEY_NAME}" metrics: -- memory -- disk -- tcp -- docker + memory: + disk: + tcp: + docker: poll-interval: 10 EOF