This is the 21st day of my participation in the August More Text Challenge

Introduction to the

Gopsutil is the go language version of Psutil. Its main function is to obtain various monitoring information on the system, including but not limited to CPU, memory, disk, network, process, docker, etc

Gopsutil supports multiple cross-platform architectures, including i386/ AMD64 / ARM architectures, as well as various systems such as Windows and Linux, with Linux being the most supported

The latest version of Gopsutil is V3. The default version is V2

"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/mem"  // to use v2
Copy the code

Official website: github.com/shirou/gops…

Application scenarios

Go language itself has the characteristics of high concurrency, no environment dependent deployment, lightweight, low memory usage, and Gopsutil can do performance monitoring, system monitoring or system information collection, information reporting, etc

The child package

Gopsutil divides different functions into different subpackages. When used, it directly imports the subpackages and then calls them. Subpackages are classified as follows:

  • The CPU;
  • Disk Disk related
  • Host Host related
  • Mem memory related
  • Net Network related
  • Process Process correlation
  • Docke docker related

In actual production, most servers are Linux, so windows-related packages are not covered here

use

In the output, the gopusutil package has converted the data format to JSON. It also implements the String() method, when you output the whole object in JSON format, and when you output a single value as String

Collecting CPU Information

The INFO method contains all CPU information, the Percent method displays CPU usage, and the load value requires the load package

// Displays CPU information
info, _ := cpu.Info()
for _, ci := range info {
   fmt.Println(ci)
}

// Prints the CPU usage every 5 seconds for a total of 9 times
for i := 1; i < 10; i++ {
   time.Sleep(time.Millisecond * 5000)
   percent, _ := cpu.Percent(time.Second, false)
   fmt.Printf("%v, cpu percent: %v", i, percent)
}

// Display CPU load value
avg, _ := load.Avg()
fmt.Println(avg)
Copy the code

Collecting Memory Information

The VirtualMemory method displays physical memory information, and the SwapMemory method displays SwapMemory information

// Displays physical memory information
memory, _ := mem.VirtualMemory()
fmt.Printf("Total: %v, Free:%v,UsedPercent:%f%%\n",memory.Total, memory.Free, memory.UsedPercent)
// Displays swap memory information
swapMemory, _ := mem.SwapMemory()
fmt.Println(swapMemory)
Copy the code

Collecting Host Information

// Displays the machine startup timestamp
bootTime, _ := host.BootTime()
fmt.Println(bootTime)
// Display machine information
info, _ := host.Info()
fmt.Println(info)
// Display the end user
users, _ := host.Users()
for _,user := range users {
   fmt.Println(user.User)
}
Copy the code

Collect disk information

The Partitions method displays all partition information, and the Usage method displays the partition Usage

// Displays disk partition information
partitions, _ := disk.Partitions(true)
for _,part := range partitions {
   fmt.Printf("part:%v\n",part.String())
   usage, _ := disk.Usage(part.Mountpoint)
   fmt.Printf("disk info:used :%v free:%v\n",usage.UsedPercent,usage.Free)
}

// Displays disk partition I/O information
counters, _ := disk.IOCounters()
for k,v := range counters {
   fmt.Printf("%v,%v\n",k,v)
}
Copy the code

Collecting Process Information

// Display all process names and PID
processes, _ := process.Processes()
for _,process := range processes {
   fmt.Println(process.Pid)
   fmt.Println(process.Name())
}
Copy the code

Collecting Network Information

To avoid conflicts with internal package NET, change to net2

// Display the display information and IO
counters, _ := net2.IOCounters(true)
for k,v := range  counters{
   fmt.Printf("%v:%v send:%v recv:%v\n", k, v, v.BytesSent, v.BytesRecv)
}
Copy the code

Collect Docker information

// Displays a list of dockerids
list, _ := docker.GetDockerIDList()
for _,v := range list{
   fmt.Println(v)
}
Copy the code

conclusion

Gopsutil toolkit can be used to easily obtain system information, information collection, system monitoring and other scenarios can be easily implemented

For complex process management, the process subpackage function calls are also complex. It is recommended that another operating system use systemctl to manage the process package: github.com/coreos/go-s…