func configMemoryRoutes() { http.HandleFunc("/page/memory", func(w http.ResponseWriter, r *http.Request) { mem, err := nux.MemInfo() if err != nil { RenderMsgJson(w, err.Error()) return } memFree := mem.MemFree + mem.Buffers + mem.Cached memUsed := mem.MemTotal - memFree var t uint64 = 1024 * 1024 RenderDataJson(w, []interface{}{mem.MemTotal / t, memUsed / t, memFree / t}) }) http.HandleFunc("/proc/memory", func(w http.ResponseWriter, r *http.Request) { mem, err := nux.MemInfo() if err != nil { RenderMsgJson(w, err.Error()) return } memFree := mem.MemFree + mem.Buffers + mem.Cached memUsed := mem.MemTotal - memFree RenderDataJson(w, map[string]interface{}{ "total": mem.MemTotal, "free": memFree, "used": memUsed, }) }) }
func heartbeat(duration time.Duration) { mem, err := nux.MemInfo() if err != nil { log.Println("[ERROR] get meminfo:", err) return } // use MB memFree := (mem.MemFree + mem.Buffers + mem.Cached) / 1024 / 1024 localIp := g.Config().LocalIp containers, err := dock.Containers(g.Config().Docker) if err != nil { log.Println("[ERROR] list containers fail:", err) // this node dead var resp model.NodeResponse err = g.RpcClient.Call("NodeState.NodeDown", localIp, &resp) if err != nil || resp.Code != 0 { log.Println("[ERROR] call rpc: NodeState.NodeDown fail:", err, "resp:", resp) } else if g.Config().Debug { log.Println("[INFO] call rpc: NodeState.NodeDown successfully. I am dead...") } for { time.Sleep(duration) containers, err = dock.Containers(g.Config().Docker) if err == nil { break } else { log.Println("[ERROR] list containers fail:", err) } } } req := model.NodeRequest{ Node: model.Node{ Ip: localIp, MemFree: memFree, }, Containers: containers, } var resp model.NodeResponse err = g.RpcClient.Call("NodeState.Push", req, &resp) if err != nil || resp.Code != 0 { log.Println("[ERROR] call rpc: NodeState.Push fail:", err, "resp:", resp) } else if g.Config().Debug { log.Println("[DEBUG] =>", req) } }
func MemMetrics() []*model.MetricValue { m, err := nux.MemInfo() if err != nil { log.Println(err) return nil } memFree := m.MemFree + m.Buffers + m.Cached memUsed := m.MemTotal - memFree pmemFree := 0.0 pmemUsed := 0.0 if m.MemTotal != 0 { pmemFree = float64(memFree) * 100.0 / float64(m.MemTotal) pmemUsed = float64(memUsed) * 100.0 / float64(m.MemTotal) } pswapFree := 0.0 pswapUsed := 0.0 if m.SwapTotal != 0 { pswapFree = float64(m.SwapFree) * 100.0 / float64(m.SwapTotal) pswapUsed = float64(m.SwapUsed) * 100.0 / float64(m.SwapTotal) } return []*model.MetricValue{ GaugeValue("mem.memtotal", m.MemTotal), GaugeValue("mem.memused", memUsed), GaugeValue("mem.memfree", memFree), GaugeValue("mem.swaptotal", m.SwapTotal), GaugeValue("mem.swapused", m.SwapUsed), GaugeValue("mem.swapfree", m.SwapFree), GaugeValue("mem.memfree.percent", pmemFree), GaugeValue("mem.memused.percent", pmemUsed), GaugeValue("mem.swapfree.percent", pswapFree), GaugeValue("mem.swapused.percent", pswapUsed), } }