Files
troubleshoot/pkg/collect/host_disk_usage.go
Andrew Reed 10a34c2e58 Host preflight (#311)
* Add HostPreflight v1beta2

* Work on TCP Load Balancer

* Host disk usage collector and analyzer

* Host memory analyzer

* TCP port status

* TCP load balancer

* Review changes

Co-authored-by: Marc Campbell <marc.e.campbell@gmail.com>
2021-02-08 16:09:01 -05:00

44 lines
943 B
Go

package collect
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/disk"
)
type DiskUsageInfo struct {
TotalBytes uint64 `json:"total_bytes"`
UsedBytes uint64 `json:"used_bytes"`
}
func HostDiskUsage(c *HostCollector) (map[string][]byte, error) {
result := map[string][]byte{}
if c.Collect.DiskUsage == nil {
return result, nil
}
du, err := disk.Usage(c.Collect.DiskUsage.Path)
if err != nil {
return result, errors.Wrapf(err, "collect disk usage for %s", c.Collect.DiskUsage.Path)
}
diskSpaceInfo := DiskUsageInfo{
TotalBytes: du.Total,
UsedBytes: du.Used,
}
b, err := json.Marshal(diskSpaceInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal disk space info")
}
key := HostDiskUsageKey(c.Collect.DiskUsage.CollectorName)
result[key] = b
return result, nil
}
func HostDiskUsageKey(name string) string {
return fmt.Sprintf("diskUsage/%s.json", name)
}