forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_table.go
213 lines (165 loc) · 5.72 KB
/
instance_table.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
package cmd
import (
"strings"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"
)
type InstanceTableValues struct {
Name boshtbl.Value
Process boshtbl.Value
ProcessState boshtbl.Value
State boshtbl.Value
AZ boshtbl.Value
VMType boshtbl.Value
IPs boshtbl.Value
// Details
VMCID boshtbl.Value
DiskCIDs boshtbl.Value
AgentID boshtbl.Value
Index boshtbl.Value
Resurrection boshtbl.Value
Bootstrap boshtbl.Value
Ignore boshtbl.Value
// DNS
DNS boshtbl.Value
// Vitals
Uptime boshtbl.Value // only for Process
Load boshtbl.Value
CPUTotal boshtbl.Value // only for Process
CPUUser boshtbl.Value
CPUSys boshtbl.Value
CPUWait boshtbl.Value
Memory boshtbl.Value
Swap boshtbl.Value
SystemDisk boshtbl.Value
EphemeralDisk boshtbl.Value
PersistentDisk boshtbl.Value
}
var InstanceTableHeader = InstanceTableValues{
Name: boshtbl.NewValueString("Instance"),
Process: boshtbl.NewValueString("Process"),
ProcessState: boshtbl.NewValueString("Process State"),
AZ: boshtbl.NewValueString("AZ"),
VMType: boshtbl.NewValueString("VM Type"),
IPs: boshtbl.NewValueString("IPs"),
// Details
State: boshtbl.NewValueString("State"),
VMCID: boshtbl.NewValueString("VM CID"),
DiskCIDs: boshtbl.NewValueString("Disk CIDs"),
AgentID: boshtbl.NewValueString("Agent ID"),
Index: boshtbl.NewValueString("Index"),
Resurrection: boshtbl.NewValueString("Resurrection\nPaused"),
Bootstrap: boshtbl.NewValueString("Bootstrap"),
Ignore: boshtbl.NewValueString("Ignore"),
// DNS
DNS: boshtbl.NewValueString("DNS A Records"),
// Vitals
Uptime: boshtbl.NewValueString("Uptime"), // only for Process
Load: boshtbl.NewValueString("Load\n(1m, 5m, 15m)"),
CPUTotal: boshtbl.NewValueString("CPU\nTotal"), // only for Process
CPUUser: boshtbl.NewValueString("CPU\nUser"),
CPUSys: boshtbl.NewValueString("CPU\nSys"),
CPUWait: boshtbl.NewValueString("CPU\nWait"),
Memory: boshtbl.NewValueString("Memory\nUsage"),
Swap: boshtbl.NewValueString("Swap\nUsage"),
SystemDisk: boshtbl.NewValueString("System\nDisk Usage"),
EphemeralDisk: boshtbl.NewValueString("Ephemeral\nDisk Usage"),
PersistentDisk: boshtbl.NewValueString("Persistent\nDisk Usage"),
}
type InstanceTable struct {
Processes, VMDetails, Details, DNS, Vitals bool
}
func (t InstanceTable) Header() InstanceTableValues {
return InstanceTableHeader
}
func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues {
var vmInfoIndex boshtbl.ValueInt
if i.Index != nil {
vmInfoIndex = boshtbl.NewValueInt(*i.Index)
}
vals := InstanceTableValues{
Name: t.buildName(i),
Process: boshtbl.ValueString{},
ProcessState: boshtbl.ValueFmt{
V: boshtbl.NewValueString(i.ProcessState),
Error: !i.IsRunning(),
},
AZ: boshtbl.NewValueString(i.AZ),
VMType: boshtbl.NewValueString(i.VMType),
IPs: boshtbl.NewValueStrings(i.IPs),
// Details
State: boshtbl.NewValueString(i.State),
VMCID: boshtbl.NewValueString(i.VMID),
DiskCIDs: boshtbl.NewValueStrings(i.DiskIDs),
AgentID: boshtbl.NewValueString(i.AgentID),
Index: vmInfoIndex,
Resurrection: boshtbl.NewValueBool(i.ResurrectionPaused),
Bootstrap: boshtbl.NewValueBool(i.Bootstrap),
Ignore: boshtbl.NewValueBool(i.Ignore),
// DNS
DNS: boshtbl.NewValueStrings(i.DNS),
// Vitals
Uptime: ValueUptime{i.Vitals.Uptime.Seconds},
Load: boshtbl.NewValueString(strings.Join(i.Vitals.Load, ", ")),
CPUTotal: ValueCPUTotal{i.Vitals.CPU.Total},
CPUUser: NewValueStringPercent(i.Vitals.CPU.User),
CPUSys: NewValueStringPercent(i.Vitals.CPU.Sys),
CPUWait: NewValueStringPercent(i.Vitals.CPU.Wait),
Memory: ValueMemSize{i.Vitals.Mem},
Swap: ValueMemSize{i.Vitals.Swap},
SystemDisk: ValueDiskSize{i.Vitals.SystemDisk()},
EphemeralDisk: ValueDiskSize{i.Vitals.EphemeralDisk()},
PersistentDisk: ValueDiskSize{i.Vitals.PersistentDisk()},
}
if len(i.VMType) == 0 {
vals.VMType = boshtbl.NewValueString(i.ResourcePool)
}
return vals
}
func (t InstanceTable) buildName(i boshdir.VMInfo) boshtbl.ValueString {
name := "?"
if len(i.JobName) > 0 {
name = i.JobName
}
if len(i.ID) > 0 {
name += "/" + i.ID
}
return boshtbl.NewValueString(name)
}
func (t InstanceTable) ForProcess(p boshdir.VMInfoProcess) InstanceTableValues {
return InstanceTableValues{
Name: boshtbl.ValueString{},
Process: boshtbl.NewValueString(p.Name),
ProcessState: boshtbl.ValueFmt{
V: boshtbl.NewValueString(p.State),
Error: !p.IsRunning(),
},
// Vitals
Uptime: ValueUptime{p.Uptime.Seconds},
Memory: ValueMemIntSize{p.Mem},
CPUTotal: ValueCPUTotal{p.CPU.Total},
}
}
// AsValues is public instead of being private to aid ease of accessing vals in tests
func (t InstanceTable) AsValues(v InstanceTableValues) []boshtbl.Value {
result := []boshtbl.Value{v.Name}
if t.Processes {
result = append(result, v.Process)
}
result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs}...)
if t.Details {
result = append(result, []boshtbl.Value{v.State, v.VMCID, v.VMType, v.DiskCIDs, v.AgentID, v.Index, v.Resurrection, v.Bootstrap, v.Ignore}...)
} else if t.VMDetails {
result = append(result, []boshtbl.Value{v.VMCID, v.VMType}...)
}
if t.DNS {
result = append(result, v.DNS)
}
if t.Vitals {
result = append(result, []boshtbl.Value{v.Uptime, v.Load}...)
result = append(result, []boshtbl.Value{v.CPUTotal, v.CPUUser, v.CPUSys, v.CPUWait}...)
result = append(result, []boshtbl.Value{v.Memory, v.Swap}...)
result = append(result, []boshtbl.Value{v.SystemDisk, v.EphemeralDisk, v.PersistentDisk}...)
}
return result
}