host_dmesg.go 3.3 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 models
  15. import (
  16. "context"
  17. "strings"
  18. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/stringutils"
  21. "yunion.io/x/sqlchemy"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SHostDmesgLogManager struct {
  28. db.SOpsLogManager
  29. }
  30. type SHostDmesgLog struct {
  31. db.SOpsLog
  32. LogLevel string `width:"8" charset:"ascii" nullable:"true" list:"user" create:"optional"`
  33. }
  34. var HostDmesgLogManager *SHostDmesgLogManager
  35. var _ db.IModelManager = (*SHostDmesgLogManager)(nil)
  36. var _ db.IModel = (*SHostDmesgLog)(nil)
  37. func NewHostDmesgLogManager(opslog interface{}, tblName string, keyword, keywordPlural string, timeField string, clickhouse bool) SHostDmesgLogManager {
  38. return SHostDmesgLogManager{
  39. SOpsLogManager: db.NewOpsLogManager(opslog, tblName, keyword, keywordPlural, timeField, clickhouse),
  40. }
  41. }
  42. func init() {
  43. tmp := NewHostDmesgLogManager(SHostDmesgLog{}, "hostdmesg_log_tbl", "hostdmesg", "hostdmesgs", "ops_time", consts.OpsLogWithClickhouse)
  44. HostDmesgLogManager = &tmp
  45. HostDmesgLogManager.SetVirtualObject(HostDmesgLogManager)
  46. }
  47. func (manager *SHostDmesgLogManager) LogDmesg(ctx context.Context, host *SHost, logLevel string, opsTime time.Time, notes interface{}, userCred mcclient.TokenCredential) {
  48. dmesgLog := &SHostDmesgLog{}
  49. dmesgLog.OpsTime = opsTime
  50. dmesgLog.LogLevel = logLevel
  51. dmesgLog.ObjId = host.GetId()
  52. dmesgLog.ObjName = host.GetName()
  53. dmesgLog.Action = db.ACT_HOST_DMESG
  54. dmesgLog.ObjType = host.Keyword()
  55. dmesgLog.Notes = stringutils.Interface2String(notes)
  56. dmesgLog.ProjectId = userCred.GetProjectId()
  57. dmesgLog.Project = userCred.GetProjectName()
  58. dmesgLog.ProjectDomainId = userCred.GetProjectDomainId()
  59. dmesgLog.ProjectDomain = userCred.GetProjectDomain()
  60. dmesgLog.UserId = userCred.GetUserId()
  61. dmesgLog.User = userCred.GetUserName()
  62. dmesgLog.DomainId = userCred.GetDomainId()
  63. dmesgLog.Domain = userCred.GetDomainName()
  64. dmesgLog.Roles = strings.Join(userCred.GetRoles(), ",")
  65. dmesgLog.SetModelManager(manager, dmesgLog)
  66. err := manager.TableSpec().Insert(ctx, dmesgLog)
  67. if err != nil {
  68. log.Errorf("fail to insert dmesgLog %s", err)
  69. }
  70. }
  71. func (manager *SHostDmesgLogManager) ListItemFilter(
  72. ctx context.Context,
  73. q *sqlchemy.SQuery,
  74. userCred mcclient.TokenCredential,
  75. input apis.HostDmesgLogListInput,
  76. ) (*sqlchemy.SQuery, error) {
  77. var err error
  78. q, err = manager.SOpsLogManager.ListItemFilter(ctx, q, userCred, input.OpsLogListInput)
  79. if err != nil {
  80. return q, err
  81. }
  82. if len(input.LogLevels) > 0 {
  83. if len(input.LogLevels) == 1 {
  84. q = q.Filter(sqlchemy.Equals(q.Field("log_level"), input.LogLevels[0]))
  85. } else {
  86. q = q.Filter(sqlchemy.In(q.Field("log_level"), input.LogLevels))
  87. }
  88. }
  89. return q, nil
  90. }