-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separete out metrics/darwin/swap.go from memory.go
- Loading branch information
Showing
5 changed files
with
83 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |