monitor.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. package cloudpods
  15. import (
  16. "crypto/sha256"
  17. "fmt"
  18. "strings"
  19. "time"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/utils"
  24. monitorapi "yunion.io/x/onecloud/pkg/apis/monitor"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  26. )
  27. var allowTags = map[string]bool{
  28. "device": true,
  29. "path": true,
  30. "fstype": true,
  31. }
  32. func (cli *SCloudpodsClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
  33. usefulResourceType := []cloudprovider.TResourceType{cloudprovider.METRIC_RESOURCE_TYPE_HOST, cloudprovider.METRIC_RESOURCE_TYPE_SERVER}
  34. isUse, _ := utils.InArray(opts.ResourceType, usefulResourceType)
  35. if !isUse {
  36. return nil, nil
  37. }
  38. info := strings.Split(string(opts.MetricType), ".")
  39. if len(info) != 2 {
  40. return nil, errors.Errorf("invalid metric type: %s", opts.MetricType)
  41. }
  42. measurement := info[0]
  43. field := info[1]
  44. from := fmt.Sprintf("now-%dm", int(time.Now().Sub(opts.StartTime).Minutes()))
  45. to := fmt.Sprintf("now-%dm", int(time.Now().Sub(opts.EndTime).Minutes()))
  46. query := &monitorapi.AlertQuery{
  47. Model: monitorapi.MetricQuery{
  48. Database: "telegraf",
  49. Measurement: measurement,
  50. Selects: []monitorapi.MetricQuerySelect{
  51. {
  52. {
  53. Type: "field",
  54. Params: []string{field},
  55. },
  56. },
  57. },
  58. Tags: []monitorapi.MetricQueryTag{
  59. {
  60. Key: "brand",
  61. Operator: "=",
  62. Value: "OneCloud",
  63. },
  64. },
  65. },
  66. From: from,
  67. To: to,
  68. }
  69. input := monitorapi.MetricQueryInput{
  70. From: from,
  71. To: to,
  72. Scope: "system",
  73. Interval: "1m",
  74. MetricQuery: []*monitorapi.AlertQuery{
  75. query,
  76. },
  77. SkipCheckSeries: true,
  78. }
  79. if cli.debug {
  80. input.ShowMeta = true
  81. }
  82. data := jsonutils.Marshal(input).(*jsonutils.JSONDict)
  83. sum256 := sha256.Sum256([]byte(data.String()))
  84. data.Set("signature", jsonutils.NewString(fmt.Sprintf("%x", sum256)))
  85. resp, err := monitor.UnifiedMonitorManager.PerformClassAction(cli.s, "query", data)
  86. if err != nil {
  87. return nil, errors.Wrapf(err, "query metric")
  88. }
  89. metrics := &monitorapi.MetricsQueryResult{}
  90. err = resp.Unmarshal(metrics)
  91. if err != nil {
  92. return nil, errors.Wrapf(err, "query metric Unmarshal")
  93. }
  94. res := []cloudprovider.MetricValues{}
  95. for _, serie := range metrics.Series {
  96. if len(serie.Tags) == 0 || (len(serie.Tags["vm_id"]) == 0 && len(serie.Tags["host_id"]) == 0) {
  97. continue
  98. }
  99. id := serie.Tags["vm_id"]
  100. if len(id) == 0 {
  101. id = serie.Tags["host_id"]
  102. }
  103. metric := cloudprovider.MetricValues{
  104. Id: id,
  105. MetricType: opts.MetricType,
  106. Values: []cloudprovider.MetricValue{},
  107. }
  108. values := []cloudprovider.MetricValue{}
  109. for _, point := range serie.Points {
  110. if len(point) != 2 {
  111. continue
  112. }
  113. tags := map[string]string{}
  114. for k, v := range serie.Tags {
  115. if allowTags[k] {
  116. tags[k] = v
  117. }
  118. }
  119. values = append(values, cloudprovider.MetricValue{
  120. Timestamp: point.Time(),
  121. Value: point.Value(),
  122. Tags: tags,
  123. })
  124. }
  125. if len(values) == 0 {
  126. continue
  127. }
  128. metric.Values = values
  129. res = append(res, metric)
  130. }
  131. return res, nil
  132. }