Last active
October 14, 2018 17:18
-
-
Save donovansolms/41ec18dee5e163c488e2e9df63ec2eb5 to your computer and use it in GitHub Desktop.
A sample of getting information (CPUs, Memory, GPUs, OS) from the current system in Go.
This file contains 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
package main | |
import ( | |
"fmt" | |
"math" | |
"strings" | |
"github.com/jaypipes/ghw" | |
"github.com/shirou/gopsutil/cpu" | |
"github.com/shirou/gopsutil/host" | |
) | |
func main() { | |
fmt.Println("Getting system stats and info") | |
// =============== | |
fmt.Println("\n\nMemory\n================\n") | |
memory, err := ghw.Memory() | |
if err != nil { | |
fmt.Printf("Error getting memory info: %v", err) | |
} | |
fmt.Println(memory.String()) | |
// =============== | |
fmt.Println("\n\nCPU\n================\n") | |
cpughw, err := ghw.CPU() | |
if err != nil { | |
fmt.Printf("Error getting CPU info: %v", err) | |
} | |
fmt.Printf("%v\n", cpughw) | |
cpuStat, err := cpu.Info() | |
if err != nil { | |
fmt.Printf("Error getting CPU info: %v", err) | |
} | |
fmt.Println(cpuStat[0].ModelName) | |
fmt.Println(cpuStat[0].VendorID) | |
fmt.Println("Cache: ", cpuStat[0].CacheSize) | |
for _, proc := range cpughw.Processors { | |
fmt.Printf(" %v\n", proc) | |
for _, core := range proc.Cores { | |
fmt.Printf(" %v\n", core) | |
} | |
if len(proc.Capabilities) > 0 { | |
// pretty-print the (large) block of capability strings into rows | |
// of 6 capability strings | |
rows := int(math.Ceil(float64(len(proc.Capabilities)) / float64(6))) | |
for row := 1; row < rows; row = row + 1 { | |
rowStart := (row * 6) - 1 | |
rowEnd := int(math.Min(float64(rowStart+6), float64(len(proc.Capabilities)))) | |
rowElems := proc.Capabilities[rowStart:rowEnd] | |
capStr := strings.Join(rowElems, " ") | |
if row == 1 { | |
fmt.Printf(" capabilities: [%s\n", capStr) | |
} else if rowEnd < len(proc.Capabilities) { | |
fmt.Printf(" %s\n", capStr) | |
} else { | |
fmt.Printf(" %s]\n", capStr) | |
} | |
} | |
} | |
} | |
topology, err := ghw.Topology() | |
if err != nil { | |
fmt.Printf("Error getting topology info: %v", err) | |
} | |
fmt.Printf("%v\n", topology) | |
for _, node := range topology.Nodes { | |
fmt.Printf(" %v\n", node) | |
for _, cache := range node.Caches { | |
fmt.Printf(" %v\n", cache) | |
} | |
} | |
// =============== | |
fmt.Println("\n\nGPU\n================\n") | |
gpu, err := ghw.GPU() | |
if err != nil { | |
fmt.Printf("Error getting GPU info: %v", err) | |
} | |
fmt.Printf("%v\n", gpu) | |
for _, card := range gpu.GraphicsCards { | |
fmt.Printf(" %v\n", card) | |
fmt.Println(" Index:", card.Index) | |
fmt.Println(" Vendor:", card.DeviceInfo.Vendor.Name) | |
fmt.Println(" Product:", card.DeviceInfo.Product.Name) | |
} | |
// =============== | |
fmt.Println("\n\nPlatform\n================\n") | |
hostStat, err := host.Info() | |
if err != nil { | |
fmt.Printf("Error getting GPU info: %v", err) | |
} | |
fmt.Println("Name:", hostStat.Hostname, hostStat.HostID) | |
fmt.Println("Platform:", hostStat.Platform) | |
fmt.Println("OS:", hostStat.OS) | |
fmt.Println("Uptime minutes:", hostStat.Uptime/60) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment