Skip to content

Instantly share code, notes, and snippets.

@cftang0827
Created April 26, 2019 08:24
Show Gist options
  • Save cftang0827/24682cae1687d15d4473a7fed377b1b6 to your computer and use it in GitHub Desktop.
Save cftang0827/24682cae1687d15d4473a7fed377b1b6 to your computer and use it in GitHub Desktop.
The example of gopsutil, get the info of CPU memory and disk
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