12345678910111213141516171819202122232425262728293031323334353637 |
- package gupta
- import (
- "bytes"
- "fmt"
- "io"
- "strconv"
- "strings"
- )
- type Memory struct {
- Total uint64 `json:"total"`
- Free uint64 `json:"free"`
- }
- func (m Memory) MarshalJSON() ([]byte, error) {
- jsonData := []byte(
- fmt.Sprintf("{\"memory\": {\"total\": \"%dKB\", \"used\": \"%dKB\", \"free\": \"%dKB\"}}",
- m.Total, m.Used(), m.Free))
- return jsonData, nil
- }
- func (m *Memory) Used() uint64 {
- return m.Total - m.Free
- }
- func NewMemory(r io.ReaderAt) Memory {
- m := Memory{}
- buf := make([]byte, 80)
- r.ReadAt(buf, 0)
- idx := bytes.Index(buf, []byte("MemAvailable"))
- line := string(buf[:idx])
- elements := strings.Fields(line)
- m.Total, _ = strconv.ParseUint(elements[1], 10, 64)
- m.Free, _ = strconv.ParseUint(elements[4], 10, 64)
- return m
- }
|