How to get system information in Go?

huangapple go评论94阅读模式
英文:

How to get system information in Go?

问题

有人可以推荐一个可以用来获取系统信息的模块吗,类似于Python的psutil吗?

当我尝试使用>go get github.com/golang/sys get 'sys'时,我收到了以下错误信息:

报告错误:
package github.com/golang/sys
        导入 github.com/golang/sys
        导入 github.com/golang/sys: 在 D:\go_source\src\github.com\golang\sys 中没有可构建的Go源文件

这是我的系统环境:

# 本地编译器 windows amd64

GOROOT=D:\Go
#GOBIN=
GOARCH=amd64
GOOS=windows
CGO_ENABLED=1

PATH=c:\mingw64\bin;%GOROOT%\bin;%PATH%

LITEIDE_GDB=gdb64
LITEIDE_MAKE=mingw32-make
LITEIDE_TERM=%COMSPEC%
LITEIDE_TERMARGS=
LITEIDE_EXEC=%COMSPEC%
LITEIDE_EXECOPT=/C

请注意,我只会返回翻译好的部分,不会回答关于翻译的问题。

英文:

Can anyone recommend a module which can be used to get system information, like Python's psutil?

When I tried >go get github.com/golang/sys get 'sys', I received the following:

Report Error:
package github.com/golang/sys
        imports github.com/golang/sys
        imports github.com/golang/sys: no buildable Go source files in D:\go_source\src\github.com\golang\sys

This my system environment:

# native compiler windows amd64

GOROOT=D:\Go
#GOBIN=
GOARCH=amd64
GOOS=windows
CGO_ENABLED=1

PATH=c:\mingw64\bin;%GOROOT%\bin;%PATH%

LITEIDE_GDB=gdb64
LITEIDE_MAKE=mingw32-make
LITEIDE_TERM=%COMSPEC%
LITEIDE_TERMARGS=
LITEIDE_EXEC=%COMSPEC%
LITEIDE_EXECOPT=/C

答案1

得分: 7

这是一个简单的Windows示例,用于提取主机名、平台、CPU型号、总内存和磁盘容量。首先安装模块:

go get github.com/shirou/gopsutil

我在安装过程中遇到了问题,还需要安装:

go get github.com/StackExchange/wmi

现在运行这段代码:

package main

import (
    "fmt"

    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/disk"
    "github.com/shirou/gopsutil/host"
    "github.com/shirou/gopsutil/mem"
)

// SysInfo保存基本的系统信息
type SysInfo struct {
    Hostname string `bson:hostname`
    Platform string `bson:platform`
    CPU      string `bson:cpu`
    RAM      uint64 `bson:ram`
    Disk     uint64 `bson:disk`
}

func main() {
    hostStat, _ := host.Info()
    cpuStat, _ := cpu.Info()
    vmStat, _ := mem.VirtualMemory()
    diskStat, _ := disk.Usage("\\") // 如果你在Unix系统上,请将这里的"\\"更改为"/"

    info := new(SysInfo)

    info.Hostname = hostStat.Hostname
    info.Platform = hostStat.Platform
    info.CPU = cpuStat[0].ModelName
    info.RAM = vmStat.Total / 1024 / 1024
    info.Disk = diskStat.Total / 1024 / 1024

    fmt.Printf("%+v\n", info)
}
英文:

This is a simple Windows example to extract the Hostname, Platform, CPU model, total RAM and disk capacity. First install the module:

go get github.com/shirou/gopsutil 

I had problems with the installation and I also had to install:

go get github.com/StackExchange/wmi

Now run this code:

package main

import (
	"fmt"

	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/mem"
)

// SysInfo saves the basic system information
type SysInfo struct {
	Hostname string `bson:hostname`
	Platform string `bson:platform`
	CPU      string `bson:cpu`
	RAM      uint64 `bson:ram`
	Disk     uint64 `bson:disk`
}

func main() {
	hostStat, _ := host.Info()
	cpuStat, _ := cpu.Info()
	vmStat, _ := mem.VirtualMemory()
	diskStat, _ := disk.Usage("\\") // If you're in Unix change this "\\" for "/"

	info := new(SysInfo)

	info.Hostname = hostStat.Hostname
	info.Platform = hostStat.Platform
	info.CPU = cpuStat[0].ModelName
	info.RAM = vmStat.Total / 1024 / 1024
	info.Disk = diskStat.Total / 1024 / 1024

	fmt.Printf("%+v\n", info)

}

答案2

得分: 5

你需要根据你的操作系统执行以下操作(参考godoc):

go get golang.org/x/sys/unix
# 或者
go get golang.org/x/sys/windows
# 或者
go get golang.org/x/sys/plan9

(根据你的操作系统而定)

英文:

You would need actually to do (following godoc):

go get golang.org/x/sys/unix
# or
go get golang.org/x/sys/windows
# or
go get golang.org/x/sys/plan9

(depending on your OS)

答案3

得分: 4

我知道这是一个旧帖子,但我还是想为其他可以受益的人提供帮助。

这里有你正在寻找的东西:

https://github.com/shirou/gopsutil

英文:

I know this is an old post, but putting this out there for others who can benefit.

exactly what you're looking for here:

https://github.com/shirou/gopsutil

huangapple
  • 本文由 发表于 2015年5月7日 14:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/30093700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定