alerts.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 misc
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/monitor"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/tsdb"
  23. "yunion.io/x/onecloud/pkg/cloudmon/options"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  27. "yunion.io/x/onecloud/pkg/util/influxdb"
  28. )
  29. const (
  30. ALERT_METRIC_DATABASE = "monitor"
  31. ALERT_RECORD_HISTORY_MEASUREMENT = "alert_record_history"
  32. )
  33. func AlertHistoryReport(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
  34. err := func() error {
  35. s := auth.GetAdminSession(ctx, options.Options.Region)
  36. resp, err := monitor.AlertRecordManager.Get(s, "history-alert", nil)
  37. if err != nil {
  38. return errors.Wrapf(err, "history-alert")
  39. }
  40. alerts := api.AlertRecordHistoryAlert{}
  41. err = resp.Unmarshal(&alerts)
  42. if err != nil {
  43. return errors.Wrapf(err, "Unmarshal AlertRecordHistoryAlert")
  44. }
  45. metrics := []influxdb.SMetricData{}
  46. for _, alert := range alerts.Data {
  47. if alert.ResType == "host" && len(alert.DomainId) == 0 {
  48. continue
  49. }
  50. if alert.ResType == "guest" && len(alert.ProjectId) == 0 {
  51. continue
  52. }
  53. metric := influxdb.SMetricData{}
  54. metric.Name = ALERT_RECORD_HISTORY_MEASUREMENT
  55. metric.Timestamp = time.Now()
  56. metric.Metrics = []influxdb.SKeyValue{
  57. {
  58. Key: "res_num",
  59. Value: fmt.Sprintf("%d", alert.ResNum),
  60. },
  61. }
  62. for k, v := range alert.GetMetricTags() {
  63. metric.Tags = append(metric.Tags, influxdb.SKeyValue{
  64. Key: k,
  65. Value: v,
  66. })
  67. }
  68. metrics = append(metrics, metric)
  69. }
  70. urls, err := tsdb.GetDefaultServiceSourceURLs(s, options.Options.SessionEndpointType)
  71. if err != nil {
  72. return errors.Wrap(err, "GetServiceURLs")
  73. }
  74. return influxdb.BatchSendMetrics(urls, ALERT_METRIC_DATABASE, metrics, false)
  75. }()
  76. if err != nil {
  77. log.Errorf("AlertHistoryReport error: %v", err)
  78. }
  79. }