Skip to content

Commit

Permalink
separete out metrics/darwin/swap.go from memory.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed May 9, 2015
1 parent 73a54b6 commit 76f33fa
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
1 change: 1 addition & 0 deletions command/command_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func specGenerators() []spec.Generator {
return []spec.Generator{
&specDarwin.KernelGenerator{},
&specDarwin.MemoryGenerator{},
&specDarwin.SwapGenerator{},
&specDarwin.CPUGenerator{},
&specDarwin.FilesystemGenerator{},
}
Expand Down
19 changes: 0 additions & 19 deletions metrics/darwin/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ Swapins: 0.
Swapouts: 0.
*/

/* sysctl vm.swapusage sample
% sysctl vm.swapusage
vm.swapusage: total = 1024.00M used = 2.50M free = 1021.50M (encrypted)
*/

var memoryLogger = logging.GetLogger("metrics.memory")
var statReg = regexp.MustCompile(`^(.+):\s+([0-9]+)\.$`)
var swapReg = regexp.MustCompile(`([0-9]+(?:\.[0-9]+))?M[^0-9]*([0-9]+(?:\.[0-9]+)?)M[^0-9]*([0-9]+(?:\.[0-9]+)?)M`)

// Generate generate metrics values
func (g *MemoryGenerator) Generate() (metrics.Values, error) {
Expand Down Expand Up @@ -98,18 +92,5 @@ func (g *MemoryGenerator) Generate() (metrics.Values, error) {
"memory.inactive": float64(inactive),
}

outBytes, err = exec.Command("sysctl", "vm.swapusage").Output()
if err != nil {
memoryLogger.Errorf("Failed (skip swap metrics): %s", err)
} else {
if matches := swapReg.FindStringSubmatch(string(outBytes)); matches != nil {
t, _ := strconv.ParseFloat(matches[1], 64)
// swap_used are calculated at server, so don't send it
// u, _ := strconv.ParseFloat(matches[2], 64)
f, _ := strconv.ParseFloat(matches[3], 64)
ret["memory.swap_total"] = t * 1024 * 1024
ret["memory.swap_free"] = f * 1024 * 1024
}
}
return metrics.Values(ret), nil
}
2 changes: 0 additions & 2 deletions metrics/darwin/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func TestMemoryGenerator(t *testing.T) {
"active",
"inactive",
"used",
"swap_total",
"swap_free",
} {
if v, ok := values["memory."+name]; !ok {
t.Errorf("memory should has %s", name)
Expand Down
58 changes: 58 additions & 0 deletions metrics/darwin/swap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// +build darwin

package darwin

import (
"fmt"
"os/exec"
"regexp"
"strconv"

"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/metrics"
)

/*
SwapGenerator collect swap usage
`memory.{metric}`: using swap size retrieved from `sysctl vm.swapusage`
metric = "swap_total", "swap_free"
graph: stacks `memory.{metric}`
*/
type SwapGenerator struct {
}

/* sysctl vm.swapusage sample
% sysctl vm.swapusage
vm.swapusage: total = 1024.00M used = 2.50M free = 1021.50M (encrypted)
*/

var swapLogger = logging.GetLogger("metrics.memory.swap")
var swapReg = regexp.MustCompile(`([0-9]+(?:\.[0-9]+)?)M[^0-9]*([0-9]+(?:\.[0-9]+)?)M[^0-9]*([0-9]+(?:\.[0-9]+)?)M`)

// Generate generate swap values
func (g *SwapGenerator) Generate() (metrics.Values, error) {
outBytes, err := exec.Command("sysctl", "vm.swapusage").Output()
if err != nil {
swapLogger.Errorf("Failed (skip these metrics): %s", err)
return nil, err
}

out := string(outBytes)
matches := swapReg.FindStringSubmatch(out)
if matches == nil || len(matches) != 4 {
return nil, fmt.Errorf("faild to parse vm.swapusage result: [%q]", out)
}
t, _ := strconv.ParseFloat(matches[1], 64)
// swap_used are calculated at server, so don't send it
// u, _ := strconv.ParseFloat(matches[2], 64)
f, _ := strconv.ParseFloat(matches[3], 64)

const mb = 1024.0 * 1024.0
return metrics.Values(map[string]float64{
"memory.swap_total": t * mb,
"memory.swap_free": f * mb,
}), nil
}
24 changes: 24 additions & 0 deletions metrics/darwin/swap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build darwin

package darwin

import "testing"

func TestSwapGenerator(t *testing.T) {
g := &SwapGenerator{}
values, err := g.Generate()
if err != nil {
t.Errorf("should not raise error: %v", err)
}

for _, name := range []string{
"swap_total",
"swap_free",
} {
if v, ok := values["memory."+name]; !ok {
t.Errorf("memory should has %s", name)
} else {
t.Logf("memory '%s' collected: %+v", name, v)
}
}
}

0 comments on commit 76f33fa

Please sign in to comment.