试着获取cpu serial

This commit is contained in:
suguo.yao 2021-12-20 16:08:08 +08:00
parent a96ba8549c
commit ceff1f0abb
5 changed files with 152 additions and 0 deletions

18
computer/computer.go Normal file
View File

@ -0,0 +1,18 @@
package computer
import (
"os/exec"
"regexp"
)
func CpuID() (*string, error) {
cmd := exec.Command("wmic", "cpu", "get", "ProcessorID")
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
reg := regexp.MustCompile(`\s+`)
str := reg.ReplaceAllString(string(out), "")
id := str[11:]
return &id, nil
}

14
computer/computer_test.go Normal file
View File

@ -0,0 +1,14 @@
package computer
import (
"fmt"
"testing"
)
func TestCpuID(t *testing.T) {
id, err := CpuID()
if err != nil {
t.Fatal(err)
}
fmt.Println(id)
}

43
cpu/cpu.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"math"
"strings"
"github.com/jaypipes/ghw"
)
func main() {
cpu, err := ghw.CPU()
if err != nil {
fmt.Printf("Error getting CPU info: %v", err)
}
fmt.Printf("%v\n", cpu)
for _, proc := range cpu.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)
}
}
}
}
}

64
example/example.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"fmt"
"github.com/intel-go/cpuid"
)
func main() {
fmt.Printf("VendorString: %s\n", cpuid.VendorIdentificatorString)
fmt.Printf("ProcessorBrandString: %s\n", cpuid.ProcessorBrandString)
fmt.Printf("SteppingId: %d\n", cpuid.SteppingId)
fmt.Printf("ProcessorType: %d\n", cpuid.ProcessorType)
fmt.Printf("DisplayFamily: %d\n", cpuid.DisplayFamily)
fmt.Printf("DisplayModel: %d\n", cpuid.DisplayModel)
fmt.Printf("CacheLineSize: %d\n", cpuid.CacheLineSize)
fmt.Printf("MaxLogicalCPUId:%d\n", cpuid.MaxLogicalCPUId)
fmt.Printf("InitialAPICId: %d\n", cpuid.InitialAPICId)
fmt.Printf("Smallest monitor-line size in bytes: %d\n", cpuid.MonLineSizeMin)
fmt.Printf("Largest monitor-line size in bytes: %d\n", cpuid.MonLineSizeMax)
fmt.Printf("Monitor Interrupt break-event is supported: %v\n", cpuid.MonitorIBE)
fmt.Printf("MONITOR/MWAIT extensions are supported: %v\n", cpuid.MonitorEMX)
fmt.Printf("AVX state: %v\n", cpuid.EnabledAVX)
fmt.Printf("AVX-512 state: %v\n", cpuid.EnabledAVX512)
fmt.Printf("Interrupt thresholds in digital thermal sensor: %v\n", cpuid.ThermalSensorInterruptThresholds)
fmt.Printf("Features: ")
for i := uint64(0); i < 64; i++ {
if cpuid.HasFeature(1 << i) {
fmt.Printf("%s ", cpuid.FeatureNames[1<<i])
}
}
fmt.Printf("\n")
fmt.Printf("ExtendedFeatures: ")
for i := uint64(0); i < 64; i++ {
if cpuid.HasExtendedFeature(1 << i) {
fmt.Printf("%s ", cpuid.ExtendedFeatureNames[1<<i])
}
}
fmt.Printf("\n")
fmt.Printf("ExtraFeatures: ")
for i := uint64(0); i < 64; i++ {
if cpuid.HasExtraFeature(1 << i) {
fmt.Printf("%s ", cpuid.ExtraFeatureNames[1<<i])
}
}
fmt.Printf("\n")
fmt.Printf("ThermalAndPowerFeatures: ")
for i := uint32(0); i < 64; i++ {
if cpuid.HasThermalAndPowerFeature(1 << i) {
if name, found := cpuid.ThermalAndPowerFeatureNames[1<<i]; found {
fmt.Printf("%s ", name)
}
}
}
fmt.Printf("\n")
for _, cacheDescription := range cpuid.CacheDescriptors {
fmt.Printf("CacheDescriptor: %v\n", cacheDescription)
}
}

13
go.mod
View File

@ -4,5 +4,18 @@ go 1.17
require (
github.com/influxdata/influxdb v1.9.5
github.com/intel-go/cpuid v0.0.0-20210602155658-5747e5cec0d9
github.com/jaypipes/ghw v0.8.0
github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf
)
require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/jaypipes/pcidb v0.6.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
howett.net/plist v0.0.0-20181124034731-591f970eefbb // indirect
)