forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_store.go
71 lines (58 loc) · 1.86 KB
/
health_store.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
package etcd
import (
"context"
"crypto/tls"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/sensu/sensu-go/types"
)
// GetClusterHealth retrieves the cluster health
func (s *Store) GetClusterHealth(ctx context.Context, cluster clientv3.Cluster, etcdClientTLSConfig *tls.Config) *types.HealthResponse {
healthResponse := &types.HealthResponse{}
// Do a get op against every cluster member. Collect the memberIDs and
// op errors into a response map, and return this map as etcd health
// information.
mList, err := cluster.MemberList(ctx)
if err != nil {
logger.WithError(err).Warning("could not get the cluster member list")
return healthResponse
}
healthResponse.Header = mList.Header
for _, member := range mList.Members {
health := &types.ClusterHealth{
MemberID: member.ID,
Name: member.Name,
}
cli, cliErr := clientv3.New(clientv3.Config{
Endpoints: member.ClientURLs,
DialTimeout: 5 * time.Second,
TLS: etcdClientTLSConfig,
})
if cliErr != nil {
logger.WithField("member", member.ID).WithError(cliErr).Error("unhealthy cluster member")
health.Err = cliErr.Error()
health.Healthy = false
healthResponse.ClusterHealth = append(healthResponse.ClusterHealth, health)
continue
}
defer func() {
_ = cli.Close()
}()
_, getErr := cli.Get(context.Background(), "health")
if getErr == nil || getErr == rpctypes.ErrPermissionDenied {
health.Err = ""
health.Healthy = true
} else {
health.Err = getErr.Error()
health.Healthy = false
}
healthResponse.ClusterHealth = append(healthResponse.ClusterHealth, health)
}
alarmResponse, err := s.client.Maintenance.AlarmList(ctx)
if err != nil {
logger.WithError(err).Error("failed to fetch etcd alarm list")
}
healthResponse.Alarms = alarmResponse.Alarms
return healthResponse
}