Skip to content

Commit 0190ee7

Browse files
committed
Adding custom command inspector and keyfile logging
1 parent 8dbd030 commit 0190ee7

File tree

7 files changed

+52
-23
lines changed

7 files changed

+52
-23
lines changed

Diff for: client/controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
7373
if hosts.getDriver(host.Address) == nil {
7474
hosts.resetDriver(host)
7575
}
76-
for _, metric := range hosts.Info.Metrics {
76+
for metric, custom := range hosts.Info.Metrics {
7777
driver := hosts.getDriver(host.Address)
78-
initializedMetric, err := inspector.Init(metric, driver)
78+
initializedMetric, err := inspector.Init(metric, driver, custom)
7979
data, err := initializedMetric.Execute()
8080
if err == nil {
8181
var unmarsh interface{}

Diff for: config.example.yaml

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Ansible inspired Config
22
hosts:
33
connection:
4+
# password logging ssh driver
45
type: ssh
56
username: root
67
password: somethingSecret
@@ -16,18 +17,29 @@ hosts:
1617
password: somethingSecret
1718
port: 2222
1819
"192.0.1.4":
20+
# local driver
1921
connection:
2022
type: local
2123
eu-west1:
24+
# key file logging ssh driver
2225
connection:
2326
type: ssh
2427
private_key_path: /path/to/private/key
2528
port: 2222
2629
children:
2730
"192.0.10.3":
2831
"192.0.10.5":
32+
eu-west2:
33+
# passworded key file logging ssh driver
34+
connection:
35+
type: ssh
36+
private_key_path: /path/to/private/key
37+
private_key_passphrase: "hexadecimal"
38+
port: 3333
2939

3040
metrics:
31-
- memory
32-
- tcp
41+
memory:
42+
tcp:
43+
# custom command to show uptime
44+
custom: "cat /proc/uptime"
3345
poll-interval: 30

Diff for: config/config.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type DashboardInfo struct {
1515
Hosts []Host
16-
Metrics []string
16+
Metrics map[string]string
1717
Title string
1818
PollInterval int
1919
}
@@ -39,12 +39,13 @@ func (dashboardInfo *DashboardInfo) GetAllHostAddresses() (addresses HostList) {
3939
}
4040

4141
type Connection struct {
42-
Type string `mapstructure:"type"`
43-
Username string `mapstructure:"username"`
44-
Password string `mapstructure:"password"`
45-
PrivateKeyPath string `mapstructure:"private_key_path"`
46-
Port int32 `mapstructure:"port"`
47-
Host string
42+
Type string `mapstructure:"type"`
43+
Username string `mapstructure:"username"`
44+
Password string `mapstructure:"password"`
45+
PrivateKeyPath string `mapstructure:"private_key_path"`
46+
PrivateKeyPassPhrase string `mapstructure:"private_key_passphrase"`
47+
Port int32 `mapstructure:"port"`
48+
Host string
4849
}
4950

5051
func (conn *Connection) ToDriver() driver.Driver {
@@ -55,6 +56,8 @@ func (conn *Connection) ToDriver() driver.Driver {
5556
Host: conn.Host,
5657
Port: int(conn.Port),
5758
KeyFile: conn.PrivateKeyPath,
59+
KeyPass: conn.PrivateKeyPassPhrase,
60+
Password: conn.Password,
5861
CheckKnownHosts: false,
5962
}
6063
default:
@@ -70,7 +73,7 @@ type Host struct {
7073

7174
type Config struct {
7275
Hosts map[interface{}]interface{} `yaml:"hosts"`
73-
Metrics []string `yaml:"metrics"`
76+
Metrics map[interface{}]interface{} `yaml:"metrics"`
7477
Title string `yaml:"title"`
7578
PollInterval int `yaml:"poll-interval"`
7679
}
@@ -95,9 +98,13 @@ func GetDashboardInfoConfig(config *Config) *DashboardInfo {
9598
if config.Title != "" {
9699
dashboardInfo.Title = config.Title
97100
}
101+
metrics := make(map[string]string)
98102

99103
dashboardInfo.Hosts = parseConfig("root", "", config.Hosts, &Connection{})
100-
dashboardInfo.Metrics = config.Metrics
104+
for metric, customCommand := range config.Metrics {
105+
metrics[fmt.Sprintf("%v", metric)] = fmt.Sprintf("%v", customCommand)
106+
}
107+
dashboardInfo.Metrics = metrics
101108
for _, host := range dashboardInfo.Hosts {
102109
log.Debugf("%s: %v", host.Address, host.Connection)
103110
}
@@ -114,6 +121,9 @@ func parseConnection(conn map[interface{}]interface{}) *Connection {
114121
if c.Type == "ssh" && c.Port == 0 {
115122
c.Port = 22
116123
}
124+
if c.Password != "" && c.PrivateKeyPath != "" {
125+
log.Fatal("Cannot specify both password login and private key login on same connection")
126+
}
117127
return &c
118128
}
119129

Diff for: driver/ssh.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type SSH struct {
2424
KeyFile string
2525
// Pass key for key file
2626
KeyPass string
27+
// Password based login
28+
Password string
2729
// Check known hosts (only disable for tests
2830
CheckKnownHosts bool
2931
// set environmental vars for server e.g []string{"DEBUG=1", "FAKE=echo"}
@@ -40,13 +42,18 @@ func (d *SSH) Client() (*goph.Client, error) {
4042
if d.SessionClient == nil {
4143
var err error
4244
var client *goph.Client
45+
var auth goph.Auth
4346
var callback ssh.HostKeyCallback
4447
if d.Port != 0 {
4548
port = d.Port
4649
}
47-
auth, err := goph.Key(d.KeyFile, d.KeyPass)
48-
if err != nil {
49-
return nil, err
50+
if d.Password != "" {
51+
auth = goph.Password(d.Password)
52+
} else {
53+
auth, err = goph.Key(d.KeyFile, d.KeyPass)
54+
if err != nil {
55+
return nil, err
56+
}
5057
}
5158
if d.CheckKnownHosts {
5259
callback, err = goph.DefaultKnownHosts()

Diff for: inspector/inspector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var inspectorMap = map[string]NewInspector{
2525
`process`: NewProcess,
2626
`loadavg`: NewLoadAvg,
2727
`tcp`: NewTcp,
28+
`custom`: NewCustom,
2829
// NOTE: Inactive for now
29-
`custom`: NewCustom,
3030
`responsetime`: NewResponseTime,
3131
}
3232

Diff for: scripts/config.local.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ hosts:
66
type: local
77

88
metrics:
9-
- memory
10-
- tcp
9+
memory:
10+
tcp:
1111
poll-interval: 30
1212

Diff for: scripts/prep-test-ssh.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ hosts:
2424
port: 2222
2525
private_key_path: "$SSH_KEY_PATH/${SSH_KEY_NAME}"
2626
metrics:
27-
- memory
28-
- disk
29-
- tcp
30-
- docker
27+
memory:
28+
disk:
29+
tcp:
30+
docker:
3131
poll-interval: 10
3232
EOF

0 commit comments

Comments
 (0)