psutil • A cross-platform process and system utilities module for Python • Linux/Windows/OSX/FreeBSD/Solaris • 32bit/64bit • CPU, Mem, Disk, Network, Process and so on
FreeBSD/OSX • Some of values can get from sysctl as Text ! • Some is Not Text • kern.devstat.all retunes “C struct devinfo_dev” • can not use cgo to stay “Pure golang” • (Linux /var/run/utmp is also not text) % sysctl vm.stats.vm.v_page_count vm.stats.vm.v_page_count: 1012332
Use syscall.Syscall • call syscall of low-level operating system primitives directory Notice: http://golang.org/pkg shows only Linux. you must read src directory to read other platform
Use “cgo -godefs" // +build ignore package disk ! /* #include #include #include */ import "C" ! type Devstat C.struct_devstat % go tool cgo -godefs types_freebsd.go > disk_freebsd_amd64.go types_freebsd.go Once you created definition, can use on other hosts.
limitation • union • C's union types are represented as a Go byte array with the same length • output perfect may not perfect • some hand editing should be required…
Ok, how about Windows? • No need to use C struct ! ! ! ! ! • easy, huh? Modkernel32 = syscall.NewLazyDLL("kernel32.dll") procGetDiskFreeSpaceExW = Modkernel32.NewProc("GetDiskFreeSpaceExW")
Conclusion • gopsutil can get system information • Linux/Windows/OSX/FreeBSD • use syscall.Syscall to do low-level • use “cgo -godefs” to generate Go from C