2017-10-23 05:31:04 +00:00
|
|
|
package stat
|
|
|
|
|
|
|
|
import (
|
2017-10-23 09:08:19 +00:00
|
|
|
"bufio"
|
2017-10-23 05:31:04 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-10-23 06:59:38 +00:00
|
|
|
type MemStat struct {
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 06:59:38 +00:00
|
|
|
func (mem MemStat) Command() string {
|
2017-10-23 05:31:04 +00:00
|
|
|
return "cat /proc/meminfo"
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:09:17 +00:00
|
|
|
func (mem MemStat) Read(r io.Reader, keys []string) (*map[string]string, error) {
|
2017-10-23 05:31:04 +00:00
|
|
|
var (
|
2017-10-24 07:09:17 +00:00
|
|
|
stats = map[string]string{}
|
2017-10-23 05:31:04 +00:00
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
key := parts[0][:len(parts[0])-1]
|
2017-10-24 07:09:17 +00:00
|
|
|
|
|
|
|
stats[key] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := mem.parse(keys, stats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mem MemStat) parse(keys []string, data map[string]string) (map[string]string, error) {
|
|
|
|
resMap := make(map[string]string)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
switch key {
|
|
|
|
case "mem.usage.total":
|
|
|
|
resMap[key] = data["MemTotal"]
|
|
|
|
case "mem.usage.free":
|
|
|
|
resMap[key] = data["MemFree"]
|
|
|
|
case "mem.usage.available":
|
|
|
|
resMap[key] = data["MemAvailable"]
|
|
|
|
case "mem.usage.buffers":
|
|
|
|
resMap[key] = data["Buffers"]
|
|
|
|
case "mem.usage.cached":
|
|
|
|
resMap[key] = data["Cached"]
|
|
|
|
case "mem.swap.usage.total":
|
|
|
|
resMap[key] = data["SwapTotal"]
|
|
|
|
case "mem.swap.usage.free":
|
|
|
|
resMap[key] = data["SwapFree"]
|
|
|
|
case "mem.swap.usage.cached":
|
|
|
|
resMap[key] = data["SwapCached"]
|
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 07:09:17 +00:00
|
|
|
return resMap, nil
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|