-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sys: add host-info endpoint, add client API method * remove old commented handler * add http tests, fix bugs * query all partitions for disk usage * fix Timestamp decoding * add comments for clarification * dont append a nil entry on disk usage query error * remove HostInfo from the sdk api We can use Logical().Read(...) to query this endpoint since the payload is contained with the data object. All warnings are preserved under Secret.Warnings. * ensure that we're testing failure case against a standby node * add and use TestWaitStandby to ensure core is on standby * remove TestWaitStandby * respond with local-only error * move HostInfo into its own helper package * fix imports; use new no-forward handler * add cpu times to collection * emit clearer multierrors/warnings by collection type * add comments on HostInfo fields
- Loading branch information
Showing
10 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package hostutil | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/shirou/gopsutil/cpu" | ||
"github.com/shirou/gopsutil/disk" | ||
"github.com/shirou/gopsutil/host" | ||
"github.com/shirou/gopsutil/mem" | ||
) | ||
|
||
// HostInfo holds all the information that gets captured on the host. The | ||
// set of information captured depends on the host operating system. For more | ||
// information, refer to: https://github.com/shirou/gopsutil#current-status | ||
type HostInfo struct { | ||
// Timestamp returns the timestamp in UTC on the collection time. | ||
Timestamp time.Time `json:"timestamp"` | ||
// CPU returns information about the CPU such as family, model, cores, etc. | ||
CPU []cpu.InfoStat `json:"cpu"` | ||
// CPUTimes returns statistics on CPU usage represented in Jiffies. | ||
CPUTimes []cpu.TimesStat `json:"cpu_times"` | ||
// Disk returns statitics on disk usage for all accessible partitions. | ||
Disk []*disk.UsageStat `json:"disk"` | ||
// Host returns general host information such as hostname, platform, uptime, | ||
// kernel version, etc. | ||
Host *host.InfoStat `json:"host"` | ||
// Memory contains statistics about the memory such as total, available, and | ||
// used memory in number of bytes. | ||
Memory *mem.VirtualMemoryStat `json:"memory"` | ||
} | ||
|
||
// HostInfoError is a typed error for more convenient error checking. | ||
type HostInfoError struct { | ||
Type string | ||
Err error | ||
} | ||
|
||
func (e *HostInfoError) WrappedErrors() []error { | ||
return []error{e.Err} | ||
} | ||
|
||
func (e *HostInfoError) Error() string { | ||
return fmt.Sprintf("%s: %s", e.Type, e.Err.Error()) | ||
} | ||
|
||
// CollectHostInfo returns information on the host, which includes general | ||
// host status, CPU, memory, and disk utilization. | ||
// | ||
// The function does a best-effort capture on the most information possible, | ||
// continuing on capture errors encountered and appending them to a resulting | ||
// multierror.Error that gets returned at the end. | ||
func CollectHostInfo() (*HostInfo, error) { | ||
var retErr *multierror.Error | ||
info := &HostInfo{Timestamp: time.Now().UTC()} | ||
|
||
if h, err := host.Info(); err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{"host", err}) | ||
} else { | ||
info.Host = h | ||
} | ||
|
||
if v, err := mem.VirtualMemory(); err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{"memory", err}) | ||
} else { | ||
info.Memory = v | ||
} | ||
|
||
parts, err := disk.Partitions(false) | ||
if err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{"disk", err}) | ||
} else { | ||
var usage []*disk.UsageStat | ||
for i, part := range parts { | ||
u, err := disk.Usage(part.Mountpoint) | ||
if err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{fmt.Sprintf("disk.%d", i), err}) | ||
continue | ||
} | ||
usage = append(usage, u) | ||
|
||
} | ||
info.Disk = usage | ||
} | ||
|
||
if c, err := cpu.Info(); err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{"cpu", err}) | ||
} else { | ||
info.CPU = c | ||
} | ||
|
||
t, err := cpu.Times(true) | ||
if err != nil { | ||
retErr = multierror.Append(retErr, &HostInfoError{"cpu_times", err}) | ||
} else { | ||
info.CPUTimes = t | ||
} | ||
|
||
return info, retErr.ErrorOrNil() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package hostutil | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCollectHostInfo(t *testing.T) { | ||
info, err := CollectHostInfo() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if info.Timestamp.IsZero() { | ||
t.Fatal("expected non-zero Timestamp") | ||
} | ||
if info.CPU == nil { | ||
t.Fatal("expected non-nil CPU value") | ||
} | ||
if info.CPUTimes == nil { | ||
t.Fatal("expected non-nil CPUTimes value") | ||
} | ||
if info.Disk == nil { | ||
t.Fatal("expected non-nil Disk value") | ||
} | ||
if info.Host == nil { | ||
t.Fatal("expected non-nil Host value") | ||
} | ||
if info.Memory == nil { | ||
t.Fatal("expected non-nil Memory value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/hashicorp/vault/helper/hostutil" | ||
"github.com/hashicorp/vault/vault" | ||
) | ||
|
||
func TestSysHostInfo(t *testing.T) { | ||
cluster := vault.NewTestCluster(t, &vault.CoreConfig{}, &vault.TestClusterOptions{ | ||
HandlerFunc: Handler, | ||
}) | ||
cluster.Start() | ||
defer cluster.Cleanup() | ||
cores := cluster.Cores | ||
|
||
vault.TestWaitActive(t, cores[0].Core) | ||
|
||
// Query against the active node, should get host information back | ||
secret, err := cores[0].Client.Logical().Read("sys/host-info") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if secret == nil || secret.Data == nil { | ||
t.Fatal("expected data in the response") | ||
} | ||
|
||
dataBytes, err := json.Marshal(secret.Data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var info hostutil.HostInfo | ||
if err := json.Unmarshal(dataBytes, &info); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if info.Timestamp.IsZero() { | ||
t.Fatal("expected non-zero Timestamp") | ||
} | ||
if info.CPU == nil { | ||
t.Fatal("expected non-nil CPU value") | ||
} | ||
if info.Disk == nil { | ||
t.Fatal("expected disk info") | ||
} | ||
if info.Host == nil { | ||
t.Fatal("expected host info") | ||
} | ||
if info.Memory == nil { | ||
t.Fatal("expected memory info") | ||
} | ||
|
||
// Query against a standby, should error | ||
secret, err = cores[1].Client.Logical().Read("sys/host-info") | ||
if err == nil || secret != nil { | ||
t.Fatalf("expected error on standby node, HostInfo: %v", secret) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.