/
config.go
173 lines (154 loc) · 3.15 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package config
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/BurntSushi/toml"
"github.com/op/go-logging"
)
type Config struct {
General ConfigGeneral
Mail ConfigSmtp
Mailgun ConfigMailgun
Slack ConfigSlack
Udp ConfigBind
Tcp ConfigBind
Http ConfigBind
Database ConfigDatabase
Metrics ConfigMetrics
Alarms []ConfigAlarm
Alerts map[string]ConfigAlert
Eventlog ConfigEventlog
Notify []ConfigNotify
}
type ConfigGeneral struct {
PublicUrl string `toml:"public_url"`
}
type ConfigSmtp struct {
From string
Server string
}
type ConfigMailgun struct {
From string
Domain string
ApiKey string `toml:"api_key"`
}
type ConfigSlack struct {
WebhookUrl string `toml:"webhook_url"`
}
type ConfigBind struct {
Listen string
}
type ConfigDatabase struct {
Filename string
Interval int
RemoteS3Url string `toml:"remote_s3_url"`
RemoteS3Region string `toml:"remote_s3_region"`
}
type ConfigMetrics struct {
Server string
Prefix string
}
type ConfigAlert struct {
Mail string
Webhook string
SlackChannel string `toml:"slack_channel"`
Script string
}
type ConfigAlarm struct {
Name string
Pattern string
Includes []string
Excludes []string
Alerts []string
}
type ConfigEventlog struct {
Path string
Mode os.FileMode
}
type ConfigNotify struct {
Lovebeat string
}
var log = logging.MustGetLogger("lovebeat")
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func readFile(conf *Config, fname string) error {
if e, _ := exists(fname); e {
log.Infof("Reading configuration file %s", fname)
if _, err := toml.DecodeFile(fname, conf); err != nil {
log.Errorf("Failed to parse configuration file '%s': %v", fname, err)
return err
}
}
return nil
}
func ReadConfig(fname string, dirname string) (conf Config, err error) {
conf = Config{
General: ConfigGeneral{
PublicUrl: "http://lovebeat.example.com/",
},
Mail: ConfigSmtp{
From: "lovebeat@example.com",
Server: "localhost:25",
},
Mailgun: ConfigMailgun{},
Slack: ConfigSlack{
WebhookUrl: "https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXX",
},
Udp: ConfigBind{
Listen: ":8127",
},
Tcp: ConfigBind{
Listen: ":8127",
},
Http: ConfigBind{
Listen: ":8080",
},
Database: ConfigDatabase{
Filename: "lovebeat.db",
Interval: 60,
},
Metrics: ConfigMetrics{
Server: "",
Prefix: "lovebeat",
},
Eventlog: ConfigEventlog{
Path: "",
Mode: 644, // Reinterpreted as octal below
},
}
err = readFile(&conf, fname)
if err != nil {
return
}
if dirname != "" {
var files []os.FileInfo
files, err = ioutil.ReadDir(dirname)
if err == nil {
for _, f := range files {
if f.Name()[0] != '.' {
path := filepath.Join(dirname, f.Name())
err = readFile(&conf, path)
if err != nil {
return
}
}
}
} else {
// This directory is optional
err = nil
}
}
mode, _ := strconv.ParseInt(strconv.FormatInt(int64(conf.Eventlog.Mode), 10), 8, 64)
conf.Eventlog.Mode = os.FileMode(mode)
return
}