github.com/vmware/govmomi@v0.37.2/govc/metric/interval/info.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 interval
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strings"
    24  	"text/tabwriter"
    25  	"time"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/metric"
    29  )
    30  
    31  type info struct {
    32  	*metric.PerformanceFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("metric.interval.info", &info{})
    37  }
    38  
    39  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.PerformanceFlag, ctx = metric.NewPerformanceFlag(ctx)
    41  	cmd.PerformanceFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *info) Description() string {
    45  	return `List historical metric intervals.
    46  
    47  Examples:
    48    govc metric.interval.info
    49    govc metric.interval.info -i 300`
    50  }
    51  
    52  func (cmd *info) Process(ctx context.Context) error {
    53  	if err := cmd.PerformanceFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    60  	m, err := cmd.Manager(ctx)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	intervals, err := m.HistoricalInterval(ctx)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	tw := tabwriter.NewWriter(cmd.Out, 2, 0, 2, ' ', 0)
    71  	cmd.Out = tw
    72  
    73  	interval := cmd.Interval(0)
    74  
    75  	for _, i := range intervals {
    76  		if interval != 0 && i.SamplingPeriod != interval {
    77  			continue
    78  		}
    79  
    80  		period := (time.Duration(i.SamplingPeriod) * time.Second).String()
    81  		period = strings.TrimSuffix(period, "0s")
    82  		if strings.Contains(period, "h") {
    83  			period = strings.TrimSuffix(period, "0m")
    84  		}
    85  		samples := i.Length / i.SamplingPeriod
    86  
    87  		fmt.Fprintf(cmd.Out, "ID:\t%d\n", i.SamplingPeriod)
    88  		fmt.Fprintf(cmd.Out, "  Enabled:\t%t\n", i.Enabled)
    89  		fmt.Fprintf(cmd.Out, "  Interval:\t%s\n", period)
    90  		fmt.Fprintf(cmd.Out, "  Available Samples:\t%d\n", samples)
    91  		fmt.Fprintf(cmd.Out, "  Name:\t%s\n", i.Name)
    92  		fmt.Fprintf(cmd.Out, "  Level:\t%d\n", i.Level)
    93  	}
    94  
    95  	return tw.Flush()
    96  }