diff --git a/pkg/cloudmon/options/options.go b/pkg/cloudmon/options/options.go index 43c6ce597a..b719d0205d 100644 --- a/pkg/cloudmon/options/options.go +++ b/pkg/cloudmon/options/options.go @@ -31,6 +31,8 @@ type CloudMonOptions struct { DisableServiceMetric bool `help:"disable service metric collect"` CollectServiceMetricIntervalMinute int64 `help:"Collect Service metirc Interval unit:minute" default:"5"` + HistoryMetricPullDays int `help:"pull history metrics" default:"-1"` + CloudAccountCollectMetricsBatchCount int `help:"Cloud Account Collect Metrics Batch Count" default:"10"` CloudResourceCollectMetricsBatchCount int `help:"Cloud Resource Collect Metrics BatchC ount" default:"40"` } diff --git a/pkg/cloudmon/resources/resources.go b/pkg/cloudmon/resources/resources.go index 6ee58d2536..07ab4c3760 100644 --- a/pkg/cloudmon/resources/resources.go +++ b/pkg/cloudmon/resources/resources.go @@ -372,6 +372,7 @@ type TResource interface { } type SResources struct { + init bool Cloudaccounts TResource Cloudproviders TResource DBInstances TResource @@ -387,6 +388,10 @@ type SResources struct { Projects TResource } +func (self *SResources) IsInit() bool { + return self.init +} + func NewResources() *SResources { return &SResources{ Cloudaccounts: NewBaseResources(&compute.Cloudaccounts), @@ -466,6 +471,7 @@ func (self *SResources) Init(ctx context.Context, userCred mcclient.TokenCredent if err != nil { log.Errorf("Resource init error: %v", err) } + self.init = true } } @@ -658,8 +664,8 @@ func (self *SResources) CollectMetrics(ctx context.Context, userCred mcclient.To resources := self.Cloudproviders.getResources(ctx, "") cloudproviders := map[string]api.CloudproviderDetails{} jsonutils.Update(&cloudproviders, resources) - sh, _ := time.LoadLocation("Asia/Shanghai") - _endTime := taskStartTime.In(sh) + az, _ := time.LoadLocation(options.Options.TimeZone) + _endTime := taskStartTime.In(az) _startTime := _endTime.Add(-1 * time.Minute * time.Duration(options.Options.CollectMetricInterval)) var wg sync.WaitGroup for i := range cloudproviders { diff --git a/pkg/cloudmon/service/service.go b/pkg/cloudmon/service/service.go index 057db05108..55a2261b6d 100644 --- a/pkg/cloudmon/service/service.go +++ b/pkg/cloudmon/service/service.go @@ -64,9 +64,27 @@ func StartService() { go cron.Start() } + ctx := context.Background() + if opts.HistoryMetricPullDays > 0 { + go func() { + for !res.IsInit() { + log.Infof("wait resources init...") + time.Sleep(time.Second * 10) + } + now := time.Now() + start := now.AddDate(0, 0, -1*opts.HistoryMetricPullDays) + s := auth.GetAdminSession(ctx, opts.BaseOptions.Region) + for start.Before(now) { + log.Infof("start collect history metric from %s", start.Format(time.RFC3339)) + res.CollectMetrics(ctx, s.GetToken(), start, false) + start = start.Add(time.Duration(opts.CollectMetricInterval) * time.Minute) + } + log.Infof("collect history metric end") + }() + } + app := app_common.InitApp(baseOpts, true). OnException(func(method, path string, body jsonutils.JSONObject, err error) { - ctx := context.Background() session := auth.GetAdminSession(ctx, commonOpts.Region) notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err) })