-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
128 lines (104 loc) · 4.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
// 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"
"log"
"os"
"os/signal"
"plugin"
"strings"
"github.com/bwNetFlow/flowpipeline/pipeline"
"github.com/hashicorp/logutils"
_ "github.com/bwNetFlow/flowpipeline/segments/alert/http"
_ "github.com/bwNetFlow/flowpipeline/segments/controlflow/branch"
_ "github.com/bwNetFlow/flowpipeline/segments/export/clickhouse"
_ "github.com/bwNetFlow/flowpipeline/segments/export/influx"
_ "github.com/bwNetFlow/flowpipeline/segments/export/prometheus"
_ "github.com/bwNetFlow/flowpipeline/segments/filter/drop"
_ "github.com/bwNetFlow/flowpipeline/segments/filter/elephant"
_ "github.com/bwNetFlow/flowpipeline/segments/filter/flowfilter"
_ "github.com/bwNetFlow/flowpipeline/segments/input/bpf"
_ "github.com/bwNetFlow/flowpipeline/segments/input/goflow"
_ "github.com/bwNetFlow/flowpipeline/segments/input/kafkaconsumer"
_ "github.com/bwNetFlow/flowpipeline/segments/input/packet"
_ "github.com/bwNetFlow/flowpipeline/segments/input/stdin"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/addcid"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/addrstrings"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/anonymize"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/aslookup"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/bgp"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/dropfields"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/geolocation"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/normalize"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/protomap"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/remoteaddress"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/reversedns"
_ "github.com/bwNetFlow/flowpipeline/segments/modify/snmp"
_ "github.com/bwNetFlow/flowpipeline/segments/pass"
_ "github.com/bwNetFlow/flowpipeline/segments/output/csv"
_ "github.com/bwNetFlow/flowpipeline/segments/output/json"
_ "github.com/bwNetFlow/flowpipeline/segments/output/kafkaproducer"
_ "github.com/bwNetFlow/flowpipeline/segments/output/lumberjack"
_ "github.com/bwNetFlow/flowpipeline/segments/output/sqlite"
_ "github.com/bwNetFlow/flowpipeline/segments/print/count"
_ "github.com/bwNetFlow/flowpipeline/segments/print/printdots"
_ "github.com/bwNetFlow/flowpipeline/segments/print/printflowdump"
_ "github.com/bwNetFlow/flowpipeline/segments/print/toptalkers"
_ "github.com/bwNetFlow/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", "warning", "loglevel: one of 'debug', 'info', 'warning' or 'error'")
version := flag.Bool("v", false, "print version")
configfile := flag.String("c", "config.yml", "location of the config file in yml format")
flag.Parse()
if *version {
fmt.Println(Version)
return
}
log.SetOutput(&logutils.LevelFilter{
Levels: []logutils.LogLevel{"debug", "info", "warning", "error"},
MinLevel: logutils.LogLevel(*loglevel),
Writer: os.Stderr,
})
for _, path := range pluginPaths {
_, err := plugin.Open(path)
if err != nil {
if err.Error() == "plugin: not implemented" {
log.Println("[error] Loading plugins is unsupported when running a static, not CGO-enabled binary.")
} else {
log.Printf("[error] Problem loading the specified plugin: %s", err)
}
return
} else {
log.Printf("[info] Loaded plugin: %s", path)
}
}
config, err := os.ReadFile(*configfile)
if err != nil {
log.Printf("[error] reading config file: %s", err)
return
}
pipe := pipeline.NewFromConfig(config)
pipe.Start()
pipe.AutoDrain()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
<-sigs
pipe.Close()
}