-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
165 lines (139 loc) · 5.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// The flowpipeline utility unifies all bwNetFlow functionality and
// provides configurable pipelines to process flows in any manner.
//
// The main entrypoint accepts command line flags to point to a configuration
// file and to establish the log level.
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"plugin"
"strings"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/BelWue/flowpipeline/pipeline"
_ "github.com/BelWue/flowpipeline/segments/alert/http"
_ "github.com/BelWue/flowpipeline/segments/controlflow/branch"
_ "github.com/BelWue/flowpipeline/segments/export/clickhouse"
_ "github.com/BelWue/flowpipeline/segments/export/influx"
_ "github.com/BelWue/flowpipeline/segments/export/prometheus"
_ "github.com/BelWue/flowpipeline/segments/filter/drop"
_ "github.com/BelWue/flowpipeline/segments/filter/elephant"
_ "github.com/BelWue/flowpipeline/segments/filter/flowfilter"
_ "github.com/BelWue/flowpipeline/segments/input/bpf"
_ "github.com/BelWue/flowpipeline/segments/input/diskbuffer"
_ "github.com/BelWue/flowpipeline/segments/input/goflow"
_ "github.com/BelWue/flowpipeline/segments/input/kafkaconsumer"
_ "github.com/BelWue/flowpipeline/segments/input/packet"
_ "github.com/BelWue/flowpipeline/segments/input/stdin"
_ "github.com/BelWue/flowpipeline/segments/modify/addcid"
_ "github.com/BelWue/flowpipeline/segments/modify/addrstrings"
_ "github.com/BelWue/flowpipeline/segments/modify/anonymize"
_ "github.com/BelWue/flowpipeline/segments/modify/aslookup"
_ "github.com/BelWue/flowpipeline/segments/modify/bgp"
_ "github.com/BelWue/flowpipeline/segments/modify/dropfields"
_ "github.com/BelWue/flowpipeline/segments/modify/geolocation"
_ "github.com/BelWue/flowpipeline/segments/modify/normalize"
_ "github.com/BelWue/flowpipeline/segments/modify/protomap"
_ "github.com/BelWue/flowpipeline/segments/modify/remoteaddress"
_ "github.com/BelWue/flowpipeline/segments/modify/reversedns"
_ "github.com/BelWue/flowpipeline/segments/modify/snmp"
_ "github.com/BelWue/flowpipeline/segments/pass"
_ "github.com/BelWue/flowpipeline/segments/output/csv"
_ "github.com/BelWue/flowpipeline/segments/output/json"
_ "github.com/BelWue/flowpipeline/segments/output/kafkaproducer"
_ "github.com/BelWue/flowpipeline/segments/output/lumberjack"
_ "github.com/BelWue/flowpipeline/segments/output/mongodb"
_ "github.com/BelWue/flowpipeline/segments/output/sqlite"
_ "github.com/BelWue/flowpipeline/segments/print/count"
_ "github.com/BelWue/flowpipeline/segments/print/printdots"
_ "github.com/BelWue/flowpipeline/segments/print/printflowdump"
_ "github.com/BelWue/flowpipeline/segments/print/toptalkers"
_ "github.com/BelWue/flowpipeline/segments/analysis/toptalkers_metrics"
)
var Version string
type flagArray []string
func (i *flagArray) String() string {
return strings.Join(*i, ",")
}
func (i *flagArray) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
var pluginPaths flagArray
flag.Var(&pluginPaths, "p", "path to load segment plugins from, can be specified multiple times")
logLevel := flag.String("l", "info", "loglevel: one of 'trace', 'debug', 'info', 'warning', 'error', 'fatal', or 'panic'")
version := flag.Bool("v", false, "print version")
prettyLogging := flag.Bool("j", false, "Json log")
configFile := flag.String("c", "config.yml", "location of the config file in yml format")
flag.Parse()
if *version {
fmt.Println(Version)
return
}
if !*prettyLogging {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime})
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerologLogLevel(logLevel))
for _, path := range pluginPaths {
_, err := plugin.Open(path)
if err != nil {
if err.Error() == "plugin: not implemented" {
log.Error().Msg("Loading plugins is unsupported when running a static, not CGO-enabled binary.")
} else {
log.Error().Err(err).Msgf("Problem loading the specified plugin '%s'", path)
}
return
} else {
log.Info().Msgf("Loaded plugin: %s", path)
}
}
config, err := os.ReadFile(*configFile)
if err != nil {
log.Error().Err(err).Msg(" reading config file: ")
return
}
pipe := pipeline.NewFromConfig(config)
pipe.Start()
pipe.AutoDrain()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT)
signal.Notify(sigs, os.Interrupt, os.Interrupt)
<-sigs
log.Info().Msg("Received exit signal")
pipe.Close()
}
func zerologLogLevel(logLevel *string) zerolog.Level {
if logLevel != nil && *logLevel != "" {
switch *logLevel {
case "trace":
log.Info().Msg("Using log level 'trace'")
return zerolog.TraceLevel
case "debug":
log.Info().Msg("Using log level 'debug'")
return zerolog.DebugLevel
case "info":
log.Info().Msg("Using log level 'info'")
return zerolog.InfoLevel
case "warning":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
case "fatal":
return zerolog.FatalLevel
case "panic":
return zerolog.PanicLevel
default:
log.Warn().Msgf("Unknown log level '%s' using default 'info'", *logLevel)
}
} else {
log.Info().Msg("Using default log level 'info'")
}
return zerolog.InfoLevel
}