-
Notifications
You must be signed in to change notification settings - Fork 1
/
configuration.go
204 lines (156 loc) · 6.31 KB
/
configuration.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cadastre
import "fmt"
import "time"
import "github.com/kylelemons/go-gypsy/yaml"
// Provides the basic information needed to run Cadastre: what servers to check,
// how often to check them, and where to put the data.
type Configuration struct {
// The list of MySQL hosts to check. A host is defined by a unique, internal name,
// a display name (something more suitable than the internal name or hostname), an optional
// category to allow for logical UI grouping and a DSN used to figure out how to connect to
// the MySQL host.
Servers []Server
// A list of MySQL hosts to check, grouped by category. This list contains the same servers
// as the "Servers" list.
ServerGroups []ServerGroup
// The interval to check each server.
FetchInterval time.Duration
// The storage engine used to persist the host information to.
Storage DataStore
// The address for the web server to listen on.
ListenAddress string
// Where to load templates from.
TemplateDirectory string
// Where to serve static assets from.
StaticAssetDirectory string
// Whether or not we're in debug mode.
DebugMode bool
}
func LoadConfigurationFromFile(configurationFile string) (*Configuration, error) {
config := &Configuration{}
// Try to load the file we got passed to us.
yamlConfig, err := yaml.ReadFile(configurationFile)
if err != nil {
return nil, fmt.Errorf("Caught an error while trying to load the configuration! %s", err)
}
// Get the address that the web server will listen on.
listenAddress, err := yamlConfig.Get("listenAddress")
if err != nil || listenAddress == "" {
return nil, fmt.Errorf("Listen address must be specified!")
}
config.ListenAddress = listenAddress
// Get the fetch interval.
fetchInterval, err := yamlConfig.Get("fetchInterval")
if err != nil {
return nil, fmt.Errorf("You must specify a fetch interval!")
}
parsedFetchInterval, err := time.ParseDuration(fetchInterval)
if err != nil {
return nil, fmt.Errorf("Fetch interval must be a valid duration string! i.e. 15s, 60m, 1h, 24h")
}
if int64(parsedFetchInterval) < 0 {
return nil, fmt.Errorf("The specified fetch interval must be non-zero!")
}
config.FetchInterval = parsedFetchInterval
// Get the template directory and the static asset directory.
templateDirectory, err := yamlConfig.Get("templateDirectory")
if err != nil || templateDirectory == "" {
return nil, fmt.Errorf("Template directory must be specified!")
}
config.TemplateDirectory = templateDirectory
staticAssetDirectory, err := yamlConfig.Get("staticAssetDirectory")
if err != nil || staticAssetDirectory == "" {
return nil, fmt.Errorf("Static asset directory must be specified!")
}
config.StaticAssetDirectory = staticAssetDirectory
// Set up the specified storage engine.
storageEngine, err := yamlConfig.Get("storageEngine.name")
if err != nil {
return nil, fmt.Errorf("Storage engine must be specified!")
}
switch storageEngine {
case "file":
fileStore := &FileStore{}
dataDirectory, err := yamlConfig.Get("storageEngine.dataDirectory")
if err != nil {
return nil, fmt.Errorf("You must specify a data directory when using the file storage engine!")
}
fileStore.DataDirectory = dataDirectory
retentionPeriod, err := yamlConfig.Get("storageEngine.retentionPeriod")
if err != nil {
return nil, fmt.Errorf("You must specify a retention period when using the file storage engine!")
}
parsedRetentionPeriod, err := time.ParseDuration(retentionPeriod)
if err != nil {
return nil, fmt.Errorf("Retention period must be a valid duration string! i.e. 15s, 60m, 1h, 24h")
}
if int64(parsedRetentionPeriod) < 0 {
return nil, fmt.Errorf("The specified retention period must be non-zero!")
}
fileStore.RetentionPeriod = parsedRetentionPeriod
config.Storage = fileStore
default:
return nil, fmt.Errorf("'%s' is not a recognized storage engine!", storageEngine)
}
// Now parse out the servers we want to monitor.
serverCount, err := yamlConfig.Count("servers")
if err != nil || serverCount < 1 {
return nil, fmt.Errorf("You must specify MySQL servers to monitor!")
}
servers := []Server{}
for i := 0; i < serverCount; i++ {
// Get the internal name for this server.
internalName, err := yamlConfig.Get(fmt.Sprintf("servers[%d].internalName", i))
if err != nil {
return nil, fmt.Errorf("You must specify an internal name for all monitored servers!")
}
// Get the display name for this server.
displayName, err := yamlConfig.Get(fmt.Sprintf("servers[%d].displayName", i))
if err != nil {
return nil, fmt.Errorf("You must specify a display name for all monitored servers!")
}
// Get the group name for this server.
groupName, _ := yamlConfig.Get(fmt.Sprintf("servers[%d].groupName", i))
// Get the DSN for this server.
dataSourceName, err := yamlConfig.Get(fmt.Sprintf("servers[%d].dataSourceName", i))
if err != nil {
return nil, fmt.Errorf("You must specify a data source name for all monitored servers!")
}
// Make sure the internal name and display name are unique.
for _, existingServer := range servers {
if existingServer.InternalName == internalName {
return nil, fmt.Errorf("All monitored servers must have a unique, internal name! Duplicate internal name: %s", internalName)
}
if existingServer.DisplayName == displayName {
return nil, fmt.Errorf("All monitored servers must have a unique, display name! Duplicate display name: %s", displayName)
}
}
server := Server{
InternalName: internalName,
DisplayName: displayName,
GroupName: groupName,
DataSourceName: dataSourceName,
}
servers = append(servers, server)
}
config.Servers = servers
// Now build our list of server groups.
serverGroups := make(map[string]*ServerGroup, 0)
for _, s := range servers {
serverGroup, ok := serverGroups[s.GroupName]
if !ok {
serverGroup = &ServerGroup{GroupName: s.GroupName, Servers: []Server{}}
serverGroups[s.GroupName] = serverGroup
}
serverGroup.Servers = append(serverGroup.Servers, s)
}
serverGroupsFlat := make([]ServerGroup, 0)
for _, sg := range serverGroups {
serverGroupsFlat = append(serverGroupsFlat, *sg)
}
config.ServerGroups = serverGroupsFlat
// Now that we're done parsing the configuration, make sure the configuration is ready to
// go and initialize anything we need to initialize, etc.
config.Storage.Initialize()
return config, nil
}