cleanup
This commit is contained in:
parent
c755a0015e
commit
7428ec351b
7 changed files with 130 additions and 122 deletions
|
@ -23,9 +23,7 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
count++
|
count++
|
||||||
if count%5000 == 0 {
|
if count%5000 == 0 {
|
||||||
if leaky.LeakyRoutineCount != 0 {
|
log.Infof("%d existing buckets", leaky.LeakyRoutineCount)
|
||||||
log.Infof("%d existing buckets", leaky.LeakyRoutineCount)
|
|
||||||
}
|
|
||||||
//when in forensics mode, garbage collect buckets
|
//when in forensics mode, garbage collect buckets
|
||||||
if cConfig.Crowdsec.BucketsGCEnabled {
|
if cConfig.Crowdsec.BucketsGCEnabled {
|
||||||
if parsed.MarshaledTime != "" {
|
if parsed.MarshaledTime != "" {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -33,7 +33,7 @@ require (
|
||||||
github.com/dghubble/sling v1.3.0
|
github.com/dghubble/sling v1.3.0
|
||||||
github.com/docker/docker v24.0.7+incompatible
|
github.com/docker/docker v24.0.7+incompatible
|
||||||
github.com/docker/go-connections v0.4.0
|
github.com/docker/go-connections v0.4.0
|
||||||
github.com/fatih/color v1.15.0
|
github.com/fatih/color v1.16.0
|
||||||
github.com/fsnotify/fsnotify v1.6.0
|
github.com/fsnotify/fsnotify v1.6.0
|
||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/gin-gonic/gin v1.9.1
|
||||||
github.com/go-co-op/gocron v1.17.0
|
github.com/go-co-op/gocron v1.17.0
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -125,8 +125,8 @@ github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
|
||||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||||
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
|
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
|
||||||
github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
|
github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
|
||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
|
@ -543,8 +543,6 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||||
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
|
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
|
||||||
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
||||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
|
||||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
|
||||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||||
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 h1:rc3tiVYb5z54aKaDfakKn0dDjIyPpTtszkjuMzyt7ec=
|
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 h1:rc3tiVYb5z54aKaDfakKn0dDjIyPpTtszkjuMzyt7ec=
|
||||||
|
|
|
@ -6,15 +6,12 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/antonmedv/expr"
|
"github.com/antonmedv/expr"
|
||||||
"github.com/antonmedv/expr/vm"
|
"github.com/antonmedv/expr/vm"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
isatty "github.com/mattn/go-isatty"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
progressbar "github.com/schollz/progressbar/v3"
|
|
||||||
tomb "gopkg.in/tomb.v2"
|
tomb "gopkg.in/tomb.v2"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
@ -320,43 +317,6 @@ func transform(transformChan chan types.Event, output chan types.Event, AcquisTo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// injectProgressBar will inject a progress bar in the acquisition pipeline if stderr is a terminal
|
|
||||||
func injectProgressBar(output chan types.Event, AcquisTomb *tomb.Tomb) chan types.Event {
|
|
||||||
if !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()) {
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
ret := make(chan types.Event)
|
|
||||||
go func() {
|
|
||||||
var pb *progressbar.ProgressBar
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-AcquisTomb.Dying():
|
|
||||||
if pb != nil {
|
|
||||||
pb.Finish()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case evt := <-ret:
|
|
||||||
if pb == nil {
|
|
||||||
// create the progress bar on first event
|
|
||||||
// to avoid log messages from the acquisition source
|
|
||||||
// breaking the progress bar already displayed
|
|
||||||
pb = progressbar.NewOptions(-1,
|
|
||||||
progressbar.OptionSetWriter(os.Stderr),
|
|
||||||
progressbar.OptionClearOnFinish(),
|
|
||||||
progressbar.OptionShowCount(),
|
|
||||||
progressbar.OptionShowIts(),
|
|
||||||
progressbar.OptionSpinnerType(43),
|
|
||||||
progressbar.OptionThrottle(time.Second/10),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
pb.Add(1)
|
|
||||||
output <- evt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb *tomb.Tomb) error {
|
func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb *tomb.Tomb) error {
|
||||||
// Don't wait if we have no sources, as it will hang forever
|
// Don't wait if we have no sources, as it will hang forever
|
||||||
if len(sources) == 0 {
|
if len(sources) == 0 {
|
||||||
|
|
48
pkg/acquisition/progressbar.go
Normal file
48
pkg/acquisition/progressbar.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package acquisition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
isatty "github.com/mattn/go-isatty"
|
||||||
|
progressbar "github.com/schollz/progressbar/v3"
|
||||||
|
tomb "gopkg.in/tomb.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// injectProgressBar will inject a progress bar in the acquisition pipeline if stderr is a terminal
|
||||||
|
func injectProgressBar(output chan types.Event, acquisTomb *tomb.Tomb) chan types.Event {
|
||||||
|
if !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()) {
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := make(chan types.Event)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
pb := progressbar.NewOptions(-1,
|
||||||
|
progressbar.OptionSetWriter(os.Stderr),
|
||||||
|
progressbar.OptionClearOnFinish(),
|
||||||
|
progressbar.OptionShowCount(),
|
||||||
|
progressbar.OptionShowIts(),
|
||||||
|
progressbar.OptionSpinnerType(43),
|
||||||
|
progressbar.OptionThrottle(time.Second/20),
|
||||||
|
progressbar.OptionSetRenderBlankState(false),
|
||||||
|
)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-acquisTomb.Dying():
|
||||||
|
if pb != nil {
|
||||||
|
pb.Finish()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case evt := <-ret:
|
||||||
|
pb.Add(1)
|
||||||
|
output <- evt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
78
pkg/types/logging.go
Normal file
78
pkg/types/logging.go
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logFormatter log.Formatter
|
||||||
|
var LogOutput *lumberjack.Logger //io.Writer
|
||||||
|
var logLevel log.Level
|
||||||
|
|
||||||
|
type CustomFormatter struct {
|
||||||
|
log.TextFormatter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *CustomFormatter) Format(entry *log.Entry) ([]byte, error) {
|
||||||
|
const clearLine = "\r\033[K"
|
||||||
|
|
||||||
|
result, err := f.TextFormatter.Format(entry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return append([]byte(clearLine), result...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
|
||||||
|
/*Configure logs*/
|
||||||
|
if cfgMode == "file" {
|
||||||
|
_maxsize := 500
|
||||||
|
if maxSize != 0 {
|
||||||
|
_maxsize = maxSize
|
||||||
|
}
|
||||||
|
_maxfiles := 3
|
||||||
|
if maxFiles != 0 {
|
||||||
|
_maxfiles = maxFiles
|
||||||
|
}
|
||||||
|
_maxage := 28
|
||||||
|
if maxAge != 0 {
|
||||||
|
_maxage = maxAge
|
||||||
|
}
|
||||||
|
_compress := true
|
||||||
|
if compress != nil {
|
||||||
|
_compress = *compress
|
||||||
|
}
|
||||||
|
|
||||||
|
LogOutput = &lumberjack.Logger{
|
||||||
|
Filename: filepath.Join(cfgFolder, "crowdsec.log"),
|
||||||
|
MaxSize: _maxsize,
|
||||||
|
MaxBackups: _maxfiles,
|
||||||
|
MaxAge: _maxage,
|
||||||
|
Compress: _compress,
|
||||||
|
}
|
||||||
|
log.SetOutput(LogOutput)
|
||||||
|
} else if cfgMode != "stdout" {
|
||||||
|
return fmt.Errorf("log mode '%s' unknown", cfgMode)
|
||||||
|
}
|
||||||
|
logLevel = cfgLevel
|
||||||
|
log.SetLevel(logLevel)
|
||||||
|
logFormatter = &CustomFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}}
|
||||||
|
log.SetFormatter(logFormatter)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConfigureLogger(clog *log.Logger) error {
|
||||||
|
if LogOutput != nil {
|
||||||
|
clog.SetOutput(LogOutput)
|
||||||
|
}
|
||||||
|
|
||||||
|
if logFormatter != nil {
|
||||||
|
clog.SetFormatter(logFormatter)
|
||||||
|
}
|
||||||
|
clog.SetLevel(logLevel)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,83 +1,9 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logFormatter log.Formatter
|
|
||||||
var LogOutput *lumberjack.Logger //io.Writer
|
|
||||||
var logLevel log.Level
|
|
||||||
|
|
||||||
type CustomFormatter struct {
|
|
||||||
log.TextFormatter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *CustomFormatter) Format(entry *log.Entry) ([]byte, error) {
|
|
||||||
// Clear line escape sequence
|
|
||||||
clearLine := "\r\033[K"
|
|
||||||
result, err := f.TextFormatter.Format(entry)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return append([]byte(clearLine), result...), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
|
|
||||||
/*Configure logs*/
|
|
||||||
if cfgMode == "file" {
|
|
||||||
_maxsize := 500
|
|
||||||
if maxSize != 0 {
|
|
||||||
_maxsize = maxSize
|
|
||||||
}
|
|
||||||
_maxfiles := 3
|
|
||||||
if maxFiles != 0 {
|
|
||||||
_maxfiles = maxFiles
|
|
||||||
}
|
|
||||||
_maxage := 28
|
|
||||||
if maxAge != 0 {
|
|
||||||
_maxage = maxAge
|
|
||||||
}
|
|
||||||
_compress := true
|
|
||||||
if compress != nil {
|
|
||||||
_compress = *compress
|
|
||||||
}
|
|
||||||
|
|
||||||
LogOutput = &lumberjack.Logger{
|
|
||||||
Filename: filepath.Join(cfgFolder, "crowdsec.log"),
|
|
||||||
MaxSize: _maxsize,
|
|
||||||
MaxBackups: _maxfiles,
|
|
||||||
MaxAge: _maxage,
|
|
||||||
Compress: _compress,
|
|
||||||
}
|
|
||||||
log.SetOutput(LogOutput)
|
|
||||||
} else if cfgMode != "stdout" {
|
|
||||||
return fmt.Errorf("log mode '%s' unknown", cfgMode)
|
|
||||||
}
|
|
||||||
logLevel = cfgLevel
|
|
||||||
log.SetLevel(logLevel)
|
|
||||||
logFormatter = &CustomFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}}
|
|
||||||
log.SetFormatter(logFormatter)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConfigureLogger(clog *log.Logger) error {
|
|
||||||
/*Configure logs*/
|
|
||||||
if LogOutput != nil {
|
|
||||||
clog.SetOutput(LogOutput)
|
|
||||||
}
|
|
||||||
|
|
||||||
if logFormatter != nil {
|
|
||||||
clog.SetFormatter(logFormatter)
|
|
||||||
}
|
|
||||||
clog.SetLevel(logLevel)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UtcNow() time.Time {
|
func UtcNow() time.Time {
|
||||||
return time.Now().UTC()
|
return time.Now().UTC()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue