-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus.go
218 lines (200 loc) · 4.27 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
package main
import (
"net/http"
"os"
"sort"
"strconv"
"time"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
type efesStatus struct {
devices []deviceStatus
serverTime time.Time
}
type deviceStatus struct {
Device
Hostname string
HostStatus string
UpdatedAt time.Time
}
func (d deviceStatus) Size() string {
if d.BytesTotal == nil {
return ""
}
return humanize.Comma(*d.BytesTotal / G)
}
func (d deviceStatus) Used() string {
if d.BytesUsed == nil {
return ""
}
return humanize.Comma(*d.BytesUsed / G)
}
func (d deviceStatus) Free() string {
if d.BytesFree == nil {
return ""
}
return humanize.Comma(*d.BytesFree / G)
}
func (d deviceStatus) Use() string {
if d.BytesUsed == nil || d.BytesTotal == nil {
return ""
}
use := (*d.BytesUsed * 100) / *d.BytesTotal
return colorPercent(use, strconv.FormatInt(use, 10))
}
func (d deviceStatus) IO() string {
if d.IoUtilization == nil {
return ""
}
return colorPercent(*d.IoUtilization, strconv.FormatInt(*d.IoUtilization, 10))
}
func colorPercent(value int64, s string) string {
switch {
case value >= 90:
return color.RedString(s)
case value >= 80:
return color.YellowString(s)
case value < 10:
return color.BlueString(s)
}
return s
}
func colorDuration(value time.Duration, s string) string {
if value > 2*time.Second {
return color.RedString(s)
} else if value > 1*time.Second {
return color.YellowString(s)
}
return s
}
func colorStatus(status string) string {
if status == "alive" {
return status
} else if status == "down" {
return color.RedString(status)
}
return color.YellowString(status)
}
func (s *efesStatus) Print() {
// Sum totals
var totalUsed, totalSize, totalFree int64 // in MB
for _, d := range s.devices {
if d.BytesUsed != nil {
totalUsed += *d.BytesUsed
}
if d.BytesTotal != nil {
totalSize += *d.BytesTotal
}
if d.BytesFree != nil && d.Status == "alive" {
totalFree += *d.BytesFree
}
}
var totalUse int64
if totalSize == 0 {
totalUse = 0
} else {
totalUse = 100 - (100*totalFree)/totalSize
}
// Convert to GB
totalUsed /= G
totalFree /= G
totalSize /= G
// Setup table
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
table.SetHeader([]string{
"Zone",
"Rack",
"Host",
"Status",
"Device",
"Status",
"Size (G)",
"Used (G)",
"Free (G)",
"Use %",
"IO %",
"Last update",
})
// Add data to the table
data := make([][]string, len(s.devices))
for i, d := range s.devices {
updatedAt := s.serverTime.Sub(d.UpdatedAt).Truncate(time.Second)
data[i] = []string{
d.ZoneName,
d.RackName,
d.Hostname,
colorStatus(d.HostStatus),
strconv.FormatInt(d.Devid, 10),
colorStatus(d.Status),
d.Size(),
d.Used(),
d.Free(),
d.Use(),
d.IO(),
colorDuration(updatedAt, updatedAt.String()),
}
}
table.AppendBulk(data) // Add Bulk Data
table.SetFooter([]string{
"", "", "", "", "",
"Total:",
humanize.Comma(totalSize),
humanize.Comma(totalUsed),
humanize.Comma(totalFree),
strconv.FormatInt(totalUse, 10),
"", "",
})
table.Render()
}
// nolint
func (c *Client) Status(sortBy string) (*efesStatus, error) {
ret := &efesStatus{
devices: make([]deviceStatus, 0),
}
var devices GetDevices
headers, err := c.request(http.MethodGet, "get-devices", nil, &devices)
if err != nil {
return nil, err
}
ret.serverTime, err = http.ParseTime(headers.Get("date"))
if err != nil {
ret.serverTime = time.Now()
}
ret.serverTime = ret.serverTime.UTC()
for _, d := range devices.Devices {
if d.Status == "dead" {
continue
}
ds := deviceStatus{
Device: d,
Hostname: d.HostName,
HostStatus: d.HostStatus,
UpdatedAt: time.Unix(d.UpdatedAt, 0),
}
ret.devices = append(ret.devices, ds)
}
switch sortBy {
case "zone":
sort.Sort(byZoneName{ret.devices})
case "rack":
sort.Sort(byRackName{ret.devices})
case "host":
sort.Sort(byHostname{ret.devices})
case "device":
sort.Sort(byDevID{ret.devices})
case "size":
sort.Sort(bySize{ret.devices})
case "used":
sort.Sort(byUsed{ret.devices})
case "free":
sort.Sort(byFree{ret.devices})
default:
c.log.Warningln("Sort key is not valid:", sortBy)
}
return ret, nil
}