From 538ab0bc62f47d6511de1b938ffc9d862eaa6d96 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Mon, 22 Apr 2024 17:44:42 +0200 Subject: [PATCH] up --- pkg/apiserver/apic_metrics.go | 43 +++++++++++++++++++- pkg/apiserver/controllers/v1/usagemetrics.go | 1 - pkg/models/localapi_swagger.yaml | 3 ++ pkg/models/log_processors_metrics.go | 11 +++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/pkg/apiserver/apic_metrics.go b/pkg/apiserver/apic_metrics.go index 9db8c80e7..05ba5fe3f 100644 --- a/pkg/apiserver/apic_metrics.go +++ b/pkg/apiserver/apic_metrics.go @@ -14,7 +14,9 @@ import ( "github.com/crowdsecurity/go-cs-lib/trace" "github.com/crowdsecurity/go-cs-lib/version" + "github.com/crowdsecurity/crowdsec/pkg/cwversion" "github.com/crowdsecurity/crowdsec/pkg/database/ent" + "github.com/crowdsecurity/crowdsec/pkg/fflag" "github.com/crowdsecurity/crowdsec/pkg/models" ) @@ -77,6 +79,12 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) { metrics.Name = lpName metrics.LastPush = lp.LastPush.UTC().Unix() + metrics.LastUpdate = lp.UpdatedAt.UTC().Unix() + + //To prevent marshalling a nil slice to null, which gets rejected by the API + if metrics.Metrics == nil { + metrics.Metrics = make([]*models.MetricsDetailItem, 0) + } allMetrics.LogProcessors = append(allMetrics.LogProcessors, &metrics) metricsIds = append(metricsIds, lpsMetric.ID) @@ -114,13 +122,42 @@ func (a *apic) GetUsageMetrics() (*models.AllMetrics, []int, error) { metrics.Name = bouncerName metrics.LastPull = bouncer.LastPull.UTC().Unix() + //To prevent marshalling a nil slice to null, which gets rejected by the API + if metrics.Metrics == nil { + metrics.Metrics = make([]*models.MetricsDetailItem, 0) + } + allMetrics.RemediationComponents = append(allMetrics.RemediationComponents, &metrics) metricsIds = append(metricsIds, bouncersMetric.ID) } - //bouncerInfos := make(map[string]string) + //FIXME: all of this should only be done once on startup/reload + allMetrics.Lapi = &models.LapiMetrics{ + ConsoleOptions: models.ConsoleOptions{ + "FIXME", + }, + } + allMetrics.Lapi.Os = &models.OSversion{ + Name: "FIXME", + Version: "FIXME", + } + allMetrics.Lapi.Version = ptr.Of(cwversion.VersionStr()) + allMetrics.Lapi.FeatureFlags = fflag.Crowdsec.GetEnabledFeatures() - //TODO: add LAPI metrics + allMetrics.Lapi.Meta = &models.MetricsMeta{ + UtcStartupTimestamp: time.Now().UTC().Unix(), + UtcNowTimestamp: time.Now().UTC().Unix(), + WindowSizeSeconds: int64(a.metricsInterval.Seconds()), + } + allMetrics.Lapi.Metrics = make([]*models.MetricsDetailItem, 0) + + if allMetrics.RemediationComponents == nil { + allMetrics.RemediationComponents = make([]*models.RemediationComponentsMetrics, 0) + } + + if allMetrics.LogProcessors == nil { + allMetrics.LogProcessors = make([]*models.LogProcessorsMetrics, 0) + } return allMetrics, metricsIds, nil } @@ -306,6 +343,8 @@ func (a *apic) SendUsageMetrics() { err = a.MarkUsageMetricsAsSent(metricsId) if err != nil { log.Errorf("unable to mark usage metrics as sent: %s", err) + } else { + log.Infof("Usage metrics sent") } } } diff --git a/pkg/apiserver/controllers/v1/usagemetrics.go b/pkg/apiserver/controllers/v1/usagemetrics.go index 1016ce3df..37aa03350 100644 --- a/pkg/apiserver/controllers/v1/usagemetrics.go +++ b/pkg/apiserver/controllers/v1/usagemetrics.go @@ -108,7 +108,6 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { "type": item0.Type, "metrics": item0.Metrics, "meta": item0.Meta, - // TODO: RC stuff like traffic stats } baseMetrics = item0.BaseMetrics default: diff --git a/pkg/models/localapi_swagger.yaml b/pkg/models/localapi_swagger.yaml index 5a1bee3cd..0ce082e50 100644 --- a/pkg/models/localapi_swagger.yaml +++ b/pkg/models/localapi_swagger.yaml @@ -1060,6 +1060,9 @@ definitions: last_push: type: integer description: last push date + last_update: + type: integer + description: last update date LapiMetrics: title: LapiMetrics type: object diff --git a/pkg/models/log_processors_metrics.go b/pkg/models/log_processors_metrics.go index c8c305d76..ab033b020 100644 --- a/pkg/models/log_processors_metrics.go +++ b/pkg/models/log_processors_metrics.go @@ -31,6 +31,9 @@ type LogProcessorsMetrics struct { // last push date LastPush int64 `json:"last_push,omitempty"` + // last update date + LastUpdate int64 `json:"last_update,omitempty"` + // name of the log processor Name string `json:"name,omitempty"` } @@ -54,6 +57,8 @@ func (m *LogProcessorsMetrics) UnmarshalJSON(raw []byte) error { LastPush int64 `json:"last_push,omitempty"` + LastUpdate int64 `json:"last_update,omitempty"` + Name string `json:"name,omitempty"` } if err := swag.ReadJSON(raw, &dataAO1); err != nil { @@ -68,6 +73,8 @@ func (m *LogProcessorsMetrics) UnmarshalJSON(raw []byte) error { m.LastPush = dataAO1.LastPush + m.LastUpdate = dataAO1.LastUpdate + m.Name = dataAO1.Name return nil @@ -91,6 +98,8 @@ func (m LogProcessorsMetrics) MarshalJSON() ([]byte, error) { LastPush int64 `json:"last_push,omitempty"` + LastUpdate int64 `json:"last_update,omitempty"` + Name string `json:"name,omitempty"` } @@ -102,6 +111,8 @@ func (m LogProcessorsMetrics) MarshalJSON() ([]byte, error) { dataAO1.LastPush = m.LastPush + dataAO1.LastUpdate = m.LastUpdate + dataAO1.Name = m.Name jsonDataAO1, errAO1 := swag.WriteJSON(dataAO1)