Browse Source

Report memory in a JSON form

This opens the road for creating the complete report
Oz Tiram 5 years ago
parent
commit
83beba716f
5 changed files with 56 additions and 30 deletions
  1. 10 1
      cmd/main.go
  2. 4 4
      gupta.go
  3. 3 3
      gupta_test.go
  4. 11 11
      memory.go
  5. 28 11
      memory_test.go

+ 10 - 1
cmd/main.go

@@ -2,9 +2,11 @@ package main
 
 import (
 	"bytes"
+	"encoding/json"
 	"flag"
 	"fmt"
 	"github.com/oz123/gupta"
+	"log"
 	"os"
 	"time"
 )
@@ -44,7 +46,14 @@ func main() {
 		cpustat := gupta.NewCPUStat(line, lineNew)
 		fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
 		mem := gupta.NewMemory(m)
-		fmt.Printf("Memory free %d\n", mem.Total())
+		//fmt.Printf("Memory free %d\n", mem.Total())
+		report := gupta.GuptaReport{}
+		report.MemoryUsage = mem
+		writer := os.Stdout
+		enc := json.NewEncoder(writer)
+		if err := enc.Encode(&report); err != nil {
+			log.Println(err)
+		}
 		m.Seek(0, 0)
 	}
 }

+ 4 - 4
gupta.go

@@ -20,13 +20,13 @@ func Run(load, cpu, memory bool, partition, networkInterface string) (cpuLoad, c
 }
 
 type GuptaReport struct {
-	cpuLoad     float64
-	cpuUsage    float64
-	memoryUsage float64
+	LoadAvarage float64
+	CPUUsage    float64
+	MemoryUsage Memory `json:"memory,omitempty"`
 }
 
 func (g *GuptaReport) GetCPULoad() string {
-	return fmt.Sprintf("%.2f", g.cpuLoad)
+	return fmt.Sprintf("%.2f", g.LoadAvarage)
 }
 
 // collect all CPU stat from /proc/cpustat

+ 3 - 3
gupta_test.go

@@ -44,9 +44,9 @@ func TestCPUStat(t *testing.T) {
 
 		// test we can get some report struct from gupta
 		report := GuptaReport{
-			cpuLoad:     0.2,
-			cpuUsage:    0.2,
-			memoryUsage: 70.0,
+			LoadAvarage: 0.2,
+			CPUUsage:    0.2,
+			MemoryUsage: Memory{},
 		}
 
 		got := report.GetCPULoad()

+ 11 - 11
memory.go

@@ -2,26 +2,26 @@ package gupta
 
 import (
 	"bytes"
+	"fmt"
 	"io"
 	"strconv"
 	"strings"
 )
 
 type Memory struct {
-	total uint64
-	free  uint64
+	Total uint64 `json:"total"`
+	Free  uint64 `json:"free"`
 }
 
-func (m *Memory) Free() uint64 {
-	return m.free
-}
-
-func (m *Memory) Total() uint64 {
-	return m.total
+func (m Memory) MarshalJSON() ([]byte, error) {
+	jsonData := []byte(
+		fmt.Sprintf("{\"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
+	return m.Total - m.Free
 }
 
 func NewMemory(r io.ReaderAt) Memory {
@@ -31,7 +31,7 @@ func NewMemory(r io.ReaderAt) Memory {
 	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)
+	m.Total, _ = strconv.ParseUint(elements[1], 10, 64)
+	m.Free, _ = strconv.ParseUint(elements[4], 10, 64)
 	return m
 }

+ 28 - 11
memory_test.go

@@ -1,6 +1,8 @@
 package gupta
 
 import (
+	"bytes"
+	"encoding/json"
 	"fmt"
 	"strings"
 	"testing"
@@ -8,8 +10,8 @@ import (
 
 func TestFreeMem(t *testing.T) {
 	m := Memory{}
-	m.free = uint64(1200)
-	got := m.Free()
+	m.Free = uint64(1200)
+	got := m.Free
 	want := uint64(1200)
 	name := "free"
 	if got != want {
@@ -18,8 +20,8 @@ func TestFreeMem(t *testing.T) {
 }
 
 func TestTotalMem(t *testing.T) {
-	m := Memory{total: uint64(2400)}
-	got := m.Total()
+	m := Memory{Total: uint64(2400)}
+	got := m.Total
 	want := uint64(2400)
 	name := "total"
 	if got != want {
@@ -28,7 +30,7 @@ func TestTotalMem(t *testing.T) {
 }
 
 func TestUsedMem(t *testing.T) {
-	m := Memory{total: uint64(2400), free: uint64(1200)}
+	m := Memory{Total: uint64(2400), Free: uint64(1200)}
 	got := m.Used()
 	fmt.Println(got)
 	want := uint64(1200)
@@ -42,10 +44,25 @@ func TestNewMemory(t *testing.T) {
 	//r, _ := os.open("/proc/meminfo")
 	r := strings.NewReader("MemTotal:       16080192 kB\nMemFree:         3617004 kB\nMemAvailable:   11258504 kB\nBuffers:         1814108 kB\n")
 	mem := NewMemory(r)
-	got := mem.total
-	want := uint64(16080192)
-	name := "memory total"
-	if got != want {
-		t.Errorf("got %s %d want %d", name, got, want)
-	}
+	t.Run("Test that we can get total memory", func(t *testing.T) {
+		got := mem.Total
+		want := uint64(16080192)
+		name := "memory total"
+		if got != want {
+			t.Errorf("got %s %d want %d", name, got, want)
+		}
+	})
+
+	t.Run("Test that we can properly report Memory usage", func(t *testing.T) {
+		var buff bytes.Buffer
+		enc := json.NewEncoder(&buff)
+		enc.Encode(&mem)
+		report := buff.String()
+		fmt.Println(report)
+		reportFields := strings.Split(report, ":")
+		fmt.Println(reportFields[1])
+		if reportFields[1] != "\"16080192KB\",\"used\"" {
+			t.Errorf("Encoding report failed")
+		}
+	})
 }