forked from ehazlett/stellar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
87 lines (74 loc) · 1.58 KB
/
service.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package health
import (
"context"
"runtime"
"time"
"github.com/cloudfoundry/gosigar"
"github.com/ehazlett/element"
api "github.com/ehazlett/stellar/api/services/health/v1"
ptypes "github.com/gogo/protobuf/types"
"google.golang.org/grpc"
)
const (
serviceID = "stellar.services.health.v1"
)
type service struct {
agent *element.Agent
started time.Time
}
func New(a *element.Agent) (*service, error) {
return &service{
agent: a,
started: time.Now(),
}, nil
}
func (s *service) Register(server *grpc.Server) error {
api.RegisterHealthServer(server, s)
return nil
}
func (s *service) ID() string {
return serviceID
}
func (s *service) Start() error {
return nil
}
func (s *service) Started() time.Time {
return s.started
}
func (s *service) Health(ctx context.Context, _ *ptypes.Empty) (*api.HealthResponse, error) {
osInfo, err := OSInfo()
if err != nil {
return nil, err
}
memory := sigar.Mem{}
if err := memory.Get(); err != nil {
return nil, err
}
ts, err := ptypes.TimestampProto(s.started)
if err != nil {
return nil, err
}
p, err := s.agent.Peers()
if err != nil {
return nil, err
}
peers := []*api.Peer{}
for _, peer := range p {
peers = append(peers, &api.Peer{
Name: peer.Name,
Addr: peer.Addr,
})
}
return &api.HealthResponse{
Health: &api.NodeHealth{
OSName: osInfo.OSName,
OSVersion: osInfo.OSVersion,
StartedAt: ts,
Cpus: int64(runtime.NumCPU()),
MemoryTotal: int64(memory.Total),
MemoryFree: int64(memory.Free),
MemoryUsed: int64(memory.ActualUsed),
Peers: peers,
},
}, nil
}