forked from percybolmer/grpcstreams
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
67 lines (59 loc) · 1.58 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"log"
"time"
// Dont forget this import :)
"github.com/mackerelio/go-osstat/cpu"
"github.com/mackerelio/go-osstat/memory"
hardwaremonitoring "github.com/percybolmer/grpcstreams/proto"
)
// Server is our struct that will handle the Hardware monitoring Logic
// It will fulfill the gRPC interface generated
type Server struct {
hardwaremonitoring.UnimplementedHardwareMonitorServer
}
// Monitor is used to start a stream of HardwareStats
func (s *Server) Monitor(req *hardwaremonitoring.EmptyRequest, stream hardwaremonitoring.HardwareMonitor_MonitorServer) error {
// Start a ticker that executes each 2 seconds
timer := time.NewTicker(1 * time.Second)
for {
select {
// Exit on stream context done
case <-stream.Context().Done():
return nil
case <-timer.C:
// Grab stats and output
hwStats, err := s.GetStats()
if err != nil {
log.Println(err.Error())
} else {
}
// Send the Hardware stats on the stream
err = stream.Send(hwStats)
if err != nil {
log.Println(err.Error())
}
}
}
}
// GetStats will extract system stats and output a Hardware Object, or an error
// if extraction fails
func (s *Server) GetStats() (*hardwaremonitoring.HardwareStats, error) {
// Extarcyt Memory statas
mem, err := memory.Get()
if err != nil {
return nil, err
}
// Extract CPU stats
cpu, err := cpu.Get()
if err != nil {
return nil, err
}
// Create our response object
hwStats := &hardwaremonitoring.HardwareStats{
Cpu: int32(cpu.Total),
MemoryFree: int32(mem.Free),
MemoryUsed: int32(mem.Used),
}
return hwStats, nil
}