@@ -3,7 +3,6 @@ package main
33import (
44 "context"
55 "fmt"
6- "os"
76 "strconv"
87 "time"
98
@@ -19,6 +18,7 @@ import (
1918 "github.com/crowdsecurity/crowdsec/pkg/csconfig"
2019 "github.com/crowdsecurity/crowdsec/pkg/cwhub"
2120 "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
21+ "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
2222 "github.com/crowdsecurity/crowdsec/pkg/metrics"
2323 "github.com/crowdsecurity/crowdsec/pkg/parser"
2424 "github.com/crowdsecurity/crowdsec/pkg/pipeline"
@@ -70,23 +70,23 @@ func initCrowdsec(ctx context.Context, cConfig *csconfig.Config, hub *cwhub.Hub,
7070 return csParsers , datasources , nil
7171}
7272
73- func startParserRoutines (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config , parsers * parser.Parsers ) {
73+ func startParserRoutines (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config , parsers * parser.Parsers , stageCollector * parser. StageParseCollector ) {
7474 for idx := range cConfig .Crowdsec .ParserRoutinesCount {
7575 log .WithField ("idx" , idx ).Info ("Starting parser routine" )
7676 g .Go (func () error {
7777 defer trace .CatchPanic ("crowdsec/runParse/" + strconv .Itoa (idx ))
78- runParse (ctx , logLines , inEvents , * parsers .Ctx , parsers .Nodes )
78+ runParse (ctx , logLines , inEvents , * parsers .Ctx , parsers .Nodes , stageCollector )
7979 return nil
8080 })
8181 }
8282}
8383
84- func startBucketRoutines (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config ) {
84+ func startBucketRoutines (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config , pourCollector * leakybucket. PourCollector ) {
8585 for idx := range cConfig .Crowdsec .BucketsRoutinesCount {
8686 log .WithField ("idx" , idx ).Info ("Starting bucket routine" )
8787 g .Go (func () error {
8888 defer trace .CatchPanic ("crowdsec/runPour/" + strconv .Itoa (idx ))
89- runPour (ctx , inEvents , holders , buckets , cConfig )
89+ runPour (ctx , inEvents , holders , buckets , cConfig , pourCollector )
9090 return nil
9191 })
9292 }
@@ -97,12 +97,12 @@ func startHeartBeat(ctx context.Context, _ *csconfig.Config, apiClient *apiclien
9797 apiClient .HeartBeat .StartHeartBeat (ctx )
9898}
9999
100- func startOutputRoutines (ctx context.Context , cConfig * csconfig.Config , parsers * parser.Parsers , apiClient * apiclient.ApiClient ) {
100+ func startOutputRoutines (ctx context.Context , cConfig * csconfig.Config , parsers * parser.Parsers , apiClient * apiclient.ApiClient , stageCollector * parser. StageParseCollector , bucketOverflows []pipeline. Event ) {
101101 for idx := range cConfig .Crowdsec .OutputRoutinesCount {
102102 log .WithField ("idx" , idx ).Info ("Starting output routine" )
103103 outputsTomb .Go (func () error {
104104 defer trace .CatchPanic ("crowdsec/runOutput/" + strconv .Itoa (idx ))
105- return runOutput (ctx , inEvents , outEvents , buckets , * parsers .PovfwCtx , parsers .Povfwnodes , apiClient )
105+ return runOutput (ctx , inEvents , outEvents , buckets , * parsers .PovfwCtx , parsers .Povfwnodes , apiClient , stageCollector , bucketOverflows )
106106 })
107107 }
108108}
@@ -135,12 +135,20 @@ func startLPMetrics(ctx context.Context, cConfig *csconfig.Config, apiClient *ap
135135}
136136
137137// runCrowdsec starts the log processor service
138- func runCrowdsec (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config , parsers * parser.Parsers , hub * cwhub.Hub , datasources []acquisitionTypes.DataSource ) error {
138+ func runCrowdsec (
139+ ctx context.Context ,
140+ g * errgroup.Group ,
141+ cConfig * csconfig.Config ,
142+ parsers * parser.Parsers ,
143+ hub * cwhub.Hub ,
144+ datasources []acquisitionTypes.DataSource ,
145+ sd * StateDumper ,
146+ ) error {
139147 inEvents = make (chan pipeline.Event )
140148 logLines = make (chan pipeline.Event )
141149
142- startParserRoutines (ctx , g , cConfig , parsers )
143- startBucketRoutines (ctx , g , cConfig )
150+ startParserRoutines (ctx , g , cConfig , parsers , sd . StageParse )
151+ startBucketRoutines (ctx , g , cConfig , sd . Pour )
144152
145153 apiClient , err := apiclient .GetLAPIClient ()
146154 if err != nil {
@@ -149,7 +157,7 @@ func runCrowdsec(ctx context.Context, g *errgroup.Group, cConfig *csconfig.Confi
149157
150158 startHeartBeat (ctx , cConfig , apiClient )
151159
152- startOutputRoutines (ctx , cConfig , parsers , apiClient )
160+ startOutputRoutines (ctx , cConfig , parsers , apiClient , sd . StageParse , sd . BucketOverflows )
153161
154162 if err := startLPMetrics (ctx , cConfig , apiClient , hub , datasources ); err != nil {
155163 return err
@@ -165,7 +173,15 @@ func runCrowdsec(ctx context.Context, g *errgroup.Group, cConfig *csconfig.Confi
165173}
166174
167175// serveCrowdsec wraps the log processor service
168- func serveCrowdsec (ctx context.Context , parsers * parser.Parsers , cConfig * csconfig.Config , hub * cwhub.Hub , datasources []acquisitionTypes.DataSource , agentReady chan bool ) {
176+ func serveCrowdsec (
177+ ctx context.Context ,
178+ parsers * parser.Parsers ,
179+ cConfig * csconfig.Config ,
180+ hub * cwhub.Hub ,
181+ datasources []acquisitionTypes.DataSource ,
182+ agentReady chan bool ,
183+ sd * StateDumper ,
184+ ) {
169185 cctx , cancel := context .WithCancel (ctx )
170186
171187 var g errgroup.Group
@@ -180,7 +196,7 @@ func serveCrowdsec(ctx context.Context, parsers *parser.Parsers, cConfig *csconf
180196
181197 agentReady <- true
182198
183- if err := runCrowdsec (cctx , & g , cConfig , parsers , hub , datasources ); err != nil {
199+ if err := runCrowdsec (cctx , & g , cConfig , parsers , hub , datasources , sd ); err != nil {
184200 log .Fatalf ("unable to start crowdsec routines: %s" , err )
185201 }
186202 }()
@@ -197,15 +213,16 @@ func serveCrowdsec(ctx context.Context, parsers *parser.Parsers, cConfig *csconf
197213 }
198214
199215 log .Debugf ("everything is dead, return crowdsecTomb" )
216+ log .Debugf ("sd.DumpDir == %s" , sd .DumpDir )
200217
201- if flags .DumpDir != "" {
202- log .Debugf ("Dumping parser+bucket states to %s" , flags .DumpDir )
218+ if sd .DumpDir != "" {
219+ log .Debugf ("Dumping parser+bucket states to %s" , sd .DumpDir )
203220
204- if err := dumpAllStates ( flags . DumpDir ); err != nil {
221+ if err := sd . Dump ( ); err != nil {
205222 log .Fatal (err )
206223 }
207224
208- os . Exit ( 0 )
225+ return nil
209226 }
210227
211228 return nil
0 commit comments