Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bisohns/saido
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Nov 8, 2022
2 parents 285ff41 + 0190ee7 commit 78ff292
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
4 changes: 2 additions & 2 deletions client/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
16 changes: 14 additions & 2 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ansible inspired Config
hosts:
connection:
# password logging ssh driver
type: ssh
username: root
password: somethingSecret
Expand All @@ -16,18 +17,29 @@ 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
port: 2222
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
28 changes: 19 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type DashboardInfo struct {
Hosts []Host
Metrics []string
Metrics map[string]string
Title string
PollInterval int
}
Expand All @@ -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 {
Expand All @@ -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:
Expand All @@ -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"`
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
13 changes: 10 additions & 3 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/config.local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ hosts:
type: local

metrics:
- memory
- tcp
memory:
tcp:
poll-interval: 30

8 changes: 4 additions & 4 deletions scripts/prep-test-ssh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 78ff292

Please sign in to comment.