bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/vmstat_darwin.go (about)

     1  package collectors
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"bosun.org/metadata"
     8  	"bosun.org/opentsdb"
     9  	"bosun.org/util"
    10  )
    11  
    12  func init() {
    13  	collectors = append(collectors, &IntervalCollector{F: c_vmstat_darwin})
    14  }
    15  
    16  func c_vmstat_darwin() (opentsdb.MultiDataPoint, error) {
    17  	var md opentsdb.MultiDataPoint
    18  	var free float64
    19  	util.ReadCommand(func(line string) error {
    20  		if line == "" || strings.HasPrefix(line, "Object cache") || strings.HasPrefix(line, "Mach Virtual") {
    21  			return nil
    22  		}
    23  		fields := strings.Split(line, ":")
    24  		if len(fields) < 2 {
    25  			return nil
    26  		}
    27  		value, err := strconv.ParseFloat(strings.TrimSpace(fields[1]), 64)
    28  		if err != nil {
    29  			return nil
    30  		}
    31  		if strings.HasPrefix(fields[0], "Pages") {
    32  			name := strings.TrimSpace(fields[0])
    33  			name = strings.Replace(name, "Pages ", "", -1)
    34  			name = strings.Replace(name, " ", "", -1)
    35  			Add(&md, "darwin.mem.vm.4kpages."+name, value, nil, metadata.Unknown, metadata.None, "")
    36  			if name == "free" {
    37  				free = value * 4096
    38  				Add(&md, osMemFree, free, nil, metadata.Gauge, metadata.Bytes, osMemFreeDesc)
    39  			}
    40  		} else if fields[0] == "Pageins" {
    41  			Add(&md, "darwin.mem.vm.pageins", value, nil, metadata.Counter, metadata.None, "")
    42  		} else if fields[0] == "Pageouts" {
    43  			Add(&md, "darwin.mem.vm.pageouts", value, nil, metadata.Counter, metadata.None, "")
    44  		}
    45  		return nil
    46  	}, "vm_stat")
    47  	util.ReadCommand(func(line string) error {
    48  		total, _ := strconv.ParseFloat(line, 64)
    49  		if total == 0 {
    50  			return nil
    51  		}
    52  		Add(&md, osMemTotal, total, nil, metadata.Gauge, metadata.Bytes, osMemTotalDesc)
    53  		if free == 0 {
    54  			return nil
    55  		}
    56  		Add(&md, osMemUsed, total-free, nil, metadata.Gauge, metadata.Bytes, osMemUsedDesc)
    57  		Add(&md, osMemPctFree, free/total, nil, metadata.Gauge, metadata.Pct, osMemPctFreeDesc)
    58  		return nil
    59  	}, "sysctl", "-n", "hw.memsize")
    60  	return md, nil
    61  }