Skip to content

Commit

Permalink
collect disk usage on rootfs for maintenance report
Browse files Browse the repository at this point in the history
  • Loading branch information
HouzuoGuo committed Mar 3, 2018
1 parent ec240f0 commit 95cd962
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions misc/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ func GetSystemUptimeSec() int {
return FindNumInRegexGroup(RegexTotalUptimeSec, string(content), 1)
}

// GetRootDiskUsageKB returns used and total space of the file system mounted on /. Returns 0 if they cannot be determined.
func GetRootDiskUsageKB() (usedKB, freeKB, totalKB int) {
fs := syscall.Statfs_t{}
err := syscall.Statfs("/", &fs)
if err != nil {
return
}
totalKB = int(int64(fs.Blocks) * fs.Bsize / 1024)
freeKB = int(int64(fs.Bfree) * fs.Bsize / 1024)
usedKB = totalKB - freeKB
return
}

/*
UtilityDir is an element of PATH that points to a directory where laitos bundled utility programs are stored. The
utility programs are not essential to most of laitos operations, however they come in handy in certain scenarios:
Expand Down
8 changes: 8 additions & 0 deletions misc/sys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func TestGetSystemUptimeSec(t *testing.T) {
}
}

func TestGetRootDiskUsageKB(t *testing.T) {
SkipTestIfCI(t)
used, free, total := GetRootDiskUsageKB()
if used == 0 || free == 0 || total == 0 || used+free != total {
t.Fatal(used/1024, free/1024, total/1024)
}
}

func TestGetSysctl(t *testing.T) {
key := "kernel.pid_max"
if runtime.GOOS != "linux" {
Expand Down
3 changes: 3 additions & 0 deletions toolbox/env_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ func (info *EnvControl) Execute(cmd Command) *Result {
// Return runtime information (uptime, CPUs, goroutines, memory usage) in a multi-line text.
func GetRuntimeInfo() string {
usedMem, totalMem := misc.GetSystemMemoryUsageKB()
usedRoot, freeRoot, totalRoot := misc.GetRootDiskUsageKB()
return fmt.Sprintf(`IP: %s
Clock: %s
Sys/prog uptime: %s / %s
Total/used/prog mem: %d / %d / %d MB
Total/used/free rootfs: %d / %d / %d MB
Sys load: %s
Num CPU/GOMAXPROCS/goroutines: %d / %d / %d
Program flags: %v
Expand All @@ -79,6 +81,7 @@ Program flags: %v
time.Now().String(),
time.Duration(misc.GetSystemUptimeSec()*int(time.Second)).String(), time.Now().Sub(misc.StartupTime).String(),
totalMem/1024, usedMem/1024, misc.GetProgramMemoryUsageKB()/1024,
totalRoot/1024, usedRoot/1024, freeRoot/1024,
misc.GetSystemLoad(),
runtime.NumCPU(), runtime.GOMAXPROCS(0), runtime.NumGoroutine(),
os.Args[1:])
Expand Down

0 comments on commit 95cd962

Please sign in to comment.