alert.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 monitor
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/apis/monitor"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules"
  22. )
  23. var (
  24. Alerts *SAlertManager
  25. Notifications *SNotificationManager
  26. Alertnotification *SAlertnotificationManager
  27. AlertResources *SAlertResourceManager
  28. AlertResourceAlert *SAlertResourceAlertManager
  29. )
  30. type SAlertManager struct {
  31. *modulebase.ResourceManager
  32. }
  33. func NewAlertManager() *SAlertManager {
  34. man := modules.NewMonitorV2Manager("alert", "alerts",
  35. []string{"id", "name", "frequency", "enabled", "settings", "state"},
  36. []string{})
  37. return &SAlertManager{
  38. ResourceManager: &man,
  39. }
  40. }
  41. type SNotificationManager struct {
  42. *modulebase.ResourceManager
  43. }
  44. func NewNotificationManager() *SNotificationManager {
  45. man := modules.NewMonitorV2Manager(
  46. "alert_notification", "alert_notifications",
  47. []string{"id", "name", "type", "is_default", "disable_resolve_message", "send_reminder", "frequency", "settings"},
  48. []string{})
  49. return &SNotificationManager{
  50. ResourceManager: &man,
  51. }
  52. }
  53. type SAlertnotificationManager struct {
  54. *modulebase.JointResourceManager
  55. }
  56. func NewAlertnotificationManager() *SAlertnotificationManager {
  57. man := modules.NewJointMonitorV2Manager("alertnotification", "alertnotifications",
  58. []string{"Alert_ID", "Alert", "Notification_ID", "Notification", "Used_by", "State", "Frequency"},
  59. []string{},
  60. Alerts, Notifications)
  61. return &SAlertnotificationManager{&man}
  62. }
  63. type SAlertResourceManager struct {
  64. *modulebase.ResourceManager
  65. }
  66. func NewAlertResourceManager() *SAlertResourceManager {
  67. man := modules.NewMonitorV2Manager("alertresource", "alertresources",
  68. []string{"Id", "Name", "Type", "Count", "Tags"},
  69. []string{})
  70. return &SAlertResourceManager{
  71. ResourceManager: &man,
  72. }
  73. }
  74. type SAlertResourceAlertManager struct {
  75. *modulebase.JointResourceManager
  76. }
  77. func NewAlertResourceAlertManager() *SAlertResourceAlertManager {
  78. man := modules.NewJointMonitorV2Manager("alertresourcealert", "alertresourcealerts",
  79. []string{"Alert_Resource_ID", "Alert_Resource", "Alert_ID", "Alert", "Alert_Record_ID", "Trigger_Time", "Data", "Common_Alert_Metric_Details"},
  80. []string{},
  81. AlertResources, Alerts)
  82. return &SAlertResourceAlertManager{&man}
  83. }
  84. func init() {
  85. Alerts = NewAlertManager()
  86. Notifications = NewNotificationManager()
  87. AlertResources = NewAlertResourceManager()
  88. for _, m := range []modulebase.IBaseManager{
  89. Alerts,
  90. Notifications,
  91. AlertResources,
  92. } {
  93. modules.Register(m)
  94. }
  95. Alertnotification = NewAlertnotificationManager()
  96. AlertResourceAlert = NewAlertResourceAlertManager()
  97. for _, m := range []modulebase.IBaseManager{
  98. Alertnotification,
  99. AlertResourceAlert,
  100. } {
  101. modules.Register(m)
  102. }
  103. }
  104. func (m *SAlertManager) DoCreate(s *mcclient.ClientSession, config *AlertConfig) (jsonutils.JSONObject, error) {
  105. input := config.ToAlertCreateInput()
  106. return m.Create(s, input.JSON(input))
  107. }
  108. func (m *SAlertManager) DoTestRun(s *mcclient.ClientSession, id string, input *monitor.AlertTestRunInput) (jsonutils.JSONObject, error) {
  109. ret, err := m.PerformAction(s, id, "test-run", input.JSON(input))
  110. if err != nil {
  111. return nil, errors.Wrap(err, "call test-run")
  112. }
  113. return ret, nil
  114. }