/
health.go
105 lines (95 loc) · 3.44 KB
/
health.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package facade
import (
"time"
"github.com/control-center/serviced/datastore"
"github.com/control-center/serviced/domain/service"
"github.com/control-center/serviced/health"
zkservice "github.com/control-center/serviced/zzk/service"
"github.com/zenoss/glog"
)
// ReportHealthStatus writes the status of a health check to the cache.
func (f *Facade) ReportHealthStatus(key health.HealthStatusKey, value health.HealthStatus, expires time.Duration) {
f.hcache.Set(key, value, expires)
}
// ReportInstanceDead removes all health checks of a particular instance from
// the cache.
func (f *Facade) ReportInstanceDead(serviceID string, instanceID int) {
f.hcache.DeleteInstance(serviceID, instanceID)
}
// GetServicesHealth returns the status of all services health instances.
func (f *Facade) GetServicesHealth(ctx datastore.Context) (map[string]map[int]map[string]health.HealthStatus, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetServicesHealth"))
store := f.serviceStore
shs, err := store.GetAllServiceHealth(ctx)
if err != nil {
glog.Errorf("Could not look up services: %s", err)
return nil, err
}
stats := make(map[string]map[int]map[string]health.HealthStatus)
for _, svc := range shs {
if stats[svc.ID], err = f.getServiceHealth(ctx, svc); err != nil {
return nil, err
}
}
return stats, nil
}
// GetServiceHealth returns the status of all health instances.
func (f *Facade) GetServiceHealth(ctx datastore.Context, serviceID string) (map[int]map[string]health.HealthStatus, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetServiceHealth"))
store := f.serviceStore
sh, err := store.GetServiceHealth(ctx, serviceID)
if err != nil {
glog.Errorf("Could not look up service %s: %s", serviceID, err)
return nil, err
}
return f.getServiceHealth(ctx, *sh)
}
func (f *Facade) getServiceHealth(ctx datastore.Context, sh service.ServiceHealth) (map[int]map[string]health.HealthStatus, error) {
states, err := f.zzk.GetServiceStates(ctx, sh.PoolID, sh.ID)
if err != nil {
glog.Errorf("Could not get service states for service %s (%s): %s", sh.Name, sh.ID, err)
return nil, err
}
stateMap := make(map[int]zkservice.State)
for _, state := range states {
stateMap[state.InstanceID] = state
}
status := make(map[int]map[string]health.HealthStatus)
for i := 0; i < sh.Instances; i++ {
stats := make(map[string]health.HealthStatus)
for name, hc := range sh.HealthChecks {
key := health.HealthStatusKey{
ServiceID: sh.ID,
InstanceID: i,
HealthCheckName: name,
}
result, ok := f.hcache.Get(key)
var uptime time.Duration
s := stateMap[i]
if s.Started.After(s.Terminated) {
uptime = time.Since(s.Started)
}
if ok {
stats[name] = result
} else if uptime == 0 {
stats[name] = hc.NotRunning()
} else {
stats[name] = hc.Unknown()
}
}
status[i] = stats
}
return status, nil
}