/
status.go
259 lines (242 loc) · 7.83 KB
/
status.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 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 web
import (
"encoding/json"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/control-center/serviced/dao"
"github.com/control-center/serviced/domain"
"github.com/control-center/serviced/health"
"github.com/control-center/serviced/isvcs"
"github.com/control-center/serviced/metrics"
"github.com/control-center/serviced/utils"
"github.com/zenoss/glog"
"github.com/zenoss/go-json-rest"
)
var (
cachedvalue atomic.Value
cachetimeout = time.Duration(5) * time.Second
emptyvalue = []byte{}
isvcsRootHealth = map[string]health.HealthStatus{
"alive": health.HealthStatus{
Status: health.OK,
StartedAt: time.Now(),
},
}
)
// SetServiceStatsCacheTimeout sets the time in seconds for stats on
// running instances to be cached for the UI.
func SetServiceStatsCacheTimeout(seconds int) {
cachetimeout = time.Duration(seconds) * time.Second
}
// ConciseServiceStatus contains the information necessary to display the
// status of a service in the UI. It joins running instances with health checks
// with memory stats. It does not include fields unnecessary for status
// display, such as config files or monitoring profiles.
type ConciseServiceStatus struct {
DockerID string
HostID string
ID string
InSync bool
InstanceID int
Instances int
Name string
ParentServiceID string
PoolID string
ServiceID string
StartedAt time.Time
RAMMax int64
RAMLast int64
RAMAverage int64
RAMCommitment utils.EngNotation
HealthChecks map[string]health.HealthStatus
}
type memorykey struct {
ServiceID string
InstanceID int
}
// convertInstancesToMetric converts dao.RunningServices into the structure the
// metrics API wants
func convertInstancesToMetric(instances []dao.RunningService) []metrics.ServiceInstance {
svcInstances := make([]metrics.ServiceInstance, len(instances))
for i, inst := range instances {
svcInstances[i] = metrics.ServiceInstance{
inst.ServiceID,
inst.InstanceID,
}
}
return svcInstances
}
func getAllServiceStatuses(ctx *requestContext) (statuses []*ConciseServiceStatus, err error) {
// Get all running service instances
glog.V(2).Info("Retrieving statuses, memory and health checks for running services")
client, err := ctx.sc.getClient()
if err != nil {
glog.Errorf("Unable to acquire client: %v", err)
return nil, err
}
var instances []dao.RunningService
if err := client.GetRunningServices(&empty, &instances); err != nil {
return nil, err
}
if instances == nil {
glog.V(3).Info("Nil service list returned")
instances = []dao.RunningService{}
}
// Look up memory stats for the last hour
// Memory stats for isvcs don't work the same way, so we don't append
// those services to this list yet
memoryStats := make(map[memorykey]metrics.MemoryUsageStats)
if len(instances) > 0 {
response := []metrics.MemoryUsageStats{}
query := dao.MetricRequest{
StartTime: time.Now().Add(-time.Hour * 24),
Instances: convertInstancesToMetric(instances),
}
if err := client.GetInstanceMemoryStats(query, &response); err != nil {
glog.Errorf("Unable to look up instance memory stats (%s)", err)
}
for _, mus := range response {
instID, err := strconv.Atoi(mus.InstanceID)
if err != nil {
glog.Errorf("Could not convert instance id %s", mus.InstanceID)
continue
}
memoryStats[memorykey{ServiceID: mus.ServiceID, InstanceID: instID}] = mus
}
}
// Look up health check statuses
healthChecks := make(map[memorykey]map[string]health.HealthStatus)
if len(instances) > 0 {
results, err := ctx.getFacade().GetServicesHealth(ctx.getDatastoreContext())
if err != nil {
glog.Errorf("Unable to look up health check results (%s)", err)
}
for svcid, insts := range results {
for instid, checks := range insts {
healthChecks[memorykey{ServiceID: svcid, InstanceID: instid}] = checks
}
}
}
// Add isvcs to the mix
isvcInstances := getIRS()
for _, isvc := range isvcInstances {
var checks map[string]health.HealthStatus
instances = append(instances, isvc)
if isvc.ServiceID == "isvc-internalservices" {
checks = isvcsRootHealth
} else {
results, err := isvcs.Mgr.GetHealthStatus(strings.TrimPrefix(isvc.ServiceID, "isvc-"), isvcs.HEALTH_STATUS_INDEX_ALL)
if err != nil {
glog.Warningf("Error acquiring health status for %s (%s)", isvc.ServiceID, err)
continue
}
checks = convertDomainHealthToNewHealth(results.HealthStatuses)
}
healthChecks[memorykey{ServiceID: isvc.ServiceID, InstanceID: isvc.InstanceID}] = checks
}
// Create the concise service statuses
for _, instance := range instances {
stat := &ConciseServiceStatus{
DockerID: instance.DockerID,
HostID: instance.HostID,
ID: instance.ID,
InSync: instance.InSync,
InstanceID: instance.InstanceID,
Instances: instance.Instances,
Name: instance.Name,
ParentServiceID: instance.ParentServiceID,
PoolID: instance.PoolID,
ServiceID: instance.ServiceID,
StartedAt: instance.StartedAt,
RAMCommitment: instance.RAMCommitment,
}
key := memorykey{ServiceID: instance.ServiceID, InstanceID: instance.InstanceID}
if mem, ok := memoryStats[key]; ok {
stat.RAMMax = mem.Max
stat.RAMLast = mem.Last
stat.RAMAverage = mem.Average
}
if hc, ok := healthChecks[key]; ok {
stat.HealthChecks = hc
}
statuses = append(statuses, stat)
}
return
}
func restGetConciseServiceStatus(w *rest.ResponseWriter, r *rest.Request, ctx *requestContext) {
f := func() ([]byte, error) {
statuses, err := getAllServiceStatuses(ctx)
if err != nil {
glog.V(2).Infof("Error retrieving service statuses: (%s)", err)
return nil, err
}
bytes, err := json.Marshal(statuses)
if err != nil {
glog.V(2).Infof("Error serializing service statuses: (%s)", err)
return nil, err
}
return bytes, nil
}
w.Header().Set("content-type", "application/json")
bytes, err := getCached(f)
if err != nil {
glog.Errorf("Error retrieving service statuses: %s", err)
restServerError(w, err)
}
w.Write(bytes)
}
func getCached(f func() ([]byte, error)) ([]byte, error) {
var err error
val := cachedvalue.Load()
if val == nil || len(val.([]byte)) == 0 {
val, err = f()
if err != nil {
return nil, err
}
cachedvalue.Store(val)
go func() {
time.Sleep(cachetimeout)
cachedvalue.Store(emptyvalue)
}()
}
return val.([]byte), nil
}
// ConvertDomainHealthToNewHealth changes the domain health check status into
// a new-style health status for consumption by the UI or other interface.
// Sort of deprecated, in that it will be rendered useless once
// domain.HealthCheckStatus is no longer returned by isvcs.Mgr.
func convertDomainHealthToNewHealth(sources []domain.HealthCheckStatus) map[string]health.HealthStatus {
var status health.Status
result := make(map[string]health.HealthStatus)
for _, hcs := range sources {
switch hcs.Status {
case "passed":
status = health.OK
case "failed":
status = health.Failed
default:
status = health.Unknown
}
// Not going to bother populating Duration, since it doesn't have an
// analogue in domain.HealthCheckStatus.
result[hcs.Name] = health.HealthStatus{
Status: status,
StartedAt: time.Unix(hcs.Timestamp, 0),
}
}
return result
}