-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
138 lines (117 loc) · 3.32 KB
/
config.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
package main
import (
"github.com/Unknwon/goconfig"
"log"
"net"
"strconv"
"time"
)
const (
config_file = "/etc/tunnelmonitor/config.ini"
config_tunnel_file = "/etc/tunnelmonitor/tunnel.ini"
)
type level int
const (
FATAL level = iota
ERROR
WARMING
INFO
)
var (
cfg *goconfig.ConfigFile
cfg_tunnel *goconfig.ConfigFile
determineTime int64
detectingDuration int64
rtt int64
// logLevel int
)
func initConfig() {
var err error
cfg, err = goconfig.LoadConfigFile(config_file)
if err != nil {
log.Fatalf("CANNOT load config file(%s) : %s\n", config_file, err)
}
cfg_tunnel, err = goconfig.LoadConfigFile(config_tunnel_file)
if err != nil {
log.Fatalf("CANNOT load config file(%s) : %s\n", config_file, err)
}
}
func readConfig() {
//chainName = configCommon("chainName", FATAL)
determineTimeIntTmp, err := strconv.Atoi(configCommon("determineTime", INFO))
determineTime = int64(determineTimeIntTmp)
if err != nil {
determineTime = 10
}
detectingDurationIntTmp, err := strconv.Atoi(configCommon("detectingDuration", INFO))
detectingDuration = int64(detectingDurationIntTmp)
if err != nil {
detectingDuration = 10
}
rttIntTmp, err := strconv.Atoi(configCommon("rtt", INFO))
rtt = int64(rttIntTmp)
if err != nil {
rtt = 1
}
// switch configCommon("logLevel", WARMING) {
// case "FINEST":
// logLevel = log4go.FINEST
// case "FINE":
// logLevel = log4go.FINE
// case "DEBUG":
// logLevel = log4go.DEBUG
// case "TRACE":
// logLevel = log4go.TRACE
// case "INFO":
// logLevel = log4go.INFO
// case "WARMING":
// logLevel = log4go.WARNING
// case "ERROR":
// logLevel = log4go.ERROR
// case "CRITICAL":
// logLevel = log4go.CRITICAL
// default:
// logLevel = log4go.INFO
// }
for _, tunnelName := range cfg_tunnel.GetSectionList() {
tunnels[tunnelName] = &TunnelInfo{}
ipAddr, err := net.ResolveIPAddr("ip4", configTunnel(tunnelName, "peerIP", FATAL))
if err != nil {
logger.Critical("peerIP format error for %s: %s ", tunnelName, err.Error())
}
tunnels[tunnelName].Ip = ipAddr.String()
tunnels[tunnelName].Weight, err = strconv.Atoi(configTunnel(tunnelName, "weight", INFO))
if err != nil {
tunnels[tunnelName].Weight = 1
}
tunnels[tunnelName].Mark = configTunnel(tunnelName, "mark", FATAL)
tunnels[tunnelName].ChainName = configTunnel(tunnelName, "chainName", FATAL)
tunnels[tunnelName].RecoverCommand = configTunnel(tunnelName, "recoverCommand", INFO)
tunnels[tunnelName].DownCommand = configTunnel(tunnelName, "downCommand", INFO)
tunnels[tunnelName].lastlive, _ = time.Parse(time.ANSIC, time.ANSIC)
}
}
func config(cfg *goconfig.ConfigFile, section string, key string, lvl level) string {
value, err := cfg.GetValue(section, key)
if err != nil {
switch lvl {
case FATAL:
logger.Critical("Can't Read config %s", err.Error())
panic(err.Error())
case ERROR:
logger.Error("Can't Read config %s", err.Error())
case WARMING:
logger.Warn("Can't Read config %s", err.Error())
case INFO:
logger.Info("Can't Read config %s", err.Error())
}
return ""
}
return value
}
func configCommon(key string, lvl level) string {
return config(cfg, goconfig.DEFAULT_SECTION, key, lvl)
}
func configTunnel(tunnelName string, key string, lvl level) string {
return config(cfg_tunnel, tunnelName, key, lvl)
}