-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
106 lines (87 loc) · 2.17 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/ncarlier/apimon/pkg/config"
"github.com/ncarlier/apimon/pkg/logger"
"github.com/ncarlier/apimon/pkg/monitoring"
"github.com/ncarlier/apimon/pkg/output"
)
var (
healthy int32
op *output.Provider
)
var (
version = flag.Bool("version", false, "Print version")
help = flag.Bool("help", false, "Print this help screen")
out = flag.String("o", "", "Logging output file (default STDOUT)")
debug = flag.Bool("vv", false, "Activate debug logging level")
verbose = flag.Bool("v", false, "Activate verbose logging level")
configFile = flag.String("c", "./apimon.yml", "Configuration file (can also be provided with STDIN)")
)
func main() {
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
if *version {
printVersion()
os.Exit(0)
}
// Get logging level
level := "warn"
if *debug {
level = "debug"
} else if *verbose {
level = "info"
}
// Setup logger
if err := logger.Configure(level, *out); err != nil {
log.Panicln("unable to init the logger", err)
}
logger.Debug.Println("starting APImon...")
// Loading configuration...
c, err := config.Load(*configFile)
if err != nil {
logger.Error.Panicln("unable to load the configuration", err)
}
// Create and start output provider...
op, err = output.NewOutputProvider(c.Output)
if err != nil {
logger.Error.Panicln(err)
}
op.Start()
// Create and start monitoring...
mon := monitoring.NewMonitoring(*c)
mon.Start()
go mon.RestartOnSDConfigChange()
// Graceful shutdown
go func() {
<-quit
logger.Debug.Println("stoping APImon...")
atomic.StoreInt32(&healthy, 0)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := mon.Stop(ctx); err != nil {
logger.Error.Fatalf("could not gracefully shutdown the daemon: %v\n", err)
}
if op != nil {
op.Stop()
}
close(done)
}()
logger.Info.Println("APImon started")
atomic.StoreInt32(&healthy, 1)
<-done
logger.Debug.Println("APImon stopped")
}