diff --git a/go.mod b/go.mod index 4cd6fb1b16fd0c2a31edd327b9906503405fc14a..7162c84187c5c48dac6d488f9fcbbd7c335da13b 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pkg/sftp v1.13.4 github.com/satori/go.uuid v1.2.0 + github.com/shirou/gopsutil v3.21.11+incompatible github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.9.0 diff --git a/go.sum b/go.sum index af971ef2249d87412e6a5d1f3d9ed7ce354d875d..22e2a1df90a709dce35b86cdc1d2acffc02de160 100644 --- a/go.sum +++ b/go.sum @@ -307,6 +307,8 @@ github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYI github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= diff --git a/pkg/utils/os/cpu.go b/pkg/utils/os/cpu.go index cf1f67e0496206924f10e01798fbbf3562bf1a74..b760e7f88c9f6ac31982235130925df6822f35d7 100644 --- a/pkg/utils/os/cpu.go +++ b/pkg/utils/os/cpu.go @@ -1 +1,50 @@ package os + +import ( + "bytes" + "encoding/json" + "fmt" + "strconv" + "strings" + + "openeluer.org/PilotGo/PilotGo/pkg/utils" +) + +// 通过 /proc/cpuinfo来获取CPU型号 +type CPUInfo struct { + CpuName string + CpuNum int +} + +func (cpu *CPUInfo) String() string { + b, err := json.Marshal(*cpu) + if err != nil { + return fmt.Sprintf("%+v", *cpu) + } + var out bytes.Buffer + err = json.Indent(&out, b, "", " ") + if err != nil { + return fmt.Sprintf("%+v", *cpu) + } + return out.String() +} + +// 获取CPU型号 +func GetCPUName() string { + cpuname, _ := utils.RunCommand("cat /proc/cpuinfo | grep name | sort | uniq") + cpuname = strings.Replace(cpuname, "\n", "", -1) + str := len("model name : ") + cpuname = cpuname[str:] + return cpuname +} + +// 获取物理CPU个数 +func GetPhysicalCPU() int { + num, _ := utils.RunCommand("cat /proc/cpuinfo| grep 'physical id'| sort| uniq| wc -l") + num = strings.Replace(num, "\n", "", -1) + cpunum, err := strconv.Atoi(num) + if err != nil { + fmt.Println("获取cpu个数失败!") + } + return cpunum +} diff --git a/pkg/utils/os/system.go b/pkg/utils/os/system.go index c771132aadbd81b3c76e81112782639d7f44b0ec..3c1e53a5b8e6c7e2055d74debf7d6f2aefd659ad 100644 --- a/pkg/utils/os/system.go +++ b/pkg/utils/os/system.go @@ -1,11 +1,36 @@ package os -// TODO: 通过 /etc/os-release 文件获取系统信息 -func GetOSInfo() { +import ( + "encoding/json" + "fmt" + "strings" -} + "github.com/shirou/gopsutil/host" + "openeluer.org/PilotGo/PilotGo/pkg/utils" +) -// TODO: 获取系统启动时间,通过 /proc/uptime 获取 -func GetUpTime() { +type SystemInfo struct { + Platform string + PlatformVersion string + KernelVersion string + KernelArch string + HostId string + Uptime string +} +func GetHostInfo() string { + SysInfo, _ := host.Info() + Uptime := fmt.Sprintf("date -d '%v second ago'", SysInfo.Uptime) + uptime, _ := utils.RunCommand(Uptime) + uptime = strings.Replace(uptime, "\n", "", -1) + sysinfo := SystemInfo{ + Platform: SysInfo.Platform, + PlatformVersion: SysInfo.PlatformVersion, + KernelVersion: SysInfo.KernelVersion, + KernelArch: SysInfo.KernelArch, + HostId: SysInfo.HostID, + Uptime: uptime, + } + system, _ := json.Marshal(sysinfo) + return string(system) }