baremetalevents.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. "yunion.io/x/sqlchemy"
  22. "yunion.io/x/sqlchemy/backends/clickhouse"
  23. api "yunion.io/x/onecloud/pkg/apis/logger"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. "yunion.io/x/onecloud/pkg/mcclient"
  27. )
  28. type SBaremetalEventManager struct {
  29. db.SModelBaseManager
  30. }
  31. type SBaremetalEvent struct {
  32. db.SModelBase
  33. Id int64 `primary:"true" auto_increment:"true" list:"user" clickhouse_partition_by:"toInt64(id/100000000000)"`
  34. HostId string `width:"128" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  35. HostName string `width:"64" charset:"utf8" nullable:"false" list:"user" create:"required"`
  36. IpmiIp string `width:"16" charset:"ascii" nullable:"true" list:"user" create:"optional"`
  37. Created time.Time `nullable:"false" create:"required" list:"user"`
  38. EventId string `width:"32" nullable:"true" create:"optional" list:"user"`
  39. Type string `width:"10" nullable:"true" create:"optional" list:"user"`
  40. Message string `nullable:"false" create:"required" list:"user"`
  41. Severity string `width:"16" charset:"ascii" nullable:"true" list:"user" create:"optional"`
  42. }
  43. var BaremetalEventManager *SBaremetalEventManager
  44. func InitBaremetalEvent() {
  45. if consts.OpsLogWithClickhouse {
  46. BaremetalEventManager = &SBaremetalEventManager{
  47. SModelBaseManager: db.NewModelBaseManagerWithDBName(
  48. SBaremetalEvent{},
  49. "baremetal_event_tbl",
  50. "baremetalevent",
  51. "baremetalevents",
  52. db.ClickhouseDB,
  53. ),
  54. }
  55. col := BaremetalEventManager.TableSpec().ColumnSpec("ops_time")
  56. if clickCol, ok := col.(clickhouse.IClickhouseColumnSpec); ok {
  57. clickCol.SetTTL(consts.SplitableMaxKeepMonths(), "MONTH")
  58. }
  59. } else {
  60. BaremetalEventManager = &SBaremetalEventManager{
  61. SModelBaseManager: db.NewModelBaseManagerWithSplitable(
  62. SBaremetalEvent{},
  63. "baremetal_event_tbl",
  64. "baremetalevent",
  65. "baremetalevents",
  66. "id",
  67. "created",
  68. consts.SplitableMaxDuration(),
  69. consts.SplitableMaxKeepMonths(),
  70. ),
  71. }
  72. }
  73. BaremetalEventManager.SetVirtualObject(BaremetalEventManager)
  74. }
  75. func (event *SBaremetalEvent) BeforeInsert() {
  76. t := time.Now().UTC()
  77. event.Id = db.CurrentTimestamp(t)
  78. }
  79. func (event *SBaremetalEvent) GetId() string {
  80. return fmt.Sprintf("%d", event.Id)
  81. }
  82. func (event *SBaremetalEvent) GetName() string {
  83. return event.HostName + event.EventId
  84. }
  85. func (event *SBaremetalEvent) GetModelManager() db.IModelManager {
  86. return BaremetalEventManager
  87. }
  88. func (manager *SBaremetalEventManager) CreateByInsertOrUpdate() bool {
  89. return false
  90. }
  91. func (manager *SBaremetalEventManager) ResourceScope() rbacscope.TRbacScope {
  92. return rbacscope.ScopeSystem
  93. }
  94. func (manager *SBaremetalEventManager) GetPagingConfig() *db.SPagingConfig {
  95. return &db.SPagingConfig{
  96. Order: sqlchemy.SQL_ORDER_DESC,
  97. MarkerFields: []string{"id"},
  98. DefaultLimit: 20,
  99. }
  100. }
  101. // 物理机日志列表
  102. func (manager *SBaremetalEventManager) ListItemFilter(
  103. ctx context.Context,
  104. q *sqlchemy.SQuery,
  105. userCred mcclient.TokenCredential,
  106. query api.BaremetalEventListInput,
  107. ) (*sqlchemy.SQuery, error) {
  108. q, err := manager.SModelBaseManager.ListItemFilter(ctx, q, userCred, query.ModelBaseListInput)
  109. if err != nil {
  110. return nil, errors.Wrap(err, "SModelBaseManager.ListItemFilter")
  111. }
  112. if !query.Since.IsZero() {
  113. q = q.GT("created", query.Since)
  114. }
  115. if !query.Until.IsZero() {
  116. q = q.LE("created", query.Until)
  117. }
  118. if len(query.HostId) == 1 {
  119. q = q.Equals("host_id", query.HostId[0])
  120. } else if len(query.HostId) > 1 {
  121. q = q.In("host_id", query.HostId)
  122. }
  123. if len(query.Id) == 1 {
  124. q = q.Equals("id", query.Id[0])
  125. } else if len(query.Id) > 1 {
  126. q = q.In("id", query.Id)
  127. }
  128. if len(query.EventId) == 1 {
  129. q = q.Equals("event_id", query.EventId[0])
  130. } else if len(query.EventId) > 1 {
  131. q = q.In("event_id", query.EventId)
  132. }
  133. if len(query.Type) == 1 {
  134. q = q.Equals("type", query.Type[0])
  135. } else if len(query.Type) > 1 {
  136. q = q.In("type", query.Type)
  137. }
  138. if len(query.IpmiIp) == 1 {
  139. q = q.Equals("ipmi_ip", query.IpmiIp[0])
  140. } else if len(query.IpmiIp) > 1 {
  141. q = q.In("ipmi_ip", query.IpmiIp)
  142. }
  143. return q, nil
  144. }