Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Created May 18, 2016 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrooijen/7f46f5066e72b57a8bfc8acbd03b17c9 to your computer and use it in GitHub Desktop.
Save mrrooijen/7f46f5066e72b57a8bfc8acbd03b17c9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"syscall"
)
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
)
type DiskStatus struct {
All uint64 `json:"all"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
}
func DiskUsage(path string) (disk DiskStatus) {
var (
fs = syscall.Statfs_t{}
err = syscall.Statfs(path, &fs)
)
if err != nil {
return
}
disk.All = fs.Blocks * uint64(fs.Bsize)
disk.Free = fs.Bfree * uint64(fs.Bsize)
disk.Used = disk.All - disk.Free
return
}
type MemoryStatus struct {
All uint64 `json:"all"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
}
func MemoryUsage() (memory MemoryStatus) {
var (
si = syscall.Sysinfo_t{}
err = syscall.Sysinfo(&si)
)
if err != nil {
return
}
memory.All = si.Totalram
memory.Free = si.Freeram
memory.Used = memory.All - memory.Free
return
}
func main() {
var (
path = "/"
disk = DiskUsage(path)
memory = MemoryUsage()
)
fmt.Println(path)
fmt.Printf("All: %.2f MB\n", float64(disk.All)/float64(MB))
fmt.Printf("Used: %.2f MB\n", float64(disk.Used)/float64(MB))
fmt.Printf("Free: %.2f MB\n", float64(disk.Free)/float64(MB))
fmt.Printf("All: %.2f MB\n", float64(memory.All)/float64(MB))
fmt.Printf("Used: %.2f MB\n", float64(memory.Used)/float64(MB))
fmt.Printf("Free: %.2f MB\n", float64(memory.Free)/float64(MB))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment