forked from shieldproject/shield
/
health.go
163 lines (140 loc) · 4.03 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
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
package core
import (
"fmt"
"github.com/pborman/uuid"
"github.com/starkandwayne/shield/db"
)
type StorageHealth struct {
UUID uuid.UUID `json:"uuid"`
Name string `json:"name"`
Healthy bool `json:"healthy"`
}
type JobHealth struct {
UUID uuid.UUID `json:"uuid"`
Target string `json:"target"`
Job string `json:"job"`
Healthy bool `json:"healthy"`
}
type Health struct {
Health struct {
Core string `json:"core"`
Storage bool `json:"storage_ok"`
Jobs bool `json:"jobs_ok"`
} `json:"health"`
Storage []StorageHealth `json:"storage"`
Jobs []JobHealth `json:"jobs"`
Stats struct {
Jobs int `json:"jobs"`
Systems int `json:"systems"`
Archives int `json:"archives"`
Storage int64 `json:"storage"`
Daily int64 `json:"daily"`
} `json:"stats"`
}
func (core *Core) checkHealth() (Health, error) {
var health Health
health.Health.Storage = core.AreStoresHealthy()
stores, err := core.DB.GetAllStores(nil)
if err != nil {
return health, fmt.Errorf("failed to retrieve all stores: %s", err)
}
health.Storage = make([]StorageHealth, len(stores))
for i, store := range stores {
health.Storage[i].UUID = store.UUID
health.Storage[i].Name = store.Name
health.Storage[i].Healthy = store.Healthy
if !health.Storage[i].Healthy {
health.Health.Storage = false
}
}
health.Health.Jobs = true
jobs, err := core.DB.GetAllJobs(nil)
if err != nil {
return health, fmt.Errorf("failed to retrieve all jobs: %s", err)
}
health.Jobs = make([]JobHealth, len(jobs))
for i, job := range jobs {
health.Jobs[i].UUID = job.UUID
health.Jobs[i].Target = job.Target.Name
health.Jobs[i].Job = job.Name
health.Jobs[i].Healthy = job.Healthy()
if !health.Jobs[i].Healthy {
health.Health.Jobs = false
}
}
health.Stats.Jobs = len(jobs)
if health.Health.Core, err = core.vault.Status(); err != nil {
return health, fmt.Errorf("failed to retrieve vault status: %s", err)
}
if health.Stats.Systems, err = core.DB.CountTargets(nil); err != nil {
return health, fmt.Errorf("failed to count systems/targets: %s", err)
}
if health.Stats.Archives, err = core.DB.CountArchives(&db.ArchiveFilter{
WithStatus: []string{"valid"},
}); err != nil {
return health, fmt.Errorf("failed to retrieve count of valid archives: %s", err)
}
if health.Stats.Storage, err = core.DB.ArchiveStorageFootprint(&db.ArchiveFilter{
WithStatus: []string{"valid"},
}); err != nil {
return health, fmt.Errorf("failed to calcualte storage footprint: %s", err)
}
health.Stats.Daily = 0 // FIXME
return health, nil
}
func (core *Core) checkTenantHealth(tenantUUID string) (Health, error) {
var health Health
health.Health.Storage = true
stores, err := core.DB.GetAllStores(&db.StoreFilter{
ForTenant: tenantUUID,
})
if err != nil {
return health, err
}
health.Storage = make([]StorageHealth, len(stores))
for i, store := range stores {
health.Storage[i].UUID = store.UUID
health.Storage[i].Name = store.Name
health.Storage[i].Healthy = store.Healthy
if !health.Storage[i].Healthy {
health.Health.Storage = false
}
}
health.Health.Jobs = true
jobs, err := core.DB.GetAllJobs(&db.JobFilter{
ForTenant: tenantUUID,
})
if err != nil {
return health, err
}
health.Jobs = make([]JobHealth, len(jobs))
for i, job := range jobs {
health.Jobs[i].UUID = job.UUID
health.Jobs[i].Target = job.Target.Name
health.Jobs[i].Job = job.Name
health.Jobs[i].Healthy = job.Healthy()
if !health.Jobs[i].Healthy {
health.Health.Jobs = false
}
}
health.Stats.Jobs = len(jobs)
if health.Stats.Systems, err = core.DB.CountTargets(&db.TargetFilter{
ForTenant: tenantUUID,
}); err != nil {
return health, err
}
if health.Health.Core, err = core.vault.Status(); err != nil {
return health, err
}
tenant, err := core.DB.GetTenant(tenantUUID)
if err != nil {
return health, err
}
if tenant == nil {
return health, nil
}
health.Stats.Archives = tenant.ArchiveCount
health.Stats.Storage = tenant.StorageUsed
health.Stats.Daily = tenant.DailyIncrease
return health, nil
}