statusprobe.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. "yunion.io/x/onecloud/pkg/cloudmon/options"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  26. baseOptions "yunion.io/x/onecloud/pkg/mcclient/options"
  27. "yunion.io/x/onecloud/pkg/util/influxdb"
  28. )
  29. func StatusProbe(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
  30. if options.Options.EnableStatusProbeDebug {
  31. log.Debugf("Start resource status probe")
  32. }
  33. if !options.Options.EnableStatusProbe {
  34. if options.Options.EnableStatusProbeDebug {
  35. log.Debugf("Resource status probe is disabled")
  36. }
  37. return
  38. }
  39. sess := auth.GetSession(ctx, userCred, options.Options.Region)
  40. metrics := make([]influxdb.SMetricData, 0)
  41. for _, model := range options.Options.StatusProbeModels {
  42. mts, err := doModelStatusProbe(sess, model)
  43. if err != nil {
  44. log.Errorf("doModelStatusProbe failed: %s", err)
  45. }
  46. metrics = append(metrics, mts...)
  47. }
  48. err := sendMetrics(sess, metrics, "system")
  49. if err != nil {
  50. log.Errorf("StatusProbe SendMetrics error: %s", err)
  51. }
  52. }
  53. func doModelStatusProbe(sess *mcclient.ClientSession, modelName string) ([]influxdb.SMetricData, error) {
  54. model, err := modulebase.GetModule(sess, modelName)
  55. if err != nil {
  56. return nil, errors.Wrap(err, "GetModule")
  57. }
  58. listOpts := baseOptions.BaseListOptions{}
  59. listOpts.Scope = "max"
  60. listOpts.SummaryStats = true
  61. limit := 0
  62. listOpts.Limit = &limit
  63. results, err := model.List(sess, jsonutils.Marshal(listOpts))
  64. if err != nil {
  65. return nil, errors.Wrap(err, "List")
  66. }
  67. statusInfoTotal := struct {
  68. apis.TotalCountBase
  69. StatusInfo []apis.StatusStatisticStatusInfo
  70. }{}
  71. err = results.Totals.Unmarshal(&statusInfoTotal)
  72. if err != nil {
  73. return nil, errors.Wrap(err, "Unmarshal statusInfoTotal")
  74. }
  75. log.Infof("statusInfoTotal: %s", jsonutils.Marshal(statusInfoTotal))
  76. metrics := make([]influxdb.SMetricData, 0)
  77. totalCount := int64(0)
  78. pendingDeletedCount := int64(0)
  79. for _, statusInfo := range statusInfoTotal.StatusInfo {
  80. metrics = append(metrics, genStatusMetricData(model, statusInfo.Status, statusInfo.TotalCount, statusInfo.PendingDeletedCount))
  81. totalCount += statusInfo.TotalCount
  82. pendingDeletedCount += statusInfo.PendingDeletedCount
  83. }
  84. metrics = append(metrics, genStatusMetricData(model, "total", totalCount, pendingDeletedCount))
  85. if options.Options.EnableStatusProbeDebug {
  86. log.Debugf("StatusProbe for model %s metrics: %s", modelName, jsonutils.Marshal(metrics))
  87. }
  88. return metrics, nil
  89. }
  90. func genStatusMetricData(model modulebase.Manager, status string, count int64, pendingDeletedCount int64) influxdb.SMetricData {
  91. return influxdb.SMetricData{
  92. Name: "status_probe",
  93. Tags: influxdb.TKeyValuePairs{
  94. influxdb.SKeyValue{
  95. Key: "service",
  96. Value: model.ServiceType(),
  97. },
  98. influxdb.SKeyValue{
  99. Key: "model",
  100. Value: model.GetKeyword(),
  101. },
  102. influxdb.SKeyValue{
  103. Key: "status",
  104. Value: status,
  105. },
  106. },
  107. Metrics: influxdb.TKeyValuePairs{
  108. influxdb.SKeyValue{
  109. Key: "count",
  110. Value: fmt.Sprintf("%d", count),
  111. },
  112. influxdb.SKeyValue{
  113. Key: "pending_deleted",
  114. Value: fmt.Sprintf("%d", pendingDeletedCount),
  115. },
  116. },
  117. }
  118. }