github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/catgo/cat-go/cat/monitor_collector.go (about)

     1  package cat
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strconv"
     7  
     8  	"github.com/shirou/gopsutil/cpu"
     9  	"github.com/shirou/gopsutil/load"
    10  )
    11  
    12  type Collector interface {
    13  	GetId() string
    14  	GetDesc() string
    15  	GetProperties() map[string]string
    16  }
    17  
    18  func b2kbstr(b uint64) string {
    19  	return strconv.Itoa(int(b / 1024))
    20  }
    21  
    22  func f642str(b float64) string {
    23  	return fmt.Sprintf("%f", b)
    24  }
    25  
    26  type memStatsCollector struct {
    27  	m runtime.MemStats
    28  
    29  	alloc,
    30  	mallocs,
    31  	lookups,
    32  	frees uint64
    33  }
    34  
    35  func (c *memStatsCollector) GetId() string {
    36  	return "mem.runtime"
    37  }
    38  
    39  func (c *memStatsCollector) GetDesc() string {
    40  	return "mem.runtime"
    41  }
    42  
    43  func (c *memStatsCollector) GetProperties() map[string]string {
    44  	runtime.ReadMemStats(&c.m)
    45  
    46  	m := map[string]string{
    47  		"mem.sys": b2kbstr(c.m.Sys),
    48  
    49  		// heap
    50  		"mem.heap.alloc":    b2kbstr(c.m.HeapAlloc),
    51  		"mem.heap.sys":      b2kbstr(c.m.HeapSys),
    52  		"mem.heap.idle":     b2kbstr(c.m.HeapIdle),
    53  		"mem.heap.inuse":    b2kbstr(c.m.HeapInuse),
    54  		"mem.heap.released": b2kbstr(c.m.HeapReleased),
    55  		"mem.heap.objects":  strconv.Itoa(int(c.m.HeapObjects)),
    56  
    57  		// stack
    58  		"mem.stack.inuse": b2kbstr(c.m.StackInuse),
    59  		"mem.stack.sys":   b2kbstr(c.m.StackSys),
    60  	}
    61  
    62  	if c.alloc > 0 {
    63  		m["mem.alloc"] = b2kbstr(c.m.TotalAlloc - c.alloc)
    64  		m["mem.mallocs"] = strconv.Itoa(int(c.m.Mallocs - c.mallocs))
    65  		m["mem.lookups"] = strconv.Itoa(int(c.m.Lookups - c.lookups))
    66  		m["mem.frees"] = strconv.Itoa(int(c.m.Frees - c.frees))
    67  	}
    68  	c.alloc = c.m.TotalAlloc
    69  	c.mallocs = c.m.Mallocs
    70  	c.lookups = c.m.Lookups
    71  	c.frees = c.m.Frees
    72  
    73  	return m
    74  }
    75  
    76  type cpuInfoCollector struct {
    77  	lastTime    *cpu.TimesStat
    78  	lastCPUTime float64
    79  }
    80  
    81  func (c *cpuInfoCollector) GetId() string {
    82  	return "cpu"
    83  }
    84  
    85  func (c *cpuInfoCollector) GetDesc() string {
    86  	return "cpu"
    87  }
    88  
    89  func (c *cpuInfoCollector) GetProperties() map[string]string {
    90  
    91  	m := make(map[string]string)
    92  
    93  	if avg, err := load.Avg(); err == nil {
    94  		m["load.1min"] = f642str(avg.Load1)
    95  		m["load.5min"] = f642str(avg.Load5)
    96  		m["load.15min"] = f642str(avg.Load15)
    97  		m["system.load.average"] = m["load.1min"]
    98  	}
    99  
   100  	if times, err := cpu.Times(false); err == nil {
   101  		if len(times) > 0 {
   102  			currentTime := times[0]
   103  
   104  			currentCpuTime := 0.0 +
   105  				currentTime.User +
   106  				currentTime.System +
   107  				currentTime.Idle +
   108  				currentTime.Nice +
   109  				currentTime.Iowait +
   110  				currentTime.Irq +
   111  				currentTime.Softirq +
   112  				currentTime.Steal +
   113  				currentTime.Guest +
   114  				currentTime.GuestNice
   115  				// +
   116  				// currentTime.Stolen
   117  
   118  			if c.lastCPUTime > 0 {
   119  				cpuTime := currentCpuTime - c.lastCPUTime
   120  
   121  				if cpuTime > 0.0 {
   122  					user := currentTime.User - c.lastTime.User
   123  					system := currentTime.System - c.lastTime.System
   124  					nice := currentTime.Nice - c.lastTime.Nice
   125  					idle := currentTime.Idle - c.lastTime.Idle
   126  					iowait := currentTime.Iowait - c.lastTime.Iowait
   127  					softirq := currentTime.Softirq - c.lastTime.Softirq
   128  					irq := currentTime.Irq - c.lastTime.Irq
   129  					steal := currentTime.Steal - c.lastTime.Steal
   130  
   131  					m["cpu.user"] = f642str(user)
   132  					m["cpu.sys"] = f642str(system)
   133  					m["cpu.nice"] = f642str(nice)
   134  					m["cpu.idle"] = f642str(idle)
   135  					m["cpu.iowait"] = f642str(iowait)
   136  					m["cpu.softirq"] = f642str(softirq)
   137  					m["cpu.irq"] = f642str(irq)
   138  					m["cpu.steal"] = f642str(steal)
   139  
   140  					m["cpu.user.percent"] = f642str(user / cpuTime * 100)
   141  					m["cpu.sys.percent"] = f642str(system / cpuTime * 100)
   142  					m["cpu.nice.percent"] = f642str(nice / cpuTime * 100)
   143  					m["cpu.idle.percent"] = f642str(idle / cpuTime * 100)
   144  					m["cpu.iowait.percent"] = f642str(iowait / cpuTime * 100)
   145  					m["cpu.softirq.percent"] = f642str(softirq / cpuTime * 100)
   146  					m["cpu.irq.percent"] = f642str(irq / cpuTime * 100)
   147  					m["cpu.steal.percent"] = f642str(steal / cpuTime * 100)
   148  				}
   149  			}
   150  			c.lastCPUTime = currentCpuTime
   151  			c.lastTime = &currentTime
   152  		}
   153  	}
   154  
   155  	// TODO process status
   156  	// if processes, err := process.Processes(); err == nil {
   157  	// 	for _, p := range processes {
   158  	// 	}
   159  	// }
   160  
   161  	return m
   162  }