Skip to content

Commit

Permalink
Merge pull request #329 from replicatedhq/host-diskUsage-traverse
Browse files Browse the repository at this point in the history
diskUsage host collector traverse file tree for directory exists
  • Loading branch information
emosbaugh authored Feb 25, 2021
2 parents b023387 + a459120 commit 7589b4f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions pkg/collect/host_disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package collect
import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/shirou/gopsutil/disk"
Expand All @@ -20,9 +22,14 @@ func HostDiskUsage(c *HostCollector) (map[string][]byte, error) {
return result, nil
}

du, err := disk.Usage(c.Collect.DiskUsage.Path)
pathExists, err := traverseFiletreeDirExists(c.Collect.DiskUsage.Path)
if err != nil {
return result, errors.Wrapf(err, "collect disk usage for %s", c.Collect.DiskUsage.Path)
return result, errors.Wrap(err, "traverse file tree")
}

du, err := disk.Usage(pathExists)
if err != nil {
return result, errors.Wrapf(err, "collect disk usage for %s", pathExists)
}
diskSpaceInfo := DiskUsageInfo{
TotalBytes: du.Total,
Expand All @@ -41,3 +48,21 @@ func HostDiskUsage(c *HostCollector) (map[string][]byte, error) {
func HostDiskUsageKey(name string) string {
return fmt.Sprintf("diskUsage/%s.json", name)
}

func traverseFiletreeDirExists(filename string) (string, error) {
filename = filepath.Clean(filename)
for i := 0; i < 50; i++ {
_, err := os.Stat(filename)
if err == nil {
return filename, nil
} else if os.IsNotExist(err) {
filename = filepath.Dir(filename)
if filename == "/" {
return filename, nil
}
} else {
return "", err
}
}
return "", errors.New("max recursion exceeded")
}
1 change: 1 addition & 0 deletions pkg/collect/host_filesystem_performance_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func HostFilesystemPerformance(c *HostCollector) (map[string][]byte, error) {
if c.Collect.FilesystemPerformance.Directory == "" {
return nil, errors.New("Directory is required to collect filesystem performance info")
}
// TODO: clean up this directory if its created
if err := os.MkdirAll(c.Collect.FilesystemPerformance.Directory, 0700); err != nil {
return nil, errors.Wrapf(err, "failed to mkdir %q", c.Collect.FilesystemPerformance.Directory)
}
Expand Down

0 comments on commit 7589b4f

Please sign in to comment.