File: info.go

package info (click to toggle)
golang-github-vmware-govmomi 0.24.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,848 kB
  • sloc: sh: 2,285; lisp: 1,560; ruby: 948; xml: 139; makefile: 54
file content (230 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download | duplicates (3)
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
/*
Copyright (c) 2017 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 metric

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"strings"
	"text/tabwriter"

	"github.com/vmware/govmomi/govc/cli"
	"github.com/vmware/govmomi/vim25/types"
)

type info struct {
	*PerformanceFlag
}

func init() {
	cli.Register("metric.info", &info{})
}

func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
	cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
	cmd.PerformanceFlag.Register(ctx, f)
}

func (cmd *info) Usage() string {
	return "PATH [NAME]..."
}

func (cmd *info) Description() string {
	return `Metric info for NAME.

If PATH is a value other than '-', provider summary and instance list are included
for the given object type.

If NAME is not specified, all available metrics for the given INTERVAL are listed.
An object PATH must be provided in this case.

Examples:
  govc metric.info vm/my-vm
  govc metric.info -i 300 vm/my-vm
  govc metric.info - cpu.usage.average
  govc metric.info /dc1/host/cluster cpu.usage.average`
}

func (cmd *info) Process(ctx context.Context) error {
	if err := cmd.PerformanceFlag.Process(ctx); err != nil {
		return err
	}
	return nil
}

type EntityDetail struct {
	Realtime   bool
	Historical bool
	Instance   []string
}

type MetricInfo struct {
	Counter          *types.PerfCounterInfo
	Enabled          []string
	PerDeviceEnabled []string
	Detail           *EntityDetail
}

type infoResult struct {
	Info []*MetricInfo
	cmd  *info
}

func (r *infoResult) Write(w io.Writer) error {
	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)

	for _, info := range r.Info {
		counter := info.Counter

		fmt.Fprintf(tw, "Name:\t%s\n", counter.Name())
		fmt.Fprintf(tw, "  Label:\t%s\n", counter.NameInfo.GetElementDescription().Label)
		fmt.Fprintf(tw, "  Summary:\t%s\n", counter.NameInfo.GetElementDescription().Summary)
		fmt.Fprintf(tw, "  Group:\t%s\n", counter.GroupInfo.GetElementDescription().Label)
		fmt.Fprintf(tw, "  Unit:\t%s\n", counter.UnitInfo.GetElementDescription().Label)
		fmt.Fprintf(tw, "  Rollup type:\t%s\n", counter.RollupType)
		fmt.Fprintf(tw, "  Stats type:\t%s\n", counter.StatsType)
		fmt.Fprintf(tw, "  Level:\t%d\n", counter.Level)
		fmt.Fprintf(tw, "    Intervals:\t%s\n", strings.Join(info.Enabled, ","))
		fmt.Fprintf(tw, "  Per-device level:\t%d\n", counter.PerDeviceLevel)
		fmt.Fprintf(tw, "    Intervals:\t%s\n", strings.Join(info.PerDeviceEnabled, ","))

		summary := info.Detail
		if summary == nil {
			continue
		}

		fmt.Fprintf(tw, "  Realtime:\t%t\n", summary.Realtime)
		fmt.Fprintf(tw, "  Historical:\t%t\n", summary.Historical)
		fmt.Fprintf(tw, "  Instances:\t%s\n", strings.Join(summary.Instance, ","))
	}

	return tw.Flush()
}

func (r *infoResult) MarshalJSON() ([]byte, error) {
	m := make(map[string]*MetricInfo)

	for _, info := range r.Info {
		m[info.Counter.Name()] = info
	}

	return json.Marshal(m)
}

func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
	if f.NArg() == 0 {
		return flag.ErrHelp
	}

	names := f.Args()[1:]

	m, err := cmd.Manager(ctx)
	if err != nil {
		return err
	}

	counters, err := m.CounterInfoByName(ctx)
	if err != nil {
		return err
	}

	intervals, err := m.HistoricalInterval(ctx)
	if err != nil {
		return err
	}
	enabled := intervals.Enabled()

	var summary *types.PerfProviderSummary
	var mids map[int32][]*types.PerfMetricId

	if f.Arg(0) == "-" {
		if len(names) == 0 {
			return flag.ErrHelp
		}
	} else {
		objs, err := cmd.ManagedObjects(ctx, f.Args()[:1])
		if err != nil {
			return err
		}

		summary, err = m.ProviderSummary(ctx, objs[0])
		if err != nil {
			return err
		}

		all, err := m.AvailableMetric(ctx, objs[0], cmd.Interval(summary.RefreshRate))
		if err != nil {
			return err
		}

		mids = all.ByKey()

		if len(names) == 0 {
			nc, _ := m.CounterInfoByKey(ctx)

			for i := range all {
				id := &all[i]
				if id.Instance != "" {
					continue
				}

				names = append(names, nc[id.CounterId].Name())
			}
		}
	}

	var metrics []*MetricInfo

	for _, name := range names {
		counter, ok := counters[name]
		if !ok {
			return cmd.ErrNotFound(name)
		}

		info := &MetricInfo{
			Counter:          counter,
			Enabled:          enabled[counter.Level],
			PerDeviceEnabled: enabled[counter.PerDeviceLevel],
		}

		metrics = append(metrics, info)

		if summary == nil {
			continue
		}

		var instances []string

		for _, id := range mids[counter.Key] {
			if id.Instance != "" {
				instances = append(instances, id.Instance)
			}
		}

		info.Detail = &EntityDetail{
			Realtime:   summary.CurrentSupported,
			Historical: summary.SummarySupported,
			Instance:   instances,
		}

	}

	return cmd.WriteResult(&infoResult{metrics, cmd})
}