ctyun.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 providerdriver
  15. import (
  16. "context"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "time"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/util/influxdb"
  26. )
  27. type CtyunCollect struct {
  28. SBaseCollectDriver
  29. }
  30. func (self *CtyunCollect) GetProvider() string {
  31. return api.CLOUD_PROVIDER_CTYUN
  32. }
  33. func (self *CtyunCollect) IsSupportMetrics() bool {
  34. return true
  35. }
  36. func init() {
  37. Register(&CtyunCollect{})
  38. }
  39. func (self *CtyunCollect) CollectServerMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ServerDetails, start, end time.Time) error {
  40. metrics := []influxdb.SMetricData{}
  41. regionServers := map[string][]api.ServerDetails{}
  42. for i := range res {
  43. regionId := res[i].RegionExtId
  44. _, ok := regionServers[regionId]
  45. if !ok {
  46. regionServers[regionId] = []api.ServerDetails{}
  47. }
  48. regionServers[regionId] = append(regionServers[regionId], res[i])
  49. }
  50. var wg sync.WaitGroup
  51. var mu sync.Mutex
  52. for regionId, servers := range regionServers {
  53. wg.Add(1)
  54. go func(regionId string, servers []api.ServerDetails) {
  55. defer func() {
  56. wg.Done()
  57. }()
  58. data := []cloudprovider.MetricValues{}
  59. for i := 0; i < (len(servers)+9)/10; i++ {
  60. opts := &cloudprovider.MetricListOptions{
  61. ResourceType: cloudprovider.METRIC_RESOURCE_TYPE_SERVER,
  62. RegionExtId: regionId,
  63. StartTime: start,
  64. EndTime: end,
  65. }
  66. last := (i + 1) * 10
  67. if last > len(servers) {
  68. last = len(servers)
  69. }
  70. for j := range servers[i*10 : last] {
  71. opts.ResourceIds = append(opts.ResourceIds, servers[i*10+j].ExternalId)
  72. }
  73. part, err := provider.GetMetrics(opts)
  74. if err != nil {
  75. if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
  76. log.Errorf("get server %s(%s) error: %v", strings.Join(opts.ResourceIds, ","), regionId, err)
  77. continue
  78. }
  79. continue
  80. }
  81. data = append(data, part...)
  82. }
  83. for _, value := range data {
  84. server, ok := res[value.Id]
  85. if !ok {
  86. continue
  87. }
  88. tags := []influxdb.SKeyValue{}
  89. for k, v := range server.GetMetricTags() {
  90. tags = append(tags, influxdb.SKeyValue{
  91. Key: k,
  92. Value: v,
  93. })
  94. }
  95. pairs := []influxdb.SKeyValue{}
  96. for k, v := range server.GetMetricPairs() {
  97. pairs = append(pairs, influxdb.SKeyValue{
  98. Key: k,
  99. Value: v,
  100. })
  101. }
  102. for _, v := range value.Values {
  103. metric := influxdb.SMetricData{
  104. Name: value.MetricType.Name(),
  105. Timestamp: v.Timestamp,
  106. Tags: []influxdb.SKeyValue{},
  107. Metrics: []influxdb.SKeyValue{
  108. {
  109. Key: value.MetricType.Key(),
  110. Value: strconv.FormatFloat(v.Value, 'E', -1, 64),
  111. },
  112. },
  113. }
  114. for k, v := range v.Tags {
  115. metric.Tags = append(metric.Tags, influxdb.SKeyValue{
  116. Key: k,
  117. Value: v,
  118. })
  119. }
  120. metric.Metrics = append(metric.Metrics, pairs...)
  121. metric.Tags = append(metric.Tags, tags...)
  122. mu.Lock()
  123. metrics = append(metrics, metric)
  124. mu.Unlock()
  125. }
  126. }
  127. }(regionId, servers)
  128. }
  129. wg.Wait()
  130. return self.sendMetrics(ctx, manager, "server", len(res), metrics)
  131. }