/
usage.go
183 lines (141 loc) · 4.55 KB
/
usage.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
/*
Copyright (c) 2020-2023 VMware, Inc. All Rights Reserved.
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 cluster
import (
"context"
"flag"
"fmt"
"io"
"text/tabwriter"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/units"
"github.com/vmware/govmomi/vim25/mo"
)
type usage struct {
*flags.DatacenterFlag
shared bool
}
func init() {
cli.Register("cluster.usage", &usage{})
}
func (cmd *usage) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)
f.BoolVar(&cmd.shared, "S", false, "Exclude host local storage")
}
func (cmd *usage) Usage() string {
return "CLUSTER"
}
func (cmd *usage) Description() string {
return `Cluster resource usage summary.
Examples:
govc cluster.usage ClusterName
govc cluster.usage -S ClusterName # summarize shared storage only
govc cluster.usage -json ClusterName | jq -r .cpu.summary.usage`
}
func (cmd *usage) Run(ctx context.Context, f *flag.FlagSet) error {
finder, err := cmd.Finder()
if err != nil {
return err
}
obj, err := finder.ClusterComputeResource(ctx, f.Arg(0))
if err != nil {
return err
}
var res Usage
var cluster mo.ClusterComputeResource
var hosts []mo.HostSystem
var datastores []mo.Datastore
pc := property.DefaultCollector(obj.Client())
err = pc.RetrieveOne(ctx, obj.Reference(), []string{"datastore", "host"}, &cluster)
if err != nil {
return err
}
err = pc.Retrieve(ctx, cluster.Host, []string{"summary"}, &hosts)
if err != nil {
return err
}
for _, host := range hosts {
res.CPU.Capacity += int64(int32(host.Summary.Hardware.NumCpuCores) * host.Summary.Hardware.CpuMhz)
res.CPU.Used += int64(host.Summary.QuickStats.OverallCpuUsage)
res.Memory.Capacity += host.Summary.Hardware.MemorySize
res.Memory.Used += int64(host.Summary.QuickStats.OverallMemoryUsage) << 20
}
err = pc.Retrieve(ctx, cluster.Datastore, []string{"summary"}, &datastores)
if err != nil {
return err
}
for _, datastore := range datastores {
shared := datastore.Summary.MultipleHostAccess
if cmd.shared && shared != nil && *shared == false {
continue
}
res.Storage.Capacity += datastore.Summary.Capacity
res.Storage.Free += datastore.Summary.FreeSpace
}
res.CPU.Free = res.CPU.Capacity - res.CPU.Used
res.CPU.summarize(ghz)
res.Memory.Free = res.Memory.Capacity - res.Memory.Used
res.Memory.summarize(size)
res.Storage.Used = res.Storage.Capacity - res.Storage.Free
res.Storage.summarize(size)
return cmd.WriteResult(&res)
}
type ResourceUsageSummary struct {
Used string `json:"used"`
Free string `json:"free"`
Capacity string `json:"capacity"`
Usage string `json:"usage"`
}
type ResourceUsage struct {
Used int64 `json:"used"`
Free int64 `json:"free"`
Capacity int64 `json:"capacity"`
Usage float64 `json:"usage"`
Summary ResourceUsageSummary `json:"summary"`
}
func (r *ResourceUsage) summarize(f func(int64) string) {
r.Usage = 100 * float64(r.Used) / float64(r.Capacity)
r.Summary.Usage = fmt.Sprintf("%.1f", r.Usage)
r.Summary.Capacity = f(r.Capacity)
r.Summary.Used = f(r.Used)
r.Summary.Free = f(r.Free)
}
func (r *ResourceUsage) write(w io.Writer, label string) {
fmt.Fprintf(w, "%s usage:\t%s%%\n", label, r.Summary.Usage)
fmt.Fprintf(w, "%s capacity:\t%s\n", label, r.Summary.Capacity)
fmt.Fprintf(w, "%s used:\t%s\n", label, r.Summary.Used)
fmt.Fprintf(w, "%s free:\t%s\n", label, r.Summary.Free)
}
func ghz(val int64) string {
return fmt.Sprintf("%.1fGHz", float64(val)/1000)
}
func size(val int64) string {
return units.ByteSize(val).String()
}
type Usage struct {
Memory ResourceUsage `json:"memory"`
CPU ResourceUsage `json:"cpu"`
Storage ResourceUsage `json:"storage"`
}
func (r *Usage) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
r.CPU.write(tw, "CPU")
fmt.Fprintf(tw, "\t\n")
r.Memory.write(tw, "Memory")
fmt.Fprintf(tw, "\t\n")
r.Storage.write(tw, "Storage")
return tw.Flush()
}