File size: 633 Bytes
d9a1d72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//go:build linux

package protocol

import "syscall"

func collectDiskUsage() (usedMB, totalMB int64, percent float64) {
	var stat syscall.Statfs_t
	if err := syscall.Statfs("/", &stat); err != nil {
		if err := syscall.Statfs(".", &stat); err != nil {
			return 0, 0, 0
		}
	}
	totalBytes := stat.Blocks * uint64(stat.Bsize)
	freeBytes := stat.Bavail * uint64(stat.Bsize)
	if totalBytes == 0 {
		return 0, 0, 0
	}
	usedBytes := totalBytes - freeBytes
	totalMB = int64(totalBytes / (1024 * 1024))
	usedMB = int64(usedBytes / (1024 * 1024))
	percent = float64(usedBytes) * 100 / float64(totalBytes)
	return usedMB, totalMB, percent
}