go 语言 获取物理机器主要参数
来源:互联网 发布:斗鱼qqq的淘宝店是什么 编辑:程序博客网 时间:2024/06/08 17:28
- 获取disk参数
- 命令行
- 通过proc虚拟文件系统
- go代码读取procdiskstats来实现
- 获取cpu参数
- 通过命令行
- 通过proc虚拟文件系统
- 获取memory参数
- 命令行
- 通过proc虚拟文件系统
- go代码读取procmeminfo 来实现
获取disk参数
命令行:
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 41153856 28058544 10998160 72% /
tmpfs 3925384 0 3925384 0% /dev/shm
/dev/sda1 487652 144402 317650 32% /boot
/dev/sda5 915383628 307557352 561320756 36% /home
通过/proc虚拟文件系统
cat /proc/diskstats
输出:
1 0 ram0 0 0 0 0 0 0 0 0 0 0 0
1 1 ram1 0 0 0 0 0 0 0 0 0 0 0
1 2 ram2 0 0 0 0 0 0 0 0 0 0 0
1 3 ram3 0 0 0 0 0 0 0 0 0 0 0
1 4 ram4 0 0 0 0 0 0 0 0 0 0 0
1 5 ram5 0 0 0 0 0 0 0 0 0 0 0
1 6 ram6 0 0 0 0 0 0 0 0 0 0 0
1 7 ram7 0 0 0 0 0 0 0 0 0 0 0
1 8 ram8 0 0 0 0 0 0 0 0 0 0 0
1 9 ram9 0 0 0 0 0 0 0 0 0 0 0
1 10 ram10 0 0 0 0 0 0 0 0 0 0 0
1 11 ram11 0 0 0 0 0 0 0 0 0 0 0
1 12 ram12 0 0 0 0 0 0 0 0 0 0 0
1 13 ram13 0 0 0 0 0 0 0 0 0 0 0
1 14 ram14 0 0 0 0 0 0 0 0 0 0 0
1 15 ram15 0 0 0 0 0 0 0 0 0 0 0
7 0 loop0 0 0 0 0 0 0 0 0 0 0 0
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0
7 2 loop2 0 0 0 0 0 0 0 0 0 0 0
7 3 loop3 0 0 0 0 0 0 0 0 0 0 0
7 4 loop4 0 0 0 0 0 0 0 0 0 0 0
7 5 loop5 0 0 0 0 0 0 0 0 0 0 0
7 6 loop6 0 0 0 0 0 0 0 0 0 0 0
7 7 loop7 0 0 0 0 0 0 0 0 0 0 0
11 0 sr0 0 0 0 0 0 0 0 0 0 0 0
8 0 sda 1344106 1663542 29293600 2377613 270750 5550066 46583400 27877737 0 3015302 30258156
8 1 sda1 696 275 27488 597 20 6 64 531 0 1011 1127
8 2 sda2 116010 51679 5829362 327116 167370 1100011 10139128 2335122 0 1163309 2662218
8 3 sda3 6206 23271 235816 39420 8468 856234 6928160 2516768 0 50862 2557927
8 4 sda4 3 0 12 86 0 0 0 0 0 86 86
8 5 sda5 1221018 1588316 23199530 2010088 94071 3593815 29516048 23017612 0 1998328 25028788
8 16 sdb 0 0 0 0 0 0 0 0 0 0 0
8 32 sdc 0 0 0 0 0 0 0 0 0 0 0
8 80 sdf 0 0 0 0 0 0 0 0 0 0 0
8 64 sde 0 0 0 0 0 0 0 0 0 0 0
8 48 sdd 0 0 0 0 0 0 0 0 0 0 0
go代码(读取/proc/diskstats来实现)
package diskimport "golang.org/x/sys/unix"// Usage returns a file system usage. path is a filessytem path such// as "/", not device file path like "/dev/vda1". If you want to use// a return value of disk.Partitions, use "Mountpoint" not "Device".func Usage(path string) (*UsageStat, error) { stat := unix.Statfs_t{} err := unix.Statfs(path, &stat) if err != nil { return nil, err } bsize := stat.Bsize ret := &UsageStat{ Path: path, Fstype: getFsType(stat), Total: (uint64(stat.Blocks) * uint64(bsize)), Free: (uint64(stat.Bavail) * uint64(bsize)), InodesTotal: (uint64(stat.Files)), InodesFree: (uint64(stat.Ffree)), } // if could not get InodesTotal, return empty if ret.InodesTotal < ret.InodesFree { return ret, nil } ret.InodesUsed = (ret.InodesTotal - ret.InodesFree) ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize) if ret.InodesTotal == 0 { ret.InodesUsedPercent = 0 } else { ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0 } if ret.Total == 0 { ret.UsedPercent = 0 } else { ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0 } return ret, nil}//返回的数据的结构体type UsageStat struct { Path string `json:"path"` Fstype string `json:"fstype"` Total uint64 `json:"total"` //总大小 Free uint64 `json:"free"` //空闲大小 Used uint64 `json:"used"` //已用部分 UsedPercent float64 `json:"usedPercent"` //磁盘使用率 InodesTotal uint64 `json:"inodesTotal"` InodesUsed uint64 `json:"inodesUsed"` InodesFree uint64 `json:"inodesFree"` InodesUsedPercent float64 `json:"inodesUsedPercent"`}通过github.com/shirou/gopsutil包调用:func disk_usage() { d, _ := disk.Usage("/") percent.disk_percent = d.UsedPercent}
获取cpu参数
通过命令行:
top
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 41153856 28058544 10998160 72% /
tmpfs 3925384 0 3925384 0% /dev/shm
/dev/sda1 487652 144402 317650 32% /boot
/dev/sda5 915383628 307557352 561320756 36% /home
….
通过/proc虚拟文件系统
/usr/bin/getconf
该进程主要用于监控系统cpu参数(可执行文件)
获取memory参数
命令行
free -m(基本块大小为M)
total used free shared buffers cached
Mem: 7666 6973 693 0 2784 2805
-/+ buffers/cache: 1383 6282
Swap: 4095 835 3260
通过/proc虚拟文件系统
cat /proc/meminfo (kernel 3.14+)
输出:是以键值对来存储的
MemTotal: 7850772 kB
MemFree: 711436 kB
Buffers: 2851584 kB
Cached: 2872556 kB
SwapCached: 79620 kB
Active: 1830980 kB
Inactive: 4040144 kB
Active(anon): 44892 kB
Inactive(anon): 102820 kB
Active(file): 1786088 kB
Inactive(file): 3937324 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4194300 kB
SwapFree: 3338684 kB
Dirty: 92 kB
Writeback: 0 kB
AnonPages: 76016 kB
Mapped: 19784 kB
Shmem: 672 kB
Slab: 1151128 kB
SReclaimable: 1114740 kB
SUnreclaim: 36388 kB
KernelStack: 5424 kB
PageTables: 15536 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 8119684 kB
Committed_AS: 3045240 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 366300 kB
VmallocChunk: 34359351684 kB
HardwareCorrupted: 0 kB
AnonHugePages: 32768 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 6144 kB
DirectMap2M: 1908736 kB
DirectMap1G: 6291456 kB
go代码(读取/proc/meminfo 来实现)
package memimport ( "strconv" "strings" "github.com/shirou/gopsutil/internal/common" "golang.org/x/sys/unix")func VirtualMemory() (*VirtualMemoryStat, error) { filename := common.HostProc("meminfo") lines, _ := common.ReadLines(filename) // flag if MemAvailable is in /proc/meminfo (kernel 3.14+) memavail := false ret := &VirtualMemoryStat{} for _, line := range lines { fields := strings.Split(line, ":") if len(fields) != 2 { continue } key := strings.TrimSpace(fields[0]) value := strings.TrimSpace(fields[1]) value = strings.Replace(value, " kB", "", -1) t, err := strconv.ParseUint(value, 10, 64) if err != nil { return ret, err } switch key { case "MemTotal": ret.Total = t * 1024 case "MemFree": ret.Free = t * 1024 case "MemAvailable": memavail = true ret.Available = t * 1024 case "Buffers": ret.Buffers = t * 1024 case "Cached": ret.Cached = t * 1024 case "Active": ret.Active = t * 1024 case "Inactive": ret.Inactive = t * 1024 case "Writeback": ret.Writeback = t * 1024 case "WritebackTmp": ret.WritebackTmp = t * 1024 case "Dirty": ret.Dirty = t * 1024 case "Shmem": ret.Shared = t * 1024 case "Slab": ret.Slab = t * 1024 case "PageTables": ret.PageTables = t * 1024 case "SwapCached": ret.SwapCached = t * 1024 } } if !memavail { ret.Available = ret.Free + ret.Buffers + ret.Cached } ret.Used = ret.Total - ret.Available ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0 return ret, nil}//返回的数据结构体,包含了memory的各种信息type VirtualMemoryStat struct {// Total amount of RAM on this system Total uint64 `json:"total"` //总内存大小 // RAM available for programs to allocate // // This value is computed from the kernel specific values. Available uint64 `json:"available"` //可用大小 // RAM used by programs // // This value is computed from the kernel specific values. Used uint64 `json:"used"` //已用内存大小 // Percentage of RAM used by programs // // This value is computed from the kernel specific values. UsedPercent float64 `json:"usedPercent"` //内存使用率 // This is the kernel's notion of free memory; RAM chips whose bits nobody // cares about the value of right now. For a human consumable number, // Available is what you really want. Free uint64 `json:"free"` //内存剩余大小 // OS X / BSD specific numbers: // http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/ Active uint64 `json:"active"` Inactive uint64 `json:"inactive"` Wired uint64 `json:"wired"` // Linux specific numbers // https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html // https://www.kernel.org/doc/Documentation/filesystems/proc.txt Buffers uint64 `json:"buffers"` //缓冲区占用内存大小 Cached uint64 `json:"cached"` //缓存大小 Writeback uint64 `json:"writeback"` Dirty uint64 `json:"dirty"` WritebackTmp uint64 `json:"writebacktmp"` Shared uint64 `json:"shared"` //共享内存大小 Slab uint64 `json:"slab"` PageTables uint64 `json:"pagetables"` SwapCached uint64 `json:"swapcached"`}}//UsedPercent大小计算: ret.Available = ret.Free + ret.Buffers + ret.Cached ret.Used = ret.Total - ret.Available ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) //通过free shell命令: total used free shared buffers cachedMem: 7850772 7175268 675504 676 2863600 2896044-/+ buffers/cache: 1415624 6435148Swap: 4194300 855496 3338804//通过github.com/shirou/gopsutil包调用:func mem_usage() { v, _ := mem.VirtualMemory() //返回的VirtualMemoryStat结构有各个不同的信息 percent.mem_percent = v.UsedPercent //mem_percent = (float64)((v.Used)/(v.Total))}
- go 语言 获取物理机器主要参数
- go 语言获取文件名、后缀
- Go语言获取环境变量代码
- go语言获取数组长度
- go语言获取新浪Cookie
- Go语言获取(*nux)系统环境变量
- Go语言获取字符串元素的个数
- go语言 获取post方式json
- Go语言学习(八)获取命令行参数
- go语言获取exe文件执行路径
- android机器获取当前语言
- 获取机器网卡的物理(MAC)地址
- php获取服务器端机器网卡的物理(MAC)地址
- 通过 WMI 获取远程机器 TotalPhysicalMemory(物理内存)
- C 语言getopt与go语言flag获取命令参数
- 【Go】Go语言初学
- Go语言
- Go语言
- SQL server数据库的一些基本操作
- no mapping found for http request with uri(源码角度分析)
- 设计模式
- 《JavaScript高级程序设计》创建对象之原型模式
- 弹性盒子Flex布局
- go 语言 获取物理机器主要参数
- java学习路线,你跑到哪儿了?
- Spring MVC和Struts2的比较
- 把数组排成最小的数
- 给定两个整形变量的值,在不创建临时变量的前提下,交换两个数的内容
- 对C++中的this指针的分析
- 记TP-Link面试被问到的问题
- 两张表用UNION关键字组合,用zTree显示树
- 丑数