service.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 service
  15. import (
  16. "context"
  17. "os"
  18. "time"
  19. _ "yunion.io/x/cloudmux/pkg/multicloud/loader"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  27. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  28. "yunion.io/x/onecloud/pkg/cloudmon/misc"
  29. "yunion.io/x/onecloud/pkg/cloudmon/options"
  30. "yunion.io/x/onecloud/pkg/cloudmon/resources"
  31. "yunion.io/x/onecloud/pkg/mcclient/auth"
  32. _ "yunion.io/x/onecloud/pkg/mcclient/modules/loader"
  33. )
  34. func StartService() {
  35. opts := &options.Options
  36. baseOpts := &options.Options.BaseOptions
  37. common_options.ParseOptions(opts, os.Args, "cloudmon.conf", "cloudmon")
  38. commonOpts := &opts.CommonOptions
  39. app_common.InitAuth(commonOpts, func() {
  40. log.Infof("Auth complete")
  41. })
  42. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, apis.SERVICE_TYPE_CLOUDMON, "", options.OnOptionsChange)
  43. res := resources.NewResources()
  44. if !opts.IsSlaveNode {
  45. cron := cronman.InitCronJobManager(true, options.Options.CronJobWorkerCount, options.Options.TimeZone)
  46. cron.AddJobAtIntervalsWithStartRun("InitResources", time.Duration(opts.ResourcesSyncInterval)*time.Minute, res.Init, true)
  47. cron.AddJobAtIntervals("IncrementResources", time.Duration(opts.ResourcesSyncInterval)*time.Minute, res.IncrementSync)
  48. cron.AddJobAtIntervals("DecrementResources", time.Duration(opts.ResourcesSyncInterval)*time.Minute, res.DecrementSync)
  49. cron.AddJobAtIntervals("UpdateResources", time.Duration(opts.ResourcesSyncInterval)*time.Minute, res.UpdateSync)
  50. cron.AddJobAtIntervalsWithStarTime("CollectResources", time.Duration(opts.CollectMetricInterval)*time.Minute, res.CollectMetrics)
  51. cron.AddJobAtIntervalsWithStartRun("PingProb", time.Duration(opts.PingProbIntervalHours)*time.Hour, misc.PingProbe, true)
  52. cron.AddJobAtIntervalsWithStartRun("StatusProbe", time.Duration(opts.StatusProbeIntervalMinutes)*time.Minute, misc.StatusProbe, true)
  53. cron.AddJobAtIntervalsWithStartRun("BucketProbe", time.Duration(opts.BucketProbeIntervalMinutes)*time.Minute, misc.BucketProbe, true)
  54. cron.AddJobEveryFewDays("UsageMetricCollect", 1, 23, 10, 10, misc.UsegReport, false)
  55. cron.AddJobEveryFewDays("AlertHistoryMetricCollect", 1, 23, 59, 59, misc.AlertHistoryReport, false)
  56. cron.AddJobAtIntervals("CollectServiceMetrics", time.Duration(opts.CollectServiceMetricIntervalMinute)*time.Minute, misc.CollectServiceMetrics)
  57. go cron.Start()
  58. }
  59. ctx := context.Background()
  60. if opts.HistoryMetricPullDays > 0 {
  61. go func() {
  62. for !res.IsInit() {
  63. log.Infof("wait resources init...")
  64. time.Sleep(time.Second * 10)
  65. }
  66. now := time.Now()
  67. start := now.AddDate(0, 0, -1*opts.HistoryMetricPullDays)
  68. s := auth.GetAdminSession(ctx, opts.BaseOptions.Region)
  69. for start.Before(now) {
  70. log.Infof("start collect history metric from %s", start.Format(time.RFC3339))
  71. res.CollectMetrics(ctx, s.GetToken(), start, false)
  72. start = start.Add(time.Duration(opts.CollectMetricInterval) * time.Minute)
  73. }
  74. log.Infof("collect history metric end")
  75. }()
  76. }
  77. app := app_common.InitApp(baseOpts, true).
  78. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  79. session := auth.GetAdminSession(ctx, commonOpts.Region)
  80. notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
  81. })
  82. app_common.ServeForever(app, baseOpts)
  83. }