utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 balancer
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/monitor"
  20. "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  21. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  22. )
  23. type HostMetric struct {
  24. Id string
  25. Values map[string]float64
  26. }
  27. type HostMetrics struct {
  28. metrics []*HostMetric
  29. indexes map[string]*HostMetric
  30. }
  31. func NewHostMetrics(ms []*HostMetric) *HostMetrics {
  32. h := &HostMetrics{
  33. metrics: ms,
  34. indexes: make(map[string]*HostMetric),
  35. }
  36. for _, m := range ms {
  37. h.indexes[m.Id] = m
  38. }
  39. return h
  40. }
  41. func (hs HostMetrics) JSONString() string {
  42. return jsonutils.Marshal(hs.metrics).String()
  43. }
  44. func (hs HostMetrics) Get(id string) *HostMetric {
  45. return hs.indexes[id]
  46. }
  47. type TsdbQuery struct {
  48. Database string
  49. Measurement string
  50. Fields []string
  51. }
  52. func InfluxdbQuery(
  53. ds *tsdb.DataSource,
  54. idKey string,
  55. hosts []IResource,
  56. query *TsdbQuery) (*HostMetrics, error) {
  57. q := monitor.NewAlertQuery(query.Database, query.Measurement).From("5m").To("now")
  58. sels := q.Selects()
  59. for _, field := range query.Fields {
  60. sels.Select(field).MEAN().AS(field)
  61. }
  62. ids := []string{}
  63. for _, h := range hosts {
  64. ids = append(ids, h.GetId())
  65. }
  66. q.Where().IN(idKey, ids)
  67. q.GroupBy().TAG(idKey).FILL_NULL()
  68. qCtx := q.ToTsdbQuery()
  69. resp, err := tsdb.HandleRequest(context.Background(), ds, qCtx)
  70. if err != nil {
  71. return nil, errors.Wrap(err, "TSDB endpoint Query")
  72. }
  73. ss := resp.Results[""].Series
  74. ms := make([]*HostMetric, len(ss))
  75. for i, s := range ss {
  76. m := &HostMetric{
  77. Id: s.Tags[idKey],
  78. Values: make(map[string]float64),
  79. }
  80. for j, f := range query.Fields {
  81. m.Values[f] = *(s.Points[0][j].(*float64))
  82. }
  83. ms[i] = m
  84. }
  85. return NewHostMetrics(ms), nil
  86. }
  87. func GetAlertSettingThreshold(s *api.AlertSetting) (float64, error) {
  88. if len(s.Conditions) != 1 {
  89. return 0, errors.Errorf("AlertSetting conditions %d != 1", len(s.Conditions))
  90. }
  91. return s.Conditions[0].Evaluator.Params[0], nil
  92. }