1
0
mirror of https://github.com/Oxalide/vsphere-influxdb-go.git synced 2023-10-10 11:36:51 +00:00

add vendoring with go dep

This commit is contained in:
Adrian Todorov
2017-10-25 20:52:40 +00:00
parent 704f4d20d1
commit a59409f16b
1627 changed files with 489673 additions and 0 deletions

101
vendor/github.com/vmware/govmomi/govc/metric/change.go generated vendored Normal file
View File

@@ -0,0 +1,101 @@
/*
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"
"flag"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
type change struct {
*PerformanceFlag
level int
device int
}
func init() {
cli.Register("metric.change", &change{})
}
func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
f.IntVar(&cmd.level, "level", 0, "Level for the aggregate counter")
f.IntVar(&cmd.device, "device-level", 0, "Level for the per device counter")
}
func (cmd *change) Usage() string {
return "NAME..."
}
func (cmd *change) Description() string {
return `Change counter NAME levels.
Examples:
govc metric.change -level 1 net.bytesRx.average net.bytesTx.average`
}
func (cmd *change) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() == 0 || (cmd.level == 0 && cmd.device == 0) {
return flag.ErrHelp
}
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
counters, err := m.CounterInfoByName(ctx)
if err != nil {
return err
}
var mapping []types.PerformanceManagerCounterLevelMapping
for _, name := range f.Args() {
counter, ok := counters[name]
if !ok {
return cmd.ErrNotFound(name)
}
mapping = append(mapping, types.PerformanceManagerCounterLevelMapping{
CounterId: counter.Key,
AggregateLevel: int32(cmd.level),
PerDeviceLevel: int32(cmd.device),
})
}
_, err = methods.UpdateCounterLevelMapping(ctx, m.Client(), &types.UpdateCounterLevelMapping{
This: m.Reference(),
CounterLevelMap: mapping,
})
return err
}

230
vendor/github.com/vmware/govmomi/govc/metric/info.go generated vendored Normal file
View File

@@ -0,0 +1,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 {
cmd *info
Info []*MetricInfo
}
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{cmd, metrics})
}

View File

@@ -0,0 +1,111 @@
/*
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 interval
import (
"context"
"flag"
"fmt"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/metric"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
type change struct {
*metric.PerformanceFlag
enabled *bool
level int
}
func init() {
cli.Register("metric.interval.change", &change{})
}
func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = metric.NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
f.Var(flags.NewOptionalBool(&cmd.enabled), "enabled", "Enable or disable")
f.IntVar(&cmd.level, "level", 0, "Level")
}
func (cmd *change) Description() string {
return `Change historical metric intervals.
Examples:
govc metric.interval.change -i 300 -level 2
govc metric.interval.change -i 86400 -enabled=false`
}
func (cmd *change) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
intervals, err := m.HistoricalInterval(ctx)
if err != nil {
return err
}
interval := cmd.Interval(0)
if interval == 0 {
return flag.ErrHelp
}
var current *types.PerfInterval
for _, i := range intervals {
if i.SamplingPeriod == interval {
current = &i
break
}
}
if current == nil {
return fmt.Errorf("%d interval ID not found", interval)
}
if cmd.level != 0 {
if cmd.level > 4 {
return flag.ErrHelp
}
current.Level = int32(cmd.level)
}
if cmd.enabled != nil {
current.Enabled = *cmd.enabled
}
_, err = methods.UpdatePerfInterval(ctx, m.Client(), &types.UpdatePerfInterval{
This: m.Reference(),
Interval: *current,
})
return err
}

View File

@@ -0,0 +1,87 @@
/*
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 interval
import (
"context"
"flag"
"fmt"
"text/tabwriter"
"time"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/metric"
)
type info struct {
*metric.PerformanceFlag
}
func init() {
cli.Register("metric.interval.info", &info{})
}
func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = metric.NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
}
func (cmd *info) Description() string {
return `List historical metric intervals.
Examples:
govc metric.interval.info
govc metric.interval.info -i 300`
}
func (cmd *info) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
intervals, err := m.HistoricalInterval(ctx)
if err != nil {
return err
}
tw := tabwriter.NewWriter(cmd.Out, 2, 0, 2, ' ', 0)
cmd.Out = tw
interval := cmd.Interval(0)
for _, i := range intervals {
if interval != 0 && i.SamplingPeriod != interval {
continue
}
fmt.Fprintf(cmd.Out, "ID:\t%d\n", i.SamplingPeriod)
fmt.Fprintf(cmd.Out, " Enabled:\t%t\n", i.Enabled)
fmt.Fprintf(cmd.Out, " Interval:\t%s\n", time.Duration(i.SamplingPeriod)*time.Second)
fmt.Fprintf(cmd.Out, " Name:\t%s\n", i.Name)
fmt.Fprintf(cmd.Out, " Level:\t%d\n", i.Level)
}
return tw.Flush()
}

140
vendor/github.com/vmware/govmomi/govc/metric/ls.go generated vendored Normal file
View File

@@ -0,0 +1,140 @@
/*
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"
"text/tabwriter"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/performance"
"github.com/vmware/govmomi/vim25/types"
)
type ls struct {
*PerformanceFlag
long bool
}
func init() {
cli.Register("metric.ls", &ls{})
}
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
f.BoolVar(&cmd.long, "l", false, "Long listing format")
}
func (cmd *ls) Usage() string {
return "PATH"
}
func (cmd *ls) Description() string {
return `List available metrics for PATH.
Examples:
govc metric.ls /dc1/host/cluster1
govc metric.ls datastore/*
govc metric.ls vm/* | grep mem. | xargs govc metric.sample vm/*`
}
func (cmd *ls) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
type lsResult struct {
cmd *ls
counters map[int32]*types.PerfCounterInfo
performance.MetricList
}
func (r *lsResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
for _, id := range r.MetricList {
if id.Instance != "" {
continue
}
info := r.counters[id.CounterId]
if r.cmd.long {
fmt.Fprintf(w, "%s\t%s\n", info.Name(),
info.NameInfo.GetElementDescription().Label)
continue
}
fmt.Fprintln(w, info.Name())
}
return tw.Flush()
}
func (r *lsResult) MarshalJSON() ([]byte, error) {
m := make(map[string]*types.PerfCounterInfo)
for _, id := range r.MetricList {
info := r.counters[id.CounterId]
m[info.Name()] = info
}
return json.Marshal(m)
}
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}
objs, err := cmd.ManagedObjects(ctx, f.Args())
if err != nil {
return err
}
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
s, err := m.ProviderSummary(ctx, objs[0])
if err != nil {
return err
}
mids, err := m.AvailableMetric(ctx, objs[0], cmd.Interval(s.RefreshRate))
if err != nil {
return err
}
counters, err := m.CounterInfoByKey(ctx)
if err != nil {
return err
}
return cmd.WriteResult(&lsResult{cmd, counters, mids})
}

View File

@@ -0,0 +1,96 @@
/*
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"
"flag"
"fmt"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/performance"
)
type PerformanceFlag struct {
*flags.DatacenterFlag
*flags.OutputFlag
m *performance.Manager
interval int
}
func NewPerformanceFlag(ctx context.Context) (*PerformanceFlag, context.Context) {
f := &PerformanceFlag{}
f.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
f.OutputFlag, ctx = flags.NewOutputFlag(ctx)
return f, ctx
}
func (f *PerformanceFlag) Register(ctx context.Context, fs *flag.FlagSet) {
f.DatacenterFlag.Register(ctx, fs)
f.OutputFlag.Register(ctx, fs)
fs.IntVar(&f.interval, "i", 0, "Interval ID")
}
func (f *PerformanceFlag) Process(ctx context.Context) error {
if err := f.DatacenterFlag.Process(ctx); err != nil {
return err
}
if err := f.OutputFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (f *PerformanceFlag) Manager(ctx context.Context) (*performance.Manager, error) {
if f.m != nil {
return f.m, nil
}
c, err := f.Client()
if err != nil {
return nil, err
}
f.m = performance.NewManager(c)
f.m.Sort = true
return f.m, err
}
func (f *PerformanceFlag) Interval(val int32) int32 {
interval := int32(f.interval)
if interval == 0 {
if val == -1 {
// realtime not supported
return 300
}
return val
}
return interval
}
func (f *PerformanceFlag) ErrNotFound(name string) error {
return fmt.Errorf("counter %q not found", name)
}

91
vendor/github.com/vmware/govmomi/govc/metric/reset.go generated vendored Normal file
View File

@@ -0,0 +1,91 @@
/*
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"
"flag"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
type reset struct {
*PerformanceFlag
}
func init() {
cli.Register("metric.reset", &reset{})
}
func (cmd *reset) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
}
func (cmd *reset) Usage() string {
return "NAME..."
}
func (cmd *reset) Description() string {
return `Reset counter NAME to the default level of data collection.
Examples:
govc metric.reset net.bytesRx.average net.bytesTx.average`
}
func (cmd *reset) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (cmd *reset) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() == 0 {
return flag.ErrHelp
}
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
counters, err := m.CounterInfoByName(ctx)
if err != nil {
return err
}
var ids []int32
for _, name := range f.Args() {
counter, ok := counters[name]
if !ok {
return cmd.ErrNotFound(name)
}
ids = append(ids, counter.Key)
}
_, err = methods.ResetCounterLevelMapping(ctx, m.Client(), &types.ResetCounterLevelMapping{
This: m.Reference(),
Counters: ids,
})
return err
}

330
vendor/github.com/vmware/govmomi/govc/metric/sample.go generated vendored Normal file
View File

@@ -0,0 +1,330 @@
/*
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"
"crypto/md5"
"flag"
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
"text/tabwriter"
"time"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/performance"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
type sample struct {
*PerformanceFlag
d int
n int
t bool
plot string
instance string
}
func init() {
cli.Register("metric.sample", &sample{})
}
func (cmd *sample) Register(ctx context.Context, f *flag.FlagSet) {
cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
cmd.PerformanceFlag.Register(ctx, f)
f.IntVar(&cmd.d, "d", 30, "Limit object display name to D chars")
f.IntVar(&cmd.n, "n", 6, "Max number of samples")
f.StringVar(&cmd.plot, "plot", "", "Plot data using gnuplot")
f.BoolVar(&cmd.t, "t", false, "Include sample times")
f.StringVar(&cmd.instance, "instance", "*", "Instance")
}
func (cmd *sample) Usage() string {
return "PATH... NAME..."
}
func (cmd *sample) Description() string {
return `Sample for object PATH of metric NAME.
Interval ID defaults to 20 (realtime) if supported, otherwise 300 (5m interval).
By default, INSTANCE '*' samples all instances and the aggregate counter.
An INSTANCE value of '-' will only sample the aggregate counter.
An INSTANCE value other than '*' or '-' will only sample the given instance counter.
If PLOT value is set to '-', output a gnuplot script. If non-empty with another
value, PLOT will pipe the script to gnuplot for you. The value is also used to set
the gnuplot 'terminal' variable, unless the value is that of the DISPLAY env var.
Only 1 metric NAME can be specified when the PLOT flag is set.
Examples:
govc metric.sample host/cluster1/* cpu.usage.average
govc metric.sample -plot .png host/cluster1/* cpu.usage.average | xargs open
govc metric.sample vm/* net.bytesTx.average net.bytesTx.average
govc metric.sample -instance vmnic0 vm/* net.bytesTx.average
govc metric.sample -instance - vm/* net.bytesTx.average`
}
func (cmd *sample) Process(ctx context.Context) error {
if err := cmd.PerformanceFlag.Process(ctx); err != nil {
return err
}
return nil
}
type sampleResult struct {
cmd *sample
m *performance.Manager
counters map[string]*types.PerfCounterInfo
Sample []performance.EntityMetric
}
func (r *sampleResult) name(e types.ManagedObjectReference) string {
var me mo.ManagedEntity
_ = r.m.Properties(context.Background(), e, []string{"name"}, &me)
name := me.Name
if r.cmd.d > 0 && len(name) > r.cmd.d {
return name[:r.cmd.d] + "*"
}
return name
}
func sampleInfoTimes(m *performance.EntityMetric) []string {
vals := make([]string, len(m.SampleInfo))
for i := range m.SampleInfo {
vals[i] = m.SampleInfo[i].Timestamp.Format(time.RFC3339)
}
return vals
}
func (r *sampleResult) Plot(w io.Writer) error {
if len(r.Sample) == 0 {
return nil
}
if r.cmd.plot != "-" {
cmd := exec.Command("gnuplot", "-persist")
cmd.Stdout = w
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
return err
}
w = stdin
defer func() {
_ = stdin.Close()
_ = cmd.Wait()
}()
}
counter := r.counters[r.Sample[0].Value[0].Name]
unit := counter.UnitInfo.GetElementDescription()
fmt.Fprintf(w, "set title %q\n", counter.Name())
fmt.Fprintf(w, "set ylabel %q\n", unit.Label)
fmt.Fprintf(w, "set xlabel %q\n", "Time")
fmt.Fprintf(w, "set xdata %s\n", "time")
fmt.Fprintf(w, "set format x %q\n", "%H:%M")
fmt.Fprintf(w, "set timefmt %q\n", "%Y-%m-%dT%H:%M:%SZ")
ext := path.Ext(r.cmd.plot)
if ext != "" {
// If a file name is given, use the extension as terminal type.
// If just an ext is given, use the entities and counter as the file name.
file := r.cmd.plot
name := r.cmd.plot[:len(r.cmd.plot)-len(ext)]
r.cmd.plot = ext[1:]
if name == "" {
h := md5.New()
for i := range r.Sample {
_, _ = io.WriteString(h, r.Sample[i].Entity.String())
}
_, _ = io.WriteString(h, counter.Name())
file = fmt.Sprintf("govc-plot-%x%s", h.Sum(nil), ext)
}
fmt.Fprintf(w, "set output %q\n", file)
defer func() {
fmt.Fprintln(r.cmd.Out, file)
}()
}
switch r.cmd.plot {
case "-", os.Getenv("DISPLAY"):
default:
fmt.Fprintf(w, "set terminal %s\n", r.cmd.plot)
}
if unit.Key == string(types.PerformanceManagerUnitPercent) {
fmt.Fprintln(w, "set yrange [0:100]")
}
fmt.Fprintln(w)
var set []string
for i := range r.Sample {
name := r.name(r.Sample[i].Entity)
name = strings.Replace(name, "_", "*", -1) // underscore is some gnuplot markup?
set = append(set, fmt.Sprintf("'-' using 1:2 title '%s' with lines", name))
}
fmt.Fprintf(w, "plot %s\n", strings.Join(set, ", "))
for i := range r.Sample {
times := sampleInfoTimes(&r.Sample[i])
for _, value := range r.Sample[i].Value {
for j := range value.Value {
fmt.Fprintf(w, "%s %s\n", times[j], value.Format(value.Value[j]))
}
}
fmt.Fprintln(w, "e")
}
return nil
}
func (r *sampleResult) Write(w io.Writer) error {
if r.cmd.plot != "" {
return r.Plot(w)
}
cmd := r.cmd
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
for i := range r.Sample {
metric := r.Sample[i]
name := r.name(metric.Entity)
t := ""
if cmd.t {
t = metric.SampleInfoCSV()
}
for _, v := range metric.Value {
counter := r.counters[v.Name]
units := counter.UnitInfo.GetElementDescription().Label
instance := v.Instance
if instance == "" {
instance = "-"
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%v\t%s\t%s\n",
name, instance, v.Name, t, v.ValueCSV(), units)
}
}
return tw.Flush()
}
func (cmd *sample) Run(ctx context.Context, f *flag.FlagSet) error {
m, err := cmd.Manager(ctx)
if err != nil {
return err
}
var paths []string
var names []string
byName, err := m.CounterInfoByName(ctx)
if err != nil {
return err
}
for _, arg := range f.Args() {
if _, ok := byName[arg]; ok {
names = append(names, arg)
} else {
paths = append(paths, arg)
}
}
if len(paths) == 0 || len(names) == 0 {
return flag.ErrHelp
}
if cmd.plot != "" {
if len(names) > 1 {
return flag.ErrHelp
}
if cmd.instance == "*" {
cmd.instance = ""
}
}
objs, err := cmd.ManagedObjects(ctx, paths)
if err != nil {
return err
}
s, err := m.ProviderSummary(ctx, objs[0])
if err != nil {
return err
}
if cmd.instance == "-" {
cmd.instance = ""
}
spec := types.PerfQuerySpec{
Format: string(types.PerfFormatNormal),
MaxSample: int32(cmd.n),
MetricId: []types.PerfMetricId{{Instance: cmd.instance}},
IntervalId: cmd.Interval(s.RefreshRate),
}
sample, err := m.SampleByName(ctx, spec, names, objs)
if err != nil {
return err
}
result, err := m.ToMetricSeries(ctx, sample)
if err != nil {
return err
}
counters, err := m.CounterInfoByName(ctx)
if err != nil {
return err
}
return cmd.WriteResult(&sampleResult{cmd, m, counters, result})
}