This commit is contained in:
Sebastien Blot 2024-04-22 17:44:42 +02:00
parent 18b8ddb49a
commit 538ab0bc62
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A
4 changed files with 55 additions and 3 deletions

View file

@ -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")
}
}
}

View file

@ -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:

View file

@ -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

View file

@ -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)