首页 > 其他分享 >golang获得基础硬件信息

golang获得基础硬件信息

时间:2023-05-09 22:27:33浏览次数:36  
标签:硬件 return nil err int 信息 golang json SliverHorn

package utils

import (
"runtime"
"time"

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

const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)

type Server struct {
Os Os `json:"os"`
Cpu Cpu `json:"cpu"`
Ram Ram `json:"ram"`
Disk Disk `json:"disk"`
}

type Os struct {
GOOS string `json:"goos"`
NumCPU int `json:"numCpu"`
Compiler string `json:"compiler"`
GoVersion string `json:"goVersion"`
NumGoroutine int `json:"numGoroutine"`
}

type Cpu struct {
Cpus []float64 `json:"cpus"`
Cores int `json:"cores"`
}

type Ram struct {
UsedMB int `json:"usedMb"`
TotalMB int `json:"totalMb"`
UsedPercent int `json:"usedPercent"`
}

type Disk struct {
UsedMB int `json:"usedMb"`
UsedGB int `json:"usedGb"`
TotalMB int `json:"totalMb"`
TotalGB int `json:"totalGb"`
UsedPercent int `json:"usedPercent"`
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: InitCPU
//@description: OS信息
//@return: o Os, err error

func InitOS() (o Os) {
o.GOOS = runtime.GOOS
o.NumCPU = runtime.NumCPU()
o.Compiler = runtime.Compiler
o.GoVersion = runtime.Version()
o.NumGoroutine = runtime.NumGoroutine()
return o
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: InitCPU
//@description: CPU信息
//@return: c Cpu, err error

func InitCPU() (c Cpu, err error) {
if cores, err := cpu.Counts(false); err != nil {
return c, err
} else {
c.Cores = cores
}
if cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true); err != nil {
return c, err
} else {
c.Cpus = cpus
}
return c, nil
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: InitRAM
//@description: RAM信息
//@return: r Ram, err error

func InitRAM() (r Ram, err error) {
if u, err := mem.VirtualMemory(); err != nil {
return r, err
} else {
r.UsedMB = int(u.Used) / MB
r.TotalMB = int(u.Total) / MB
r.UsedPercent = int(u.UsedPercent)
}
return r, nil
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: InitDisk
//@description: 硬盘信息
//@return: d Disk, err error

func InitDisk() (d Disk, err error) {
if u, err := disk.Usage("/"); err != nil {
return d, err
} else {
d.UsedMB = int(u.Used) / MB
d.UsedGB = int(u.Used) / GB
d.TotalMB = int(u.Total) / MB
d.TotalGB = int(u.Total) / GB
d.UsedPercent = int(u.UsedPercent)
}
return d, nil
}

标签:硬件,return,nil,err,int,信息,golang,json,SliverHorn
From: https://www.cnblogs.com/cheyunhua/p/17386469.html

相关文章

  • golang的zap日志切割
    packageinternalimport("github.com/flipped-aurora/gin-vue-admin/server/global"rotatelogs"github.com/lestrrat-go/file-rotatelogs""go.uber.org/zap/zapcore""os""path""time")varF......
  • 郭天祥书籍硬件查漏补缺
    ①二极管1.二极管具有正向导通特性,但需要一定的门槛,称为门槛电压。锗为0.2V,硅为0.6V2.实际的二极管具有压降,这个压降称为正向压降。锗为0.3V,硅为0.7V3.二极管的管芯分为点接触、面接触和平面型。这直接影响其允许通过的电流。4.二极管最重要的参数:最大正向电流,最大反向工......
  • 医院信息集成平台 HL7协议对接
    1.介绍HL7缩写于HealthLevelSeven,是创建于1987年,用来发展独立卫生保健行业的电子交换交换标准,经过多年的发展,HL7已经有多个版本。简单的理解其实就像XML,JSON格式一样,HL7也是一种数据格式,可以理解为一个包含很多行字符串的消息体,这一整个就是一个HL7消息内容。HL7官网http://ww......
  • 探索数字化转型新道路!流辰信息微服务与您一起创未来!
    科技在进步,社会在发展,办公自动化也在高速发展中。数字化转型是当下企业获得长久发展的趋势之一,在信息瞬间万变的社会中,谁掌握了核心技术,谁能与时代同步,谁就能开启新的康庄大道,谁就能在转型升级的道路中越走越顺畅。流辰信息微服务关注低代码开发市场,与时俱进,升级创新,为各大、中型......
  • IIS启动应用程序池报错"服务无法在此时接受控制信息"
    https://www.cnblogs.com/yaotome/p/9540300.html网站突然打不开,重新生成程序不行,重新打开vs也不行,重启了网站还是不行,重启应用池就发现问题了。可以关,启不来了,也删不掉,提示“服务无法在此时接受控制信息”。用下面方法解决了。用管理员方式打开命令行输入命令netsh winsock ......
  • golang中xorm自动维护表结构自动导入数据的实现
    Xorm简介Go标准库提供的数据库接口database/sql比较底层,使用它来操作数据库非常繁琐,而且容易出错。因而社区开源了不少第三方库,有各式各样的ORM(ObjectRelationalMapping,对象关系映射库),如gorm和xorm。其中xorm是一个简单但强大的ORM库,使用它可以大大简化我们的数据库操作,笔......
  • Golang GMP原理(2)
    GMP调度场景场景1P拥有G1,M1获取P后开始运行G1,G1使用gofunc创建G2,为了局部性G2优先加入到P1的本地队列场景2G1运行完成后(函数:goexit),M上运行的goroutine切换为G0,G0负责调度时协程的切换(函数:schedule)。从P的本地队列取G2,从G0切换到G2,并开始运行G2(函数:execute)。实现了线程......
  • Golang MySQL 操作
    1.  创建go_db目录      mkdirgo_db2. root@VirtualBox:/mnt/share/goframe/go_db#gomodinitgo_dbgo:creatingnewgo.mod:modulego_dbroot@VirtualBox:/mnt/share/goframe/go_db#goget-ugithub.com/go-sql-driver/mysqlgo:addedgithub.com/go-......
  • golang map key struct hash policy
     Theeasiestandmostflexiblewayistousea struct asthekeytype,includingallthedatayouwanttobepartofthekey,soinyourcase:typeKeystruct{X,Yint}Andthat'sall.Usingit:m:=map[Key]int{}m[Key{2,2}]=4m[Key{2......
  • 钉钉PC端使用 Blazor WebAssembly 读取用户信息
    功能:在钉钉内打开BlazorWebAssembly网站时,读取钉钉当前的用户信息,并显示启用的功能列表版本:.NET6界面库:AntDesignBlazor基础要求:1.让公司管理员设置自己为钉钉开发者2.下载钉钉RC版作为调试工具,下载地址:https://open.dingtalk.com/document/resource......