66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package stat
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type MemStat struct {
|
|
}
|
|
|
|
func (mem MemStat) Command() string {
|
|
return "cat /proc/meminfo"
|
|
}
|
|
|
|
func (mem MemStat) Read(r io.Reader, keys []string) (*map[string]string, error) {
|
|
var (
|
|
stats = map[string]string{}
|
|
scanner = bufio.NewScanner(r)
|
|
)
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
parts := strings.Fields(line)
|
|
key := parts[0][:len(parts[0])-1]
|
|
|
|
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:
|
|
|
|
}
|
|
}
|
|
|
|
return resMap, nil
|
|
}
|