Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disk usage data discrepancy on Darwin #167

Open
wants to merge 1 commit into
base: aws-cwa-release-1-23-0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions patches/gopsutil/v3/disk/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package disk

import (
"context"
"fmt"
"os/exec"
"strconv"
"strings"

"github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -85,3 +89,69 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
cmd := exec.Command("/bin/df", "-ki", path)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("error executing df command: %v", err)
}

lines := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(lines) < 2 {
return nil, fmt.Errorf("unexpected df output format")
}

// Skip the header line and process the data line
fields := strings.Fields(lines[1])
if len(fields) < 9 {
jefchien marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("unexpected number of fields in df output")
}

// Parse values
total, err := parseUint64(fields[1])
if err != nil {
return nil, fmt.Errorf("error parsing total blocks: %v", err)
}
used, err := parseUint64(fields[2])
if err != nil {
return nil, fmt.Errorf("error parsing used blocks: %v", err)
}
free, err := parseUint64(fields[3])
if err != nil {
return nil, fmt.Errorf("error parsing available blocks: %v", err)
}
inodesUsed, err := parseUint64(fields[5])
if err != nil {
return nil, fmt.Errorf("error parsing iused: %v", err)
}
inodesFree, err := parseUint64(fields[6])
if err != nil {
return nil, fmt.Errorf("error parsing ifree: %v", err)
}

// Calculate percentages
usedPercent := float64(used) / float64(total) * 100
inodesTotal := inodesUsed + inodesFree
inodesUsedPercent := float64(inodesUsed) / float64(inodesTotal) * 100

// Create UsageStat object
us := &UsageStat{
Path: fields[8],
Fstype: fields[0],
Total: total * 1024,
Free: free * 1024,
Used: used * 1024,
UsedPercent: usedPercent,
InodesTotal: inodesTotal,
InodesUsed: inodesUsed,
InodesFree: inodesFree,
InodesUsedPercent: inodesUsedPercent,
}

return us, nil
}

func parseUint64(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 64)
}
4 changes: 2 additions & 2 deletions patches/gopsutil/v3/disk/disk_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build freebsd || linux || darwin
// +build freebsd linux darwin
//go:build freebsd || linux
// +build freebsd linux

package disk

Expand Down
Loading