forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (126 loc) · 2.87 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
// Copyright 2015 Eleme Inc. All rights reserved.
// Banshee is a real-time anomalies or outliers detection system for periodic
// metrics.
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/eleme/banshee/alerter"
"github.com/eleme/banshee/alerter/notifier"
"github.com/eleme/banshee/algorithm"
"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.yaml", "config file path")
showVersion = flag.Bool("v", false, "show version")
// Variables
cfg = config.New()
db *storage.DB
flt *filter.Filter
)
// usage prints command line usage to stderr.
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " ./banshee -c filename [-d]\n")
fmt.Fprintf(os.Stderr, " ./banshee -v\n")
fmt.Fprintf(os.Stderr, "%s@%s %s\n", version.Product, version.Version, version.Website)
os.Exit(2)
}
// initLog initializes logging.
func initLog() {
if *debug {
log.SetLevel(log.DEBUG)
}
}
func initConfig() {
// Config parsing.
if flag.NFlag() == 0 || (flag.NFlag() == 1 && *debug) { // Case ./program [-d]
log.Warnf("no config specified, using default..")
} else { // Case ./program -c filename
err := cfg.UpdateWithYamlFile(*fileName)
if err != nil {
log.Fatalf("failed to load %s, %s", *fileName, err)
}
}
// Config validation.
err := cfg.Validate()
if err != nil {
log.Fatalf("config: %s", err)
}
}
func initDB() {
// Rely on config.
if cfg == nil {
panic(errors.New("db require config"))
}
path := cfg.Storage.Path
opts := &storage.Options{
Period: cfg.Period,
Expiration: cfg.Expiration,
}
var err error
db, err = storage.Open(path, opts)
if err != nil {
log.Fatalf("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 = filter.New(cfg)
flt.Init(db)
}
func initAlgo() {
// Rely on config.
if cfg == nil {
panic(errors.New("filter require db and config"))
}
algo.Init(cfg)
}
func initNotifier() {
// Rely on config.
if cfg == nil {
panic(errors.New("filter require db and config"))
}
notifier.Init(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()
initAlgo()
initNotifier()
}
func main() {
health.Init(db)
go health.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()
}