Skip to content

Commit 270a1ee

Browse files
committed
wrote meminfo parser
1 parent 0e59091 commit 270a1ee

File tree

7 files changed

+129
-10
lines changed

7 files changed

+129
-10
lines changed

inspector/df.go renamed to inspector/disk.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"strings"
77
)
88

9+
// DFMetrics : Metrics used by DF
910
type DFMetrics struct {
10-
size float64
11-
used float64
12-
available float64
13-
percentFull int
11+
Size float64
12+
Used float64
13+
Available float64
14+
PercentFull int
1415
}
1516

16-
// DF : Parsing the `df` output for memory monitoring
17+
// DF : Parsing the `df` output for disk monitoring
1718
type DF struct {
1819
fields
1920
// The values read from the command output string are defaultly in KB
@@ -63,10 +64,10 @@ func (i *DF) Parse(output string) {
6364

6465
func (i DF) createMetric(columns []string, percent int) DFMetrics {
6566
return DFMetrics{
66-
size: NewByteSize(columns[1], i.RawByteSize).format(i.DisplayByteSize),
67-
used: NewByteSize(columns[2], i.RawByteSize).format(i.DisplayByteSize),
68-
available: NewByteSize(columns[3], i.RawByteSize).format(i.DisplayByteSize),
69-
percentFull: percent,
67+
Size: NewByteSize(columns[1], i.RawByteSize).format(i.DisplayByteSize),
68+
Used: NewByteSize(columns[2], i.RawByteSize).format(i.DisplayByteSize),
69+
Available: NewByteSize(columns[3], i.RawByteSize).format(i.DisplayByteSize),
70+
PercentFull: percent,
7071
}
7172
}
7273

File renamed without changes.

inspector/inspector.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const (
1515
)
1616

1717
var inspectorMap = map[string]Inspector{
18-
`disk`: NewDF(),
18+
`disk`: NewDF(),
19+
`meminfo`: NewMemInfo(),
1920
}
2021

2122
type fields struct {

inspector/meminfo.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package inspector
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"strings"
6+
)
7+
8+
// Metrics used by MemInfo
9+
type MemInfoMetrics struct {
10+
MemTotal float64
11+
MemFree float64
12+
Cached float64
13+
SwapTotal float64
14+
SwapFree float64
15+
}
16+
17+
// MemInfo : Parsing the `/proc/meminfo` file output for memory monitoring
18+
type MemInfo struct {
19+
fields
20+
// The values read from the command output string are defaultly in KB
21+
RawByteSize string
22+
// We want do display disk values in GB
23+
DisplayByteSize string
24+
// Values of metrics being read
25+
Values MemInfoMetrics
26+
}
27+
28+
// Parse : run custom parsing on output of the command
29+
func (i *MemInfo) Parse(output string) {
30+
log.Debug("Parsing ouput string in MemInfo inspector")
31+
memTotal := i.getMatching("MemTotal", output)
32+
memFree := i.getMatching("MemFree", output)
33+
cached := i.getMatching("Cached", output)
34+
swapTotal := i.getMatching("SwapTotal", output)
35+
swapFree := i.getMatching("SwapFree", output)
36+
i.Values = i.createMetric([]string{memTotal, memFree, cached, swapTotal, swapFree})
37+
}
38+
39+
func (i MemInfo) getMatching(metric string, rows string) string {
40+
lines := strings.Split(rows, "\n")
41+
for _, line := range lines {
42+
if strings.HasPrefix(line, metric) {
43+
columns := strings.Fields(line)
44+
return columns[1]
45+
}
46+
}
47+
return `0`
48+
}
49+
50+
func (i MemInfo) createMetric(columns []string) MemInfoMetrics {
51+
return MemInfoMetrics{
52+
MemTotal: NewByteSize(columns[0], i.RawByteSize).format(i.DisplayByteSize),
53+
MemFree: NewByteSize(columns[1], i.RawByteSize).format(i.DisplayByteSize),
54+
Cached: NewByteSize(columns[2], i.RawByteSize).format(i.DisplayByteSize),
55+
SwapTotal: NewByteSize(columns[3], i.RawByteSize).format(i.DisplayByteSize),
56+
SwapFree: NewByteSize(columns[4], i.RawByteSize).format(i.DisplayByteSize),
57+
}
58+
}
59+
60+
// NewMemInfo : Initialize a new MemInfo instance
61+
func NewMemInfo() *MemInfo {
62+
return &MemInfo{
63+
fields: fields{
64+
Type: File,
65+
FilePath: `/proc/meminfo`,
66+
},
67+
RawByteSize: `KB`,
68+
DisplayByteSize: `MB`,
69+
}
70+
71+
}

inspector/meminfo_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build !windows
2+
3+
package inspector
4+
5+
import (
6+
"testing"
7+
)
8+
9+
func TestMemInfo(t *testing.T) {
10+
d := NewMemInfo()
11+
if d.Type != File || d.FilePath != `/proc/meminfo` {
12+
t.Error("Initialized meminfo wrongly")
13+
}
14+
}

integration/integration_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,17 @@ func TestDFonSSH(t *testing.T) {
1818
i.Parse(output)
1919
fmt.Printf(`%#v`, i.Values)
2020
}
21+
22+
func TestMemInfoonSSH(t *testing.T) {
23+
d := driver.NewSSHForTest()
24+
i := inspector.NewMemInfo()
25+
output, err := d.ReadFile(i.String())
26+
if err != nil {
27+
t.Error(err)
28+
}
29+
i.Parse(output)
30+
if i.Values.MemTotal == 0 {
31+
t.Error("showing percent used as 0")
32+
}
33+
fmt.Printf(`%#v`, i.Values)
34+
}

integration/integration_unix_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,23 @@ func TestDFonLocal(t *testing.T) {
1919
t.Error(err)
2020
}
2121
i.Parse(output)
22+
if i.Values[0].Used == 0 {
23+
t.Error("showing percent used as 0")
24+
}
25+
fmt.Printf(`%#v`, i.Values)
26+
}
27+
28+
func TestMemInfoonLocal(t *testing.T) {
29+
d := driver.Local{}
30+
// can either use NewDF() or get the interface and perform type assertion
31+
i := (inspector.GetInspector(`meminfo`)).(*inspector.MemInfo)
32+
output, err := d.ReadFile(i.String())
33+
if err != nil {
34+
t.Error(err)
35+
}
36+
i.Parse(output)
37+
if i.Values.MemTotal == 0 {
38+
t.Error("showing percent used as 0")
39+
}
2240
fmt.Printf(`%#v`, i.Values)
2341
}

0 commit comments

Comments
 (0)