github.com/vmware/govmomi@v0.37.2/performance/example_test.go (about)

     1  /*
     2  Copyright (c) 2019 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package performance_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/vmware/govmomi/object"
    24  	"github.com/vmware/govmomi/performance"
    25  	"github.com/vmware/govmomi/simulator"
    26  	"github.com/vmware/govmomi/view"
    27  	"github.com/vmware/govmomi/vim25"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  func ExampleManager_ToMetricSeries() {
    32  	simulator.Run(func(ctx context.Context, c *vim25.Client) error {
    33  		// Get virtual machines references
    34  		m := view.NewManager(c)
    35  
    36  		v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, nil, true)
    37  		if err != nil {
    38  			return err
    39  		}
    40  
    41  		defer v.Destroy(ctx)
    42  
    43  		vmsRefs, err := v.Find(ctx, []string{"VirtualMachine"}, nil)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		// Create a PerfManager
    49  		perfManager := performance.NewManager(c)
    50  
    51  		// Retrieve counters name list
    52  		counters, err := perfManager.CounterInfoByName(ctx)
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		var names []string
    58  		for name := range counters {
    59  			names = append(names, name)
    60  		}
    61  
    62  		// Create PerfQuerySpec
    63  		spec := types.PerfQuerySpec{
    64  			MaxSample:  1,
    65  			MetricId:   []types.PerfMetricId{{Instance: "*"}},
    66  			IntervalId: 300,
    67  		}
    68  
    69  		// Query metrics
    70  		sample, err := perfManager.SampleByName(ctx, spec, names, vmsRefs)
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		result, err := perfManager.ToMetricSeries(ctx, sample)
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		// Read result
    81  		for _, metric := range result {
    82  			vm := object.NewVirtualMachine(c, metric.Entity)
    83  			name, err := vm.ObjectName(ctx)
    84  			if err != nil {
    85  				return err
    86  			}
    87  
    88  			for _, v := range metric.Value {
    89  				counter := counters[v.Name]
    90  				units := counter.UnitInfo.GetElementDescription().Label
    91  
    92  				instance := v.Instance
    93  				if instance == "" {
    94  					instance = "-"
    95  				}
    96  
    97  				if len(v.Value) != 0 && v.Name == "sys.uptime.latest" {
    98  					fmt.Printf("%s\t%s\t%s\t%s\n", name, instance, v.Name, units)
    99  					break
   100  				}
   101  			}
   102  		}
   103  		return nil
   104  	})
   105  
   106  	// Output:
   107  	// DC0_H0_VM0	*	sys.uptime.latest	s
   108  	// DC0_H0_VM1	*	sys.uptime.latest	s
   109  	// DC0_C0_RP0_VM0	*	sys.uptime.latest	s
   110  	// DC0_C0_RP0_VM1	*	sys.uptime.latest	s
   111  }