-
Notifications
You must be signed in to change notification settings - Fork 12
/
goiftop.go
225 lines (195 loc) · 6.7 KB
/
goiftop.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/fs714/goiftop/accounting"
"github.com/fs714/goiftop/api"
"github.com/fs714/goiftop/engine"
"github.com/fs714/goiftop/notify"
"github.com/fs714/goiftop/utils/config"
"github.com/fs714/goiftop/utils/log"
"github.com/fs714/goiftop/utils/version"
"github.com/google/gopacket/pcap"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func init() {
flag.StringVar(&config.IfaceListString, "i", "", "Interface name list seperated by comma for libpcap and afpacket, like eth0, eth1. This is used for libpcap and afpacket engine")
flag.StringVar(&config.GroupListString, "nflog", "", "Nflog interface, group id and direction list seperated by comma, like eth0:2:in, eth0:3:out, eth1:4:int, eth1:5:out. This is used for nflog engine")
flag.StringVar(&config.Engine, "engine", "libpcap", "Packet capture engine, could be libpcap, afpacket and nflog")
flag.BoolVar(&config.IsDecodeL4, "l4", false, "Show transport layer flows")
flag.BoolVar(&config.PrintEnable, "print.enable", false, "enable print notifier")
flag.Int64Var(&config.PrintInterval, "print.interval", 2, "Interval to print flows")
flag.BoolVar(&config.WebHookEnable, "webhook.enable", false, "enable webhook notifier")
flag.StringVar(&config.WebHookUrl, "webhook.url", "", "webhokk url")
flag.Int64Var(&config.WebHookInterval, "webhook.interval", 15, "Interval for webhook to send out flows")
flag.IntVar(&config.WebHookPostTimeout, "webhook.post_timeout", 2, "Post timeout for webhook to send out flows")
flag.StringVar(&config.WebHookNodeId, "webhook.node_id", "", "Node identification for webhook")
flag.StringVar(&config.WebHookNodeOamAddr, "webhook.node_oam_addr", "", "node oam address for webhook")
flag.BoolVar(&config.IsEnableHttpSrv, "http", false, "Enable http server and ui")
flag.StringVar(&config.HttpSrvAddr, "addr", "0.0.0.0", "Http server listening address")
flag.StringVar(&config.HttpSrvPort, "port", "31415", "Http server listening port")
flag.BoolVar(&config.IsProfiling, "profiling", false, "Enable profiling by http")
flag.BoolVar(&config.IsShowVersion, "v", false, "Show version")
flag.Parse()
err := log.SetLevel("info")
if err != nil {
fmt.Println("failed to set log level")
os.Exit(1)
}
err = log.SetFormat("text")
if err != nil {
fmt.Println("failed to set log format")
os.Exit(1)
}
log.SetOutput(os.Stdout)
}
func ArgsValidation() (err error) {
if config.Engine != engine.LibPcapEngineName && config.Engine != engine.AfpacketEngineName &&
config.Engine != engine.NflogEngineName {
err = errors.New("invalid engine name: " + config.Engine)
return
}
if config.Engine == engine.LibPcapEngineName || config.Engine == engine.AfpacketEngineName {
if config.IfaceListString == "" {
err = errors.New("no interface provided")
return
}
}
if config.Engine == engine.NflogEngineName {
if config.GroupListString == "" {
err = errors.New("no group id provided")
return
}
}
return
}
func main() {
if config.IsShowVersion {
fmt.Println(version.Version)
os.Exit(0)
}
if os.Geteuid() != 0 {
log.Errorln("must run as root")
os.Exit(1)
}
err := ArgsValidation()
if err != nil {
log.Errorf("args validation failed with err: %s", err.Error())
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
ExitWG := &sync.WaitGroup{}
if config.Engine == engine.NflogEngineName {
err = config.ParseNflogConfig()
if err != nil {
log.Errorln(err.Error())
os.Exit(1)
}
} else {
config.ParseIfaces()
}
accounting.GlobalAcct = accounting.NewAccounting()
accounting.GlobalAcct.SetRetention(300)
for _, iface := range config.IfaceList {
accounting.GlobalAcct.AddInterface(iface)
}
ExitWG.Add(1)
go func(ctx context.Context) {
defer ExitWG.Done()
accounting.GlobalAcct.Start(ctx)
}(ctx)
var engineList []engine.PktCapEngine
if config.Engine == engine.LibPcapEngineName {
for _, iface := range config.IfaceList {
eIn := engine.NewLibPcapEngine(iface, "", pcap.DirectionIn, 65535, config.IsDecodeL4, accounting.GlobalAcct.Ch)
eOut := engine.NewLibPcapEngine(iface, "", pcap.DirectionOut, 65535, config.IsDecodeL4, accounting.GlobalAcct.Ch)
engineList = append(engineList, eIn)
engineList = append(engineList, eOut)
}
} else if config.Engine == engine.AfpacketEngineName {
for _, iface := range config.IfaceList {
eIn := engine.NewAfpacketEngine(iface, pcap.DirectionIn, config.IsDecodeL4, accounting.GlobalAcct.Ch)
eOut := engine.NewAfpacketEngine(iface, pcap.DirectionOut, config.IsDecodeL4, accounting.GlobalAcct.Ch)
engineList = append(engineList, eIn)
engineList = append(engineList, eOut)
}
} else if config.Engine == engine.NflogEngineName {
for _, nflogConf := range config.NflogConfigList {
e := engine.NewNflogEngine(nflogConf.IfaceName, nflogConf.GroupId, nflogConf.Direction, config.IsDecodeL4, accounting.GlobalAcct.Ch)
engineList = append(engineList, e)
}
} else {
err = errors.New("invalid engine name: " + config.Engine)
log.Errorln(err.Error())
os.Exit(1)
}
for _, e := range engineList {
go func(e engine.PktCapEngine) {
err = e.StartEngine()
if err != nil {
log.Errorf("failed to start engine with err: %s", err.Error())
os.Exit(1)
}
}(e)
}
if config.IsEnableHttpSrv || config.IsProfiling {
router := api.InitRouter()
srv := &http.Server{
Addr: config.HttpSrvAddr + ":" + config.HttpSrvPort,
Handler: router,
ReadTimeout: 300 * time.Second,
WriteTimeout: 300 * time.Second,
MaxHeaderBytes: 1 << 20,
}
ExitWG.Add(1)
go func() {
defer ExitWG.Done()
log.Infof("start http server on %s:%s", config.HttpSrvAddr, config.HttpSrvPort)
_ = srv.ListenAndServe()
}()
ExitWG.Add(1)
go func(ctx context.Context) {
defer ExitWG.Done()
select {
case <-ctx.Done():
cctx, ccancel := context.WithTimeout(context.Background(), 1*time.Second)
defer ccancel()
err := srv.Shutdown(cctx)
if err != nil {
log.Errorf("failed to close http server with err: %s", err.Error())
}
log.Infoln("http server exit")
}
}(ctx)
}
if config.PrintEnable {
time.Sleep(1 * time.Second)
ExitWG.Add(1)
go func(ctx context.Context) {
defer ExitWG.Done()
notify.PrintNotifier(ctx, config.PrintInterval)
}(ctx)
}
if config.WebHookEnable {
time.Sleep(1 * time.Second)
ExitWG.Add(1)
go func(ctx context.Context) {
defer ExitWG.Done()
notify.WebhookNotifier(ctx, config.WebHookInterval, config.WebHookNodeId, config.WebHookNodeOamAddr,
config.WebHookUrl, config.WebHookPostTimeout)
}(ctx)
}
<-signalCh
cancel()
ExitWG.Wait()
log.Infoln("goiftop exit")
}