-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (81 loc) · 1.86 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
package main
import (
"fmt"
"math/rand"
"os"
"runtime"
"runtime/debug"
"syscall"
"time"
"github.com/nicholaskh/etclib"
"github.com/nicholaskh/golib/locking"
"github.com/nicholaskh/golib/server"
"github.com/nicholaskh/golib/signal"
log "github.com/nicholaskh/log4go"
)
var (
GcollectorConf *GcollectorConfig
)
func init() {
parseFlags()
if options.concurrency != 0 {
runtime.GOMAXPROCS(options.concurrency)
}
if options.showVersion {
server.ShowVersionAndExit()
}
conf := server.LoadConfig(options.configFile)
GcollectorConf = new(GcollectorConfig)
GcollectorConf.LoadConfig(conf)
if options.kill {
if err := server.KillProcess(options.lockFile); err != nil {
fmt.Fprintf(os.Stderr, "stop failed: %s\n", err)
os.Exit(1)
}
etclib.Dial(GcollectorConf.EtcServers)
loadLocalAddr()
UnregisterEtc()
os.Exit(0)
}
server.SetupLogging(options.logFile, options.logLevel, options.crashLogFile)
if options.lockFile != "" {
if locking.InstanceLocked(options.lockFile) {
fmt.Fprintf(os.Stderr, "Another gcollector is running, exit...\n")
os.Exit(1)
}
locking.LockInstance(options.lockFile)
}
signal.RegisterSignalHandler(syscall.SIGINT, func(sig os.Signal) {
shutdown()
})
err := RegisterEtc(GcollectorConf.EtcServers)
if err != nil {
panic(err)
}
GcollectorConf.LoadForwarder(conf)
rand.Seed(time.Now().UnixNano())
}
func main() {
defer func() {
cleanup()
if err := recover(); err != nil {
fmt.Println(err)
debug.PrintStack()
}
}()
go server.RunSysStats(time.Now(), time.Duration(options.tick)*time.Second)
gcollector := NewGcollector(GcollectorConf)
gcollector.RunForever()
}
func shutdown() {
cleanup()
log.Info("Terminated")
os.Exit(0)
}
func cleanup() {
if options.lockFile != "" {
locking.UnlockInstance(options.lockFile)
log.Debug("Cleanup lock %s", options.lockFile)
}
UnregisterEtc()
}