forked from zr-hebo/sniffer-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.35 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
package main
import (
"flag"
"fmt"
sd "github.com/zr-hebo/sniffer-agent/session-dealer"
"github.com/zr-hebo/sniffer-agent/session-dealer/mysql"
"os"
log "github.com/sirupsen/logrus"
"github.com/zr-hebo/sniffer-agent/capture"
"github.com/zr-hebo/sniffer-agent/communicator"
"github.com/zr-hebo/sniffer-agent/exporter"
)
var (
logLevel string
)
func init() {
flag.StringVar(&logLevel, "log_level", "warn", "log level. Default is info")
}
func initLog() {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
panic(fmt.Sprintf("cannot set log level:%s, there have four types can set: debug, info, warn, error", logLevel))
}
}
func main() {
flag.Parse()
prepareEnv()
go communicator.Server()
mainServer()
}
func mainServer() {
ept := exporter.NewExporter()
networkCard := capture.NewNetworkCard()
log.Info("begin listen")
for queryPiece := range networkCard.Listen() {
err := ept.Export(queryPiece)
if err != nil {
log.Error(err.Error())
}
queryPiece.Recovery()
}
log.Errorf("cannot get network package from %s", capture.DeviceName)
os.Exit(1)
}
func prepareEnv() {
initLog()
sd.CheckParams()
mysql.PrepareEnv()
}