Created
April 26, 2019 08:24
-
-
Save cftang0827/24682cae1687d15d4473a7fed377b1b6 to your computer and use it in GitHub Desktop.
The example of gopsutil, get the info of CPU memory and disk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"time" | |
"github.com/shirou/gopsutil/cpu" | |
"github.com/shirou/gopsutil/disk" | |
"github.com/shirou/gopsutil/mem" | |
) | |
func getDeviceInfo() map[string]interface{} { | |
//https://koangel.github.io/2017/05/04/gopsutil-get-system/ | |
v, _ := mem.VirtualMemory() | |
// c, _ := cpu.Info() | |
cc, _ := cpu.Percent(time.Second, true) | |
d, _ := disk.Usage("/") | |
info := make(map[string]interface{}) | |
info["mem_usage"] = fmt.Sprintf("%.2f %%", v.UsedPercent) | |
info["cpu_usage"] = fmt.Sprintf("%.2f %%", cc[0]) | |
info["disk_usage"] = fmt.Sprintf("%.2f %%", d.UsedPercent) | |
return info | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment