mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #15246 from gouqi11/cloudpodsMonitor
fix(cloudmon):cloudpodsMonitor
This commit is contained in:
@@ -19,13 +19,17 @@ import (
|
||||
)
|
||||
|
||||
type CloudpodsCollect struct {
|
||||
SBaseCollectDriver
|
||||
SCollectByMetricTypeDriver
|
||||
}
|
||||
|
||||
func (self *CloudpodsCollect) GetProvider() string {
|
||||
return api.CLOUD_PROVIDER_CLOUDPODS
|
||||
}
|
||||
|
||||
func (collect *CloudpodsCollect) IsSupportMetrics() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(&CloudpodsCollect{})
|
||||
}
|
||||
|
||||
@@ -539,8 +539,8 @@ func (self *SUnifiedMonitorManager) GetPropertySimpleQuery(ctx context.Context,
|
||||
if input.EndTime.Sub(input.StartTime).Hours() > 1 {
|
||||
return nil, httperrors.NewInputParameterError("The query interval is greater than one hour")
|
||||
}
|
||||
st := input.StartTime.Format("2006-01-02T15:04:05Z")
|
||||
et := input.EndTime.Format("2006-01-02T15:04:05Z")
|
||||
st := input.StartTime.Format(time.RFC3339)
|
||||
et := input.EndTime.Format(time.RFC3339)
|
||||
conditions := []string{}
|
||||
for k, v := range input.Tags {
|
||||
conditions = append(conditions, fmt.Sprintf("%s = '%s'", k, v))
|
||||
@@ -548,6 +548,7 @@ func (self *SUnifiedMonitorManager) GetPropertySimpleQuery(ctx context.Context,
|
||||
conditions = append(conditions, fmt.Sprintf("time >= '%s'", st))
|
||||
conditions = append(conditions, fmt.Sprintf("time <= '%s'", et))
|
||||
sqlstr += strings.Join(conditions, " and ")
|
||||
sqlstr += " limit 2000"
|
||||
dataSource, err := DataSourceManager.GetDefaultSource()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "s.GetDefaultSource")
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/logger"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/webconsole"
|
||||
"yunion.io/x/onecloud/pkg/multicloud"
|
||||
)
|
||||
@@ -513,3 +514,52 @@ func (self *SRegion) CreateInstance(hostId, hypervisor string, opts *cloudprovid
|
||||
ins := &SInstance{}
|
||||
return ins, self.create(&modules.Servers, input, ins)
|
||||
}
|
||||
|
||||
type SMetricData struct {
|
||||
Id string `json:"id"`
|
||||
Time time.Time `json:"time"`
|
||||
Value float64 `json:"value"`
|
||||
}
|
||||
|
||||
func (cli *SCloudpodsClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
|
||||
brandArr := []string{"OneCloud", "VMware"}
|
||||
metrics := []SMetricData{}
|
||||
for i := 0; i < len(brandArr); i++ {
|
||||
params := map[string]interface{}{
|
||||
"metric_name": opts.MetricType,
|
||||
"tags": map[string]interface{}{
|
||||
"brand": brandArr[i],
|
||||
},
|
||||
}
|
||||
onecloudObj, err := monitor.UnifiedMonitorManager.Get(cli.s, "simple-query", jsonutils.Marshal(params))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp := []SMetricData{}
|
||||
onecloudObj.Unmarshal(&tmp, "values")
|
||||
metrics = append(metrics, tmp...)
|
||||
}
|
||||
|
||||
idWithMetric := map[string][]cloudprovider.MetricValue{}
|
||||
for _, v := range metrics {
|
||||
if _, isExist := idWithMetric[v.Id]; !isExist {
|
||||
idWithMetric[v.Id] = []cloudprovider.MetricValue{{
|
||||
Timestamp: v.Time,
|
||||
Value: v.Value}}
|
||||
} else {
|
||||
idWithMetric[v.Id] = append(idWithMetric[v.Id], cloudprovider.MetricValue{
|
||||
Timestamp: v.Time,
|
||||
Value: v.Value})
|
||||
}
|
||||
}
|
||||
|
||||
res := []cloudprovider.MetricValues{}
|
||||
for _, metric := range metrics {
|
||||
res = append(res, cloudprovider.MetricValues{
|
||||
Id: metric.Id,
|
||||
MetricType: opts.MetricType,
|
||||
Values: idWithMetric[metric.Id],
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -154,3 +154,7 @@ func (self *SCloudpodsProvider) GetSysInfo() (jsonutils.JSONObject, error) {
|
||||
func (self *SCloudpodsProvider) GetVersion() string {
|
||||
return self.client.GetVersion()
|
||||
}
|
||||
|
||||
func (provider *SCloudpodsProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
|
||||
return provider.client.GetMetrics(opts)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/cloudpods"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
@@ -60,4 +61,12 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
shellutils.R(&cloudprovider.MetricListOptions{}, "instance-monitor-list", "Instance Monitor List", func(cli *cloudpods.SRegion, opts *cloudprovider.MetricListOptions) error {
|
||||
metrics, err := cli.GetClient().GetMetrics(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(metrics, len(metrics), 0, 0, []string{})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user