Skip to content

Commit

Permalink
add disk render
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Dec 5, 2024
1 parent a406e34 commit e3ef92b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/gpud/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
pollXidEvents bool
pollGPMEvents bool
netcheck bool
diskcheck bool

enableAutoUpdate bool
autoUpdateExitCode int
Expand Down Expand Up @@ -530,6 +531,11 @@ cat summary.txt
Usage: "enable network connectivity checks to global edge/derp servers (default: true)",
Destination: &netcheck,
},
&cli.BoolTFlag{
Name: "diskcheck",
Usage: "enable disk checks (default: true)",
Destination: &diskcheck,
},
},
},
{
Expand Down
1 change: 1 addition & 0 deletions cmd/gpud/command/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func cmdScan(cliContext *cli.Context) error {
diagnose.WithPollXidEvents(pollXidEvents),
diagnose.WithPollGPMEvents(pollGPMEvents),
diagnose.WithNetcheck(netcheck),
diagnose.WithDiskcheck(diskcheck),
}
if zapLvl.Level() <= zap.DebugLevel { // e.g., info, warn, error
diagnoseOpts = append(diagnoseOpts, diagnose.WithDebug(true))
Expand Down
9 changes: 8 additions & 1 deletion components/diagnose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type Op struct {
pollXidEvents bool
pollGPMEvents bool

netcheck bool
netcheck bool
diskcheck bool
}

type OpOption func(*Op)
Expand Down Expand Up @@ -59,3 +60,9 @@ func WithNetcheck(b bool) OpOption {
op.netcheck = b
}
}

func WithDiskcheck(b bool) OpOption {
return func(op *Op) {
op.diskcheck = b
}
}
11 changes: 11 additions & 0 deletions components/diagnose/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
query_log_common "github.com/leptonai/gpud/components/query/log/common"
query_log_tail "github.com/leptonai/gpud/components/query/log/tail"
"github.com/leptonai/gpud/log"
"github.com/leptonai/gpud/pkg/disk"
pkg_dmesg "github.com/leptonai/gpud/pkg/dmesg"
"github.com/leptonai/gpud/pkg/file"
latency_edge "github.com/leptonai/gpud/pkg/latency/edge"
Expand Down Expand Up @@ -238,6 +239,16 @@ func Scan(ctx context.Context, opts ...OpOption) error {
}
}

if op.diskcheck {
fmt.Printf("\n%s checking disk\n", inProgress)
partitions, err := disk.GetPartitions()
if err != nil {
log.Logger.Warnw("error getting partitions", "error", err)
} else {
partitions.RenderTable(os.Stdout)
}
}

fmt.Printf("\n\n%s scan complete\n\n", checkMark)
return nil
}
21 changes: 21 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package disk
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"

"github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
"github.com/shirou/gopsutil/v4/disk"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -89,6 +91,25 @@ func (parts Partitions) TotalBytes() uint64 {
return total
}

func (parts Partitions) RenderTable(wr io.Writer) {
table := tablewriter.NewWriter(wr)
table.SetHeader([]string{"Device", "Mount", "Mounted", "Fstype", "Total", "Free", "Used"})

for _, part := range parts {
table.Append([]string{
part.Device,
part.MountPoint,
strconv.FormatBool(part.Mounted),
part.Fstype,
part.Usage.TotalHumanized,
part.Usage.FreeHumanized,
part.Usage.UsedHumanized,
})
}

table.Render()
}

type Partition struct {
Device string `json:"device"`
MountPoint string `json:"mount_point"`
Expand Down
7 changes: 6 additions & 1 deletion pkg/disk/disk_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package disk

import "testing"
import (
"os"
"testing"
)

func TestGetPartitions(t *testing.T) {
t.Parallel()
Expand All @@ -14,4 +17,6 @@ func TestGetPartitions(t *testing.T) {
t.Fatalf("failed to marshal partitions to yaml: %v", err)
}
t.Logf("partitions:\n%s\n", string(yb))

partitions.RenderTable(os.Stdout)
}

0 comments on commit e3ef92b

Please sign in to comment.