-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
309 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/blackfireio/osinfo" | ||
"time" | ||
|
||
"github.com/crowdsecurity/go-cs-lib/ptr" | ||
"github.com/crowdsecurity/go-cs-lib/trace" | ||
|
||
"github.com/crowdsecurity/crowdsec/pkg/acquisition" | ||
"github.com/crowdsecurity/crowdsec/pkg/apiclient" | ||
"github.com/crowdsecurity/crowdsec/pkg/csconfig" | ||
"github.com/crowdsecurity/crowdsec/pkg/cwversion" | ||
"github.com/crowdsecurity/crowdsec/pkg/fflag" | ||
"github.com/crowdsecurity/crowdsec/pkg/models" | ||
) | ||
|
||
func detectOs() (string, string) { | ||
if cwversion.System == "docker" { | ||
return "docker", "" | ||
} | ||
|
||
osInfo, err := osinfo.GetOSInfo() | ||
if err != nil { | ||
return cwversion.System, "???" | ||
} | ||
|
||
return osInfo.Name, osInfo.Version | ||
} | ||
|
||
func lpMetricsPayload(consoleCfg *csconfig.ConsoleConfig, datasources []acquisition.DataSource, windowSize int, utcStartupTimestamp int64) models.AllMetrics { | ||
meta := &models.MetricsMeta{ | ||
UtcStartupTimestamp: float64(utcStartupTimestamp), | ||
WindowSizeSeconds: int64(windowSize), | ||
} | ||
|
||
osName, osVersion := detectOs() | ||
|
||
os := &models.OSversion{ | ||
Name: osName, | ||
Version: osVersion, | ||
} | ||
|
||
features := fflag.Crowdsec.GetEnabledFeatures() | ||
|
||
datasourceMap := map[string]int64{} | ||
|
||
for _, ds := range datasources { | ||
datasourceMap[ds.GetName()] += 1 | ||
} | ||
|
||
return models.AllMetrics{ | ||
LogProcessors: []models.LogProcessorsMetrics{ | ||
{ | ||
&models.LogProcessorsMetricsItems0{ | ||
BaseMetrics: models.BaseMetrics{ | ||
Meta: meta, | ||
Os: os, | ||
Version: ptr.Of(cwversion.VersionStr()), | ||
FeatureFlags: features, | ||
}, | ||
ConsoleOptions: consoleCfg.EnabledOptions(), | ||
Datasources: datasourceMap, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// lpMetrics collects metrics from the LP and sends them to the LAPI | ||
func lpMetrics(client *apiclient.ApiClient, consoleCfg *csconfig.ConsoleConfig, datasources []acquisition.DataSource) error { | ||
defer trace.CatchPanic("crowdsec/runLpMetrics") | ||
log.Trace("Starting lpMetrics goroutine") | ||
|
||
windowSize := 6 | ||
utcStartupEpoch := time.Now().Unix() | ||
|
||
met := lpMetricsPayload(consoleCfg, datasources, windowSize, utcStartupEpoch) | ||
|
||
ticker := time.NewTicker(time.Duration(windowSize) * time.Second) | ||
|
||
log.Tracef("Sending lp metrics every %d seconds", windowSize) | ||
|
||
LOOP: | ||
for { | ||
select { | ||
case <-ticker.C: | ||
met.LogProcessors[0][0].Meta.UtcNowTimestamp = float64(time.Now().Unix()) | ||
|
||
_, resp, err := client.UsageMetrics.Add(context.Background(), &met) | ||
if err != nil { | ||
log.Errorf("failed to send lp metrics: %s", err) | ||
continue | ||
} | ||
|
||
if resp.Response.StatusCode != http.StatusCreated { | ||
log.Errorf("failed to send lp metrics: %s", resp.Response.Status) | ||
continue | ||
} | ||
|
||
log.Tracef("lp usage metrics sent") | ||
case <-lpMetricsTomb.Dying(): | ||
break LOOP | ||
} | ||
} | ||
|
||
ticker.Stop() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package apiclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/crowdsecurity/crowdsec/pkg/models" | ||
) | ||
|
||
type UsageMetricsService service | ||
|
||
func (s *UsageMetricsService) Add(ctx context.Context, metrics *models.AllMetrics) (interface{}, *Response, error) { | ||
log.Warnf("prefix: %s", s.client.URLPrefix) | ||
u := fmt.Sprintf("%s/usage-metrics/", s.client.URLPrefix) | ||
|
||
req, err := s.client.NewRequest(http.MethodPost, u, &metrics) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var response interface{} | ||
|
||
resp, err := s.client.Do(ctx, req, &response) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return &response, resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
|
||
jwt "github.com/appleboy/gin-jwt/v2" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sanity-io/litter" | ||
"github.com/go-openapi/strfmt" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/crowdsecurity/crowdsec/pkg/models" | ||
) | ||
|
||
// UsageMetrics receives metrics from log processors and remediation components | ||
func (c *Controller) UsageMetrics(gctx *gin.Context) { | ||
var input models.AllMetrics | ||
|
||
claims := jwt.ExtractClaims(gctx) | ||
// TBD: use defined rather than hardcoded key to find back owner | ||
machineID := claims["id"].(string) | ||
|
||
if err := gctx.ShouldBindJSON(&input); err != nil { | ||
log.Errorf("Failed to bind json: %s", err) | ||
gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) | ||
return | ||
} | ||
|
||
if err := input.Validate(strfmt.Default); err != nil { | ||
log.Errorf("Failed to validate input: %s", err) | ||
c.HandleDBErrors(gctx, err) | ||
return | ||
} | ||
|
||
log.Infof("Received all metrics from %s", machineID) | ||
|
||
inputStr := litter.Sdump(input) | ||
log.Info(inputStr) | ||
|
||
gctx.JSON(http.StatusCreated, "FOOBAR") | ||
} |
Oops, something went wrong.