github.com/vmware/govmomi@v0.37.2/govc/metric/change.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  package metric
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/vim25/methods"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type change struct {
    29  	*PerformanceFlag
    30  
    31  	level  int
    32  	device int
    33  }
    34  
    35  func init() {
    36  	cli.Register("metric.change", &change{})
    37  }
    38  
    39  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.PerformanceFlag, ctx = NewPerformanceFlag(ctx)
    41  	cmd.PerformanceFlag.Register(ctx, f)
    42  
    43  	f.IntVar(&cmd.level, "level", 0, "Level for the aggregate counter")
    44  	f.IntVar(&cmd.device, "device-level", 0, "Level for the per device counter")
    45  }
    46  
    47  func (cmd *change) Usage() string {
    48  	return "NAME..."
    49  }
    50  
    51  func (cmd *change) Description() string {
    52  	return `Change counter NAME levels.
    53  
    54  Examples:
    55    govc metric.change -level 1 net.bytesRx.average net.bytesTx.average`
    56  }
    57  
    58  func (cmd *change) Process(ctx context.Context) error {
    59  	if err := cmd.PerformanceFlag.Process(ctx); err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }
    64  
    65  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() == 0 || (cmd.level == 0 && cmd.device == 0) {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	m, err := cmd.Manager(ctx)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	counters, err := m.CounterInfoByName(ctx)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	var mapping []types.PerformanceManagerCounterLevelMapping
    81  
    82  	for _, name := range f.Args() {
    83  		counter, ok := counters[name]
    84  		if !ok {
    85  			return cmd.ErrNotFound(name)
    86  		}
    87  
    88  		mapping = append(mapping, types.PerformanceManagerCounterLevelMapping{
    89  			CounterId:      counter.Key,
    90  			AggregateLevel: int32(cmd.level),
    91  			PerDeviceLevel: int32(cmd.device),
    92  		})
    93  	}
    94  
    95  	_, err = methods.UpdateCounterLevelMapping(ctx, m.Client(), &types.UpdateCounterLevelMapping{
    96  		This:            m.Reference(),
    97  		CounterLevelMap: mapping,
    98  	})
    99  
   100  	return err
   101  }