forked from mesosphere-backup/dcos-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
202 lines (175 loc) · 6.18 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
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
// Copyright 2016 Mesosphere, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"github.com/dcos/dcos-go/dcos"
"github.com/dcos/dcos-go/dcos/nodeutil"
mesosAgent "github.com/dcos/dcos-metrics/collectors/mesos/agent"
"github.com/dcos/dcos-metrics/collectors/node"
httpProducer "github.com/dcos/dcos-metrics/producers/http"
httpHelpers "github.com/dcos/dcos-metrics/util/http/helpers"
log "github.com/Sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
var (
// VERSION set by $(git describe --always)
// Set by scripts/build.sh, executed by `make build`
VERSION = "unset"
// REVISION set by $(git rev-parse --shore HEAD)
// Set by scripts/build.sh, executed by `make build`
REVISION = "unset"
)
// Config defines the top-level configuration options for the dcos-metrics-collector project.
// It is (currently) broken up into two main sections: collectors and producers.
type Config struct {
// Config from the service config file
Collector CollectorConfig `yaml:"collector"`
Producers ProducersConfig `yaml:"producers"`
IAMConfigPath string `yaml:"iam_config_path"`
CACertificatePath string `yaml:"ca_certificate_path"`
// Generated by dcos.NodeInfo{}
MesosID string
IPAddress string
ClusterID string
// Flag configuration
DCOSRole string
ConfigPath string
LogLevel string
VersionFlag bool
}
// CollectorConfig contains configuration options relevant to the "collector"
// portion of this project. That is, the code responsible for querying Mesos,
// et. al to gather metrics and send them to a "producer".
type CollectorConfig struct {
HTTPProfiler bool `yaml:"http_profiler"`
Node *node.Collector `yaml:"node,omitempty"`
MesosAgent *mesosAgent.Collector `yaml:"mesos_agent,omitempty"`
}
// ProducersConfig contains references to other structs that provide individual producer configs.
// The configuration for all producers is then located in their corresponding packages.
//
// For example: Config.Producers.KafkaProducerConfig references kafkaProducer.Config. This struct
// contains an optional Kafka configuration. This configuration is available in the source file
// 'producers/kafka/kafka.go'. It is then the responsibility of the individual producers to
// validate the configuration the user has provided and panic if necessary.
type ProducersConfig struct {
HTTPProducerConfig httpProducer.Config `yaml:"http,omitempty"`
//KafkaProducerConfig kafkaProducer.Config `yaml:"kafka,omitempty"`
//StatsdProducerConfig statsdProducer.Config `yaml:"statsd,omitempty"`
}
func (c *Config) setFlags(fs *flag.FlagSet) {
fs.StringVar(&c.ConfigPath, "config", c.ConfigPath, "The path to the config file.")
fs.StringVar(&c.LogLevel, "loglevel", c.LogLevel, "Logging level (default: info). Must be one of: debug, info, warn, error, fatal, panic.")
fs.StringVar(&c.DCOSRole, "role", c.DCOSRole, "The DC/OS role this instance runs on.")
fs.BoolVar(&c.VersionFlag, "version", c.VersionFlag, "Print version and revsion then exit")
}
func (c *Config) loadConfig() error {
fileByte, err := ioutil.ReadFile(c.ConfigPath)
if err != nil {
return err
}
if err = yaml.Unmarshal(fileByte, &c); err != nil {
return err
}
return nil
}
func (c *Config) getNodeInfo() error {
log.Debug("Getting node info")
client, err := httpHelpers.NewMetricsClient(c.CACertificatePath, c.IAMConfigPath)
if err != nil {
return err
}
// Get NodeInfo
var stateURL = "http://leader.mesos:5050/state"
if len(c.IAMConfigPath) > 0 {
stateURL = "https://leader.mesos:5050/state"
}
nodeInfo, err := nodeutil.NewNodeInfo(client, nodeutil.OptionMesosStateURL(stateURL))
if err != nil {
log.Errorf("Error getting NodeInfo{}: err")
}
ip, err := nodeInfo.DetectIP()
if err != nil {
log.Error(err)
}
c.Collector.MesosAgent.NodeInfo.IPAddress = ip.String()
c.Collector.Node.NodeInfo.IPAddress = ip.String()
mid, err := nodeInfo.MesosID(nil)
if err != nil {
log.Error(err)
}
c.Collector.MesosAgent.NodeInfo.MesosID = mid
c.Collector.Node.NodeInfo.MesosID = mid
if c.DCOSRole == dcos.RoleMaster {
c.Collector.Node.NodeInfo.ClusterID, err = nodeInfo.ClusterID()
if err != nil {
return err
}
}
return nil
}
// newConfig establishes our default, base configuration.
func newConfig() Config {
return Config{
Collector: CollectorConfig{
HTTPProfiler: true,
MesosAgent: &mesosAgent.Collector{
PollPeriod: 15,
Port: 5051,
},
Node: &node.Collector{
PollPeriod: 15,
},
},
Producers: ProducersConfig{
HTTPProducerConfig: httpProducer.Config{
Port: 8000,
},
},
ConfigPath: "dcos-metrics-config.yaml",
LogLevel: "info",
}
}
// getNewConfig loads the configuration and sets precedence of configuration values.
// For example: command line flags override values provided in the config file.
func getNewConfig(args []string) (Config, error) {
c := newConfig()
thisFlagSet := flag.NewFlagSet("", flag.ExitOnError)
c.setFlags(thisFlagSet)
// Override default config with CLI flags if any
if err := thisFlagSet.Parse(args); err != nil {
fmt.Println("Errors encountered parsing flags.")
return c, err
}
if err := c.loadConfig(); err != nil {
return c, err
}
// Note: .getNodeInfo() is last so we are sure we have all the
// configuration we need from flags and config file to make
// this run correctly.
if err := c.getNodeInfo(); err != nil {
return c, err
}
// Set the client for the collector to reuse in GET operations
// to local state and other HTTP sessions
collectorClient, err := httpHelpers.NewMetricsClient(c.CACertificatePath, c.IAMConfigPath)
if err != nil {
return c, err
}
c.Collector.MesosAgent.HTTPClient = collectorClient
return c, nil
}