Skip to content

Commit 44cd3f6

Browse files
committed
feat: top directory added
1 parent d9f7fd1 commit 44cd3f6

3 files changed

Lines changed: 58 additions & 42 deletions

File tree

cmd/linux-health/main.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
func main() {
16+
// ---- SYSTEM STATS ----
1617
stats, err := system.GetSystemStats()
1718
if err != nil {
1819
fmt.Println("error:", err)
@@ -21,44 +22,48 @@ func main() {
2122

2223
health := system.GenerateHealth(stats)
2324

25+
// ---- TOP MEMORY PROCESS ----
2426
topName := ""
2527
topPID := 0
26-
var topMB uint64
28+
topMB := uint64(0)
2729

28-
if topProc, _ := process.TopMemoryProcess(); topProc != nil {
30+
topProc, _ := process.TopMemoryProcess()
31+
if topProc != nil {
2932
topName = topProc.Cmd
3033
topPID = topProc.PID
3134
topMB = topProc.MemKB / 1024
3235
}
3336

37+
// ---- DISK HOTSPOTS (FAST) ----
3438
hotspots := []string{}
35-
dirs, _ := disk.TopDirs(os.Getenv("HOME"), 2)
36-
39+
dirs, _ := disk.TopDirs(os.Getenv("HOME"), 3)
3740
for _, d := range dirs {
38-
hotspots = append(hotspots, d.Path+" "+d.SizeHuman)
41+
hotspots = append(hotspots,
42+
fmt.Sprintf("%s %s", d.Path, d.SizeHuman),
43+
)
3944
}
4045

41-
// ---- SERVICES ----
42-
services := []string{}
43-
if failed, _ := service.FailedServices(); len(failed) > 0 {
44-
for _, s := range failed {
45-
services = append(services, s.Name+" ("+s.State+")")
46-
}
46+
// ---- FAILED SERVICES ----
47+
failedServices := []string{}
48+
failed, _ := service.FailedServices()
49+
for _, s := range failed {
50+
failedServices = append(failedServices,
51+
fmt.Sprintf("%s (%s)", s.Name, s.State),
52+
)
4753
}
4854

49-
networkState := "inactive"
50-
if nets, _ := network.ActiveInterfaces(); len(nets) > 0 {
51-
networkState = "active"
52-
}
55+
// ---- NETWORK ----
56+
networkState := network.Summary()
5357

58+
// ---- OUTPUT ----
5459
output.PrintBlock(
5560
stats,
5661
health,
5762
topName,
5863
topPID,
5964
topMB,
6065
networkState,
61-
services,
66+
failedServices,
6267
hotspots,
6368
)
6469

internal/disk/top_dirs.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package disk
33
import (
44
"bytes"
55
"os/exec"
6-
"sort"
76
"strings"
87
)
98

@@ -13,7 +12,7 @@ type DirUsage struct {
1312
}
1413

1514
func TopDirs(root string, limit int) ([]DirUsage, error) {
16-
cmd := exec.Command("du", "-sh", root+"/*")
15+
cmd := exec.Command("sh", "-c", "du -sh "+root+"/* 2>/dev/null")
1716
var out bytes.Buffer
1817
cmd.Stdout = &out
1918

@@ -25,27 +24,15 @@ func TopDirs(root string, limit int) ([]DirUsage, error) {
2524
var dirs []DirUsage
2625

2726
for _, line := range lines {
28-
line = strings.TrimSpace(line)
29-
if line == "" {
30-
continue
27+
fields := strings.Fields(line)
28+
if len(fields) >= 2 {
29+
dirs = append(dirs, DirUsage{
30+
SizeHuman: fields[0],
31+
Path: fields[1],
32+
})
3133
}
32-
33-
parts := strings.Fields(line)
34-
if len(parts) < 2 {
35-
continue
36-
}
37-
38-
dirs = append(dirs, DirUsage{
39-
SizeHuman: parts[0],
40-
Path: parts[1],
41-
})
4234
}
4335

44-
// Sort descending by size string (du already sorts roughly)
45-
sort.Slice(dirs, func(i, j int) bool {
46-
return dirs[i].SizeHuman > dirs[j].SizeHuman
47-
})
48-
4936
if len(dirs) > limit {
5037
dirs = dirs[:limit]
5138
}

pkg/output/block.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package output
22

33
import (
44
"fmt"
5-
65
"github.com/ravindran-dev/linux-health/internal/system"
6+
"strings"
77
)
88

99
const boxWidth = 40
@@ -41,15 +41,15 @@ func PrintBlock(
4141
}
4242

4343
sep()
44+
row("HOTSPOTS:")
4445

45-
if len(diskHotspots) > 0 {
46-
row("HOTSPOTS:")
46+
if len(diskHotspots) == 0 {
47+
row(" No large directories found")
48+
} else {
4749
for _, d := range diskHotspots {
4850
row(" " + trim(d))
4951
}
50-
sep()
5152
}
52-
5353
row("SERVICES:")
5454
if len(failedServices) == 0 {
5555
row(" ✓ No failed services")
@@ -60,7 +60,8 @@ func PrintBlock(
6060
}
6161

6262
sep()
63-
row(fmt.Sprintf("NETWORK : %s", network))
63+
64+
printNetwork(network)
6465

6566
bottom()
6667
}
@@ -125,3 +126,26 @@ func trim(s string) string {
125126
}
126127
return s
127128
}
129+
130+
func printNetwork(n string) {
131+
132+
if n == "" {
133+
row("NETWORK : unavailable")
134+
return
135+
}
136+
137+
parts := strings.Fields(n)
138+
139+
if len(parts) < 6 {
140+
row("NETWORK : " + trim(n))
141+
return
142+
}
143+
144+
iface := parts[0]
145+
rx := parts[2]
146+
tx := parts[4]
147+
drops := parts[len(parts)-1]
148+
149+
row(fmt.Sprintf("NETWORK : %-5s RX: %-5s TX: %-5s", iface, rx, tx))
150+
row(fmt.Sprintf("DROPS : %s", drops))
151+
}

0 commit comments

Comments
 (0)