Skip to content

Commit 4810ebf

Browse files
committed
added uptime inspector
1 parent 3bdc5ab commit 4810ebf

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

inspector/loadavg.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type LoadAvgMetrics struct {
1414
Load15M float64
1515
}
1616

17-
// LoadAvg : Parsing the /proc/loadavg output for disk monitoring
17+
// LoadAvg : Parsing the /proc/loadavg output for load average monitoring
1818
type LoadAvg struct {
1919
fields
2020
Values LoadAvgMetrics

inspector/uptime.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package inspector
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
// UptimeMetrics : Metrics used by Uptime
11+
type UptimeMetrics struct {
12+
Up float64
13+
Idle float64
14+
}
15+
16+
// Uptime : Parsing the /proc/uptime output for uptime monitoring
17+
type Uptime struct {
18+
fields
19+
Values UptimeMetrics
20+
}
21+
22+
// Parse : run custom parsing on output of the command
23+
func (i *Uptime) Parse(output string) {
24+
var err error
25+
log.Debug("Parsing ouput string in Uptime inspector")
26+
columns := strings.Fields(output)
27+
Up, err := strconv.ParseFloat(columns[0], 64)
28+
Idle, err := strconv.ParseFloat(columns[1], 64)
29+
if err != nil {
30+
log.Fatalf(`Error Parsing Uptime: %s `, err)
31+
}
32+
33+
i.Values = UptimeMetrics{
34+
Up,
35+
Idle,
36+
}
37+
}
38+
39+
// NewUptime : Initialize a new Uptime instance
40+
func NewUptime() *Uptime {
41+
return &Uptime{
42+
fields: fields{
43+
Type: File,
44+
FilePath: `/proc/uptime`,
45+
},
46+
}
47+
48+
}

integration/integration_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,16 @@ func TestLoadAvgonSSH(t *testing.T) {
8888
t.Errorf("%f", i.Values.Load1M)
8989
}
9090
}
91+
92+
func TestUptimeonSSH(t *testing.T) {
93+
d := driver.NewSSHForTest()
94+
i := inspector.NewUptime()
95+
output, err := d.ReadFile(i.String())
96+
if err != nil {
97+
t.Error(err)
98+
}
99+
i.Parse(output)
100+
if i.Values.Up == 0 {
101+
t.Errorf("%f", i.Values.Up)
102+
}
103+
}

integration/integration_unix_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestCustomonLocal(t *testing.T) {
105105
}
106106

107107
func TestLoadAvgonLocal(t *testing.T) {
108-
d := driver.NewSSHForTest()
108+
d := driver.Local{}
109109
i := inspector.NewLoadAvg()
110110
output, err := d.ReadFile(i.String())
111111
if err != nil {
@@ -117,3 +117,17 @@ func TestLoadAvgonLocal(t *testing.T) {
117117
}
118118
fmt.Printf("%#v", i.Values)
119119
}
120+
121+
func TestUptimeonLocal(t *testing.T) {
122+
d := driver.Local{}
123+
i := inspector.NewUptime()
124+
output, err := d.ReadFile(i.String())
125+
if err != nil {
126+
t.Error(err)
127+
}
128+
i.Parse(output)
129+
if i.Values.Up == 0 {
130+
t.Errorf("%f", i.Values.Up)
131+
}
132+
fmt.Printf("%#v", i.Values)
133+
}

0 commit comments

Comments
 (0)