github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os/exec"
     8  	"runtime"
     9  
    10  	"github.com/pivotal-golang/lager"
    11  )
    12  
    13  //go:generate counterfeiter . Metrics
    14  
    15  type Metrics interface {
    16  	NumCPU() int
    17  	NumGoroutine() int
    18  	LoopDevices() int
    19  	BackingStores() int
    20  	DepotDirs() int
    21  }
    22  
    23  type metrics struct {
    24  	backingStoresPath string
    25  	depotPath         string
    26  	logger            lager.Logger
    27  }
    28  
    29  func NewMetrics(logger lager.Logger, backingStoresPath, depotPath string) Metrics {
    30  	return &metrics{
    31  		backingStoresPath: backingStoresPath,
    32  		depotPath:         depotPath,
    33  		logger:            logger.Session("metrics"),
    34  	}
    35  }
    36  
    37  func (m *metrics) NumCPU() int {
    38  	return runtime.NumCPU()
    39  }
    40  
    41  func (m *metrics) NumGoroutine() int {
    42  	return runtime.NumGoroutine()
    43  }
    44  
    45  func (m *metrics) LoopDevices() int {
    46  	devices, err := exec.Command("losetup", "-a").CombinedOutput()
    47  	if err != nil {
    48  		m.logger.Error("cannot-get-loop-devices", fmt.Errorf("%s, out: %s", err, string(devices)))
    49  		return -1
    50  	}
    51  	return bytes.Count(devices, []byte("\n"))
    52  }
    53  
    54  func (m *metrics) BackingStores() int {
    55  	entries, err := ioutil.ReadDir(m.backingStoresPath)
    56  	if err != nil {
    57  		m.logger.Error("cannot-get-backing-stores", err)
    58  		return -1
    59  	}
    60  
    61  	return len(entries)
    62  }
    63  
    64  func (m *metrics) DepotDirs() int {
    65  	entries, err := ioutil.ReadDir(m.depotPath)
    66  	if err != nil {
    67  		m.logger.Error("cannot-get-depot-dirs", err)
    68  		return -1
    69  	}
    70  
    71  	return len(entries)
    72  }