-
Notifications
You must be signed in to change notification settings - Fork 122
/
main.go
130 lines (115 loc) · 2.55 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
// Copyright 2015 Eleme Inc. All rights reserved.
package main
import (
"errors"
"flag"
"fmt"
"os"
"runtime"
"github.com/eleme/banshee/alerter"
"github.com/eleme/banshee/cleaner"
"github.com/eleme/banshee/config"
"github.com/eleme/banshee/detector"
"github.com/eleme/banshee/filter"
"github.com/eleme/banshee/health"
"github.com/eleme/banshee/storage"
"github.com/eleme/banshee/util/log"
"github.com/eleme/banshee/version"
"github.com/eleme/banshee/webapp"
)
var (
// Arguments
debug = flag.Bool("d", false, "debug mode")
fileName = flag.String("c", "config.json", "config file path")
showVersion = flag.Bool("v", false, "show version")
// Variables
cfg = config.New()
db *storage.DB
flt = filter.New()
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: banshee [-c config] [-d] [-v]\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "copyright eleme https://github.com/eleme/banshee.\n")
os.Exit(2)
}
func initLog() {
log.SetName("banshee")
if *debug {
log.SetLevel(log.DEBUG)
}
goVs := runtime.Version()
nCPU := runtime.GOMAXPROCS(-1)
vers := version.Version
log.Debug("banshee%s %s %d cpu", vers, goVs, nCPU)
}
func initConfig() {
// Config parsing.
if flag.NFlag() == 0 || (flag.NFlag() == 1 && *debug) {
// Case ./program [-d]
log.Warn("no config specified, using default..")
} else {
// Update config.
err := cfg.UpdateWithJSONFile(*fileName)
if err != nil {
log.Fatal("failed to load %s, %s", *fileName, err)
}
}
// Config validation.
err := cfg.Validate()
if err != nil {
if err == config.ErrAlerterCommandEmpty {
// Ignore alerter command empty.
log.Warn("config: %s", err)
} else {
log.Fatal("config: %s", err)
}
}
}
func initDB() {
// Rely on config.
if cfg == nil {
panic(errors.New("db require config"))
}
path := cfg.Storage.Path
var err error
db, err = storage.Open(path)
if err != nil {
log.Fatal("failed to open %s: %v", path, err)
}
}
func initFilter() {
// Rely on db and config.
if db == nil || cfg == nil {
panic(errors.New("filter require db and config"))
}
// Init filter
flt.Init(db)
flt.SetHitLimit(cfg)
}
func init() {
// Arguments
flag.Usage = usage
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Version)
os.Exit(1)
}
// Init
initLog()
initConfig()
initDB()
initFilter()
}
func main() {
health.Init(db)
go health.Start()
cleaner := cleaner.New(cfg, db)
go cleaner.Start()
alerter := alerter.New(cfg, db)
alerter.Start()
go webapp.Start(cfg, db, flt)
detector := detector.New(cfg, db, flt)
detector.Out(alerter.In)
detector.Start()
}