github.com/vmware/govmomi@v0.37.2/examples/perfmanager/main.go (about)

     1  /*
     2  Copyright (c) 2017 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  /*
    18  This example program shows how to collect performance metrics from virtual machines using govmomi.
    19  */
    20  
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"flag"
    26  	"fmt"
    27  
    28  	"github.com/vmware/govmomi/examples"
    29  	"github.com/vmware/govmomi/performance"
    30  	"github.com/vmware/govmomi/view"
    31  	"github.com/vmware/govmomi/vim25"
    32  	"github.com/vmware/govmomi/vim25/types"
    33  )
    34  
    35  var interval = flag.Int("i", 20, "Interval ID")
    36  
    37  func main() {
    38  	examples.Run(func(ctx context.Context, c *vim25.Client) error {
    39  		// Get virtual machines references
    40  		m := view.NewManager(c)
    41  
    42  		v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, nil, true)
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		defer v.Destroy(ctx)
    48  
    49  		vmsRefs, err := v.Find(ctx, []string{"VirtualMachine"}, nil)
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		// Create a PerfManager
    55  		perfManager := performance.NewManager(c)
    56  
    57  		// Retrieve counters name list
    58  		counters, err := perfManager.CounterInfoByName(ctx)
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		var names []string
    64  		for name := range counters {
    65  			names = append(names, name)
    66  		}
    67  
    68  		// Create PerfQuerySpec
    69  		spec := types.PerfQuerySpec{
    70  			MaxSample:  1,
    71  			MetricId:   []types.PerfMetricId{{Instance: "*"}},
    72  			IntervalId: int32(*interval),
    73  		}
    74  
    75  		// Query metrics
    76  		sample, err := perfManager.SampleByName(ctx, spec, names, vmsRefs)
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		result, err := perfManager.ToMetricSeries(ctx, sample)
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		// Read result
    87  		for _, metric := range result {
    88  			name := metric.Entity
    89  
    90  			for _, v := range metric.Value {
    91  				counter := counters[v.Name]
    92  				units := counter.UnitInfo.GetElementDescription().Label
    93  
    94  				instance := v.Instance
    95  				if instance == "" {
    96  					instance = "-"
    97  				}
    98  
    99  				if len(v.Value) != 0 {
   100  					fmt.Printf("%s\t%s\t%s\t%s\t%s\n",
   101  						name, instance, v.Name, v.ValueCSV(), units)
   102  				}
   103  			}
   104  		}
   105  		return nil
   106  	})
   107  }