yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/monitor.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package shell
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/log"
    21  	"yunion.io/x/pkg/util/timeutils"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/onecloud/pkg/mcclient/modulebase"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
    26  	"yunion.io/x/onecloud/pkg/util/printutils"
    27  	"yunion.io/x/onecloud/pkg/util/shellutils"
    28  )
    29  
    30  func init() {
    31  	type NamespaceListOptions struct {
    32  	}
    33  	shellutils.R(&NamespaceListOptions{}, "namespace-list", "List monbitor metric namespaces", func(cli *aliyun.SRegion, args *NamespaceListOptions) error {
    34  		nslist, err := cli.FetchNamespaces()
    35  		if err != nil {
    36  			return err
    37  		}
    38  		printList(nslist, 0, 0, 0, nil)
    39  		return nil
    40  	})
    41  
    42  	type MetricListOptions struct {
    43  		NAMESPACE string `help:"namespace"`
    44  	}
    45  	shellutils.R(&MetricListOptions{}, "metrics-list", "List metrics in a namespace", func(cli *aliyun.SRegion, args *MetricListOptions) error {
    46  		metrics, err := cli.FetchMetrics(args.NAMESPACE)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		printList(metrics, 0, 0, 0, nil)
    51  		return nil
    52  	})
    53  
    54  	shellutils.R(&cloudprovider.MetricListOptions{}, "metric-list", "List metrics in a namespace", func(cli *aliyun.SRegion, args *cloudprovider.MetricListOptions) error {
    55  		metrics, err := cli.GetClient().GetMetrics(args)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		for i := range metrics {
    60  			log.Infof("metric: %s %s %s", metrics[i].Id, metrics[i].MetricType, metrics[i].Unit)
    61  			printList(metrics[i].Values, len(metrics[i].Values), 0, 0, []string{})
    62  		}
    63  		return nil
    64  	})
    65  
    66  	type DescribeMetricListOptions struct {
    67  		METRIC    string `help:"metric name"`
    68  		NAMESPACE string `help:"name space"`
    69  		Since     string `help:"since, 2019-11-29T11:22:00Z"`
    70  		Until     string `help:"since, 2019-11-30T11:22:00Z"`
    71  	}
    72  	shellutils.R(&DescribeMetricListOptions{}, "metric-data-list", "DescribeMetricList", func(cli *aliyun.SRegion, args *DescribeMetricListOptions) error {
    73  		var since time.Time
    74  		var err error
    75  		if len(args.Since) > 0 {
    76  			since, err = timeutils.ParseTimeStr(args.Since)
    77  			if err != nil {
    78  				return err
    79  			}
    80  		}
    81  		var until time.Time
    82  		if len(args.Until) > 0 {
    83  			until, err = timeutils.ParseTimeStr(args.Until)
    84  			if err != nil {
    85  				return err
    86  			}
    87  		}
    88  		data, err := cli.FetchMetricData(args.METRIC, args.NAMESPACE, since, until)
    89  		if err != nil {
    90  			return err
    91  		}
    92  		result := &modulebase.ListResult{
    93  			Data:  data,
    94  			Total: len(data),
    95  		}
    96  		printutils.PrintJSONList(result, nil)
    97  		return nil
    98  	})
    99  }