system
system copied to clipboard
build failure under go 1.5.3, ubuntu 14.04
# go get github.com/statsd/system
# github.com/statsd/system/pkg/memory
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:66: cannot use stat (type *linux.MemInfo) as type linux.MemInfo in argument to percent
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:67: cannot use stat (type *linux.MemInfo) as type linux.MemInfo in argument to swapPercent
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:70: invalid operation: stat["MemTotal"] (type *linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:71: cannot use stat (type *linux.MemInfo) as type linux.MemInfo in argument to used
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:72: invalid operation: stat["MemFree"] (type *linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:73: invalid operation: stat["Active"] (type *linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:74: invalid operation: stat["SwapTotal"] (type *linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:75: invalid operation: stat["SwapFree"] (type *linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:93: invalid operation: s["SwapTotal"] (type linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:94: invalid operation: s["SwapFree"] (type linux.MemInfo does not support indexing)
/data/go/pkg/src/github.com/statsd/system/pkg/memory/memory.go:94: too many errors
Seeing the same problem on Ubuntu 16.04 with go1.6.2 on xLinux.
That's because MemInfo changed to a struct. Before that, it was a type MemInfo map[string]uint64
. The easy fix is to checkout github.com/c9s/goprocinfo/linux to 1cb208c8060065fad6c43524586d01ee05a78a6a.
This should fix it:
diff --git a/pkg/memory/memory.go b/pkg/memory/memory.go
index 6f80b15..fc83197 100644
--- a/pkg/memory/memory.go
+++ b/pkg/memory/memory.go
@@ -1,5 +1,4 @@
-//
-// Memory resource.
+// Package memory defines Memory resource.
//
// This collector reports on the following meminfo metrics:
//
@@ -67,12 +66,12 @@ func (m *Memory) Report() {
m.client.Gauge("swap.percent", swapPercent(stat))
if m.Extended {
- m.client.Gauge("total", bytes(stat["MemTotal"]))
+ m.client.Gauge("total", bytes(stat.MemTotal))
m.client.Gauge("used", bytes(used(stat)))
- m.client.Gauge("free", bytes(stat["MemFree"]))
- m.client.Gauge("active", bytes(stat["Active"]))
- m.client.Gauge("swap.total", bytes(stat["SwapTotal"]))
- m.client.Gauge("swap.free", bytes(stat["SwapFree"]))
+ m.client.Gauge("free", bytes(stat.MemFree))
+ m.client.Gauge("active", bytes(stat.Active))
+ m.client.Gauge("swap.total", bytes(stat.SwapTotal))
+ m.client.Gauge("swap.free", bytes(stat.SwapFree))
}
case <-m.exit:
@@ -89,9 +88,9 @@ func (m *Memory) Stop() error {
}
// calculate swap percentage.
-func swapPercent(s linux.MemInfo) int {
- total := s["SwapTotal"]
- used := total - s["SwapFree"]
+func swapPercent(s *linux.MemInfo) int {
+ total := s.SwapTotal
+ used := total - s.SwapFree
p := float64(used) / float64(total) * 100
if math.IsNaN(p) {
@@ -102,8 +101,8 @@ func swapPercent(s linux.MemInfo) int {
}
// calculate percentage.
-func percent(s linux.MemInfo) int {
- total := s["MemTotal"]
+func percent(s *linux.MemInfo) int {
+ total := s.MemTotal
p := float64(used(s)) / float64(total) * 100
if math.IsNaN(p) {
@@ -114,8 +113,8 @@ func percent(s linux.MemInfo) int {
}
// used memory.
-func used(s linux.MemInfo) uint64 {
- return s["MemTotal"] - s["MemFree"] - s["Buffers"] - s["Cached"]
+func used(s *linux.MemInfo) uint64 {
+ return s.MemTotal - s.MemFree - s.Buffers - s.Cached
}
// convert to bytes.
Thanks guys for fixing! Can you tell me how to apply the patch? I am not familiar with how go gets the packages from git before compiling. And I am unsure if your fix is already on git, or if I have to run it on the machine where I will compile it...