gitee.com/h79/goutils@v1.22.10/common/system/info.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/shirou/gopsutil/v3/cpu"
     6  	"github.com/shirou/gopsutil/v3/disk"
     7  	"github.com/shirou/gopsutil/v3/mem"
     8  )
     9  
    10  func Info() string {
    11  	d := Disk()
    12  	m := Mem()
    13  	return SIToString(&d, &m, CpuPercent())
    14  }
    15  
    16  func SIToString(d *DiskPath, m *MemInfo, cpuPercent float64) string {
    17  	in := fmt.Sprintf("Cpu=> UsedPercent: %.2f\r\n"+
    18  		"Memory=> Total: %v, Available: %v, Used: %v, UsedPercent: %.2f\r\n"+
    19  		"Disk=> Path: %v, Total: %v, Free: %v, Used: %v, UsedPercent: %.2f\r\n",
    20  		cpuPercent,
    21  		m.Total, m.Available, m.Used, m.UsedPercent,
    22  		d.Path, d.Total, d.Free, d.Used, d.UsedPercent)
    23  	return in
    24  }
    25  
    26  const GB = 1024 * 1024 * 1024
    27  
    28  func Mem() MemInfo {
    29  
    30  	v, er := mem.VirtualMemory()
    31  	if er != nil {
    32  		return MemInfo{}
    33  	}
    34  	return MemInfo{
    35  		Total:       v.Total / GB,
    36  		Available:   v.Available / GB,
    37  		Used:        v.Used / GB,
    38  		UsedPercent: v.UsedPercent,
    39  	}
    40  }
    41  
    42  type MemInfo struct {
    43  	// Total amount of RAM on this system
    44  	Total uint64 `json:"total"`
    45  
    46  	// RAM available for programs to allocate
    47  	//
    48  	// This value is computed from the kernel specific values.
    49  	Available uint64 `json:"available"`
    50  
    51  	// RAM used by programs
    52  	//
    53  	// This value is computed from the kernel specific values.
    54  	Used uint64 `json:"used"`
    55  
    56  	// Percentage of RAM used by programs
    57  	//
    58  	// This value is computed from the kernel specific values.
    59  	UsedPercent float64 `json:"usedPercent"`
    60  }
    61  
    62  func CpuPercent() float64 {
    63  	in, er := cpu.Percent(0, false)
    64  	if er != nil {
    65  		return 0
    66  	}
    67  	return in[0]
    68  }
    69  
    70  func Cpu() CpuInfo {
    71  	in, er := cpu.Times(false) //(0*time.Second, false)
    72  	if er != nil {
    73  		return CpuInfo{}
    74  	}
    75  	var inf CpuInfo
    76  	for i := range in {
    77  		inf.User = in[i].User
    78  		inf.System = in[i].System
    79  		inf.Idle = in[i].Idle
    80  		inf.Nice = in[i].Nice
    81  	}
    82  	return inf
    83  }
    84  
    85  type CpuInfo struct {
    86  	User   float64 `json:"user"`
    87  	System float64 `json:"system"`
    88  	Idle   float64 `json:"idle"`
    89  	Nice   float64 `json:"nice"`
    90  }
    91  
    92  func Disk() DiskPath {
    93  	return DiskWithPath("/")
    94  }
    95  
    96  func DiskWithPath(path string) DiskPath {
    97  	d, err := disk.Usage(path)
    98  	if err != nil {
    99  		return DiskPath{}
   100  	}
   101  	return DiskPath{
   102  		Path:        d.Path,
   103  		Total:       d.Total / GB,
   104  		Free:        d.Free / GB,
   105  		Used:        d.Used / GB,
   106  		UsedPercent: d.UsedPercent,
   107  	}
   108  }
   109  
   110  func DiskAll() []DiskPath {
   111  	var paths []DiskPath
   112  	part, er := disk.Partitions(false)
   113  	if er != nil {
   114  		return paths
   115  	}
   116  	for i := range part {
   117  		if d, err := disk.Usage(part[i].Mountpoint); err == nil {
   118  			var dp = DiskPath{
   119  				Path:        d.Path,
   120  				Total:       d.Total / GB,
   121  				Free:        d.Free / GB,
   122  				Used:        d.Used / GB,
   123  				UsedPercent: d.UsedPercent,
   124  			}
   125  			if dp.Total > 0 {
   126  				paths = append(paths, dp)
   127  			}
   128  		}
   129  	}
   130  	return paths
   131  }
   132  
   133  type DiskPath struct {
   134  	Path        string  `json:"path,omitempty"`
   135  	Total       uint64  `json:"total"`
   136  	Free        uint64  `json:"free"`
   137  	Used        uint64  `json:"used"`
   138  	UsedPercent float64 `json:"usedPercent"`
   139  }