|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "fmt"
|
6 |
| - "io/ioutil" |
7 | 5 | "net/http"
|
8 | 6 |
|
9 | 7 | "github.com/hooklift/gowsdl/soap"
|
10 | 8 | "github.com/prometheus/client_golang/prometheus"
|
11 | 9 | "github.com/prometheus/client_golang/prometheus/promhttp"
|
12 | 10 | log "github.com/sirupsen/logrus"
|
13 | 11 | flag "github.com/spf13/pflag"
|
14 |
| - config "github.com/spf13/viper" |
15 | 12 |
|
16 | 13 | "github.com/SUSE/sap_host_exporter/collector/alert"
|
17 | 14 | "github.com/SUSE/sap_host_exporter/collector/dispatcher"
|
18 | 15 | "github.com/SUSE/sap_host_exporter/collector/enqueue_server"
|
19 | 16 | "github.com/SUSE/sap_host_exporter/collector/start_service"
|
20 | 17 | "github.com/SUSE/sap_host_exporter/internal"
|
| 18 | + "github.com/SUSE/sap_host_exporter/internal/config" |
21 | 19 | "github.com/SUSE/sap_host_exporter/internal/sapcontrol"
|
22 | 20 | )
|
23 | 21 |
|
24 | 22 | func init() {
|
25 |
| - config.SetConfigName("sap_host_exporter") |
26 |
| - config.AddConfigPath("./") |
27 |
| - config.AddConfigPath("$HOME/.config/") |
28 |
| - config.AddConfigPath("/etc/") |
29 |
| - config.AddConfigPath("/usr/etc/") |
30 |
| - |
31 | 23 | flag.String("port", "9680", "The port number to listen on for HTTP requests")
|
32 | 24 | flag.String("address", "0.0.0.0", "The address to listen on for HTTP requests")
|
33 | 25 | flag.String("log-level", "info", "The minimum logging level; levels are, in ascending order: debug, info, warn, error")
|
34 |
| - flag.String("sap-control-url", "", "The URL of the SAPControl SOAP web service, e.g. http://$HOST:$PORT") |
35 |
| - flag.String("config", "", "The path where a custom configuration.yaml file is located. NOTE: the conf must be yaml") |
36 |
| - |
37 |
| - err := config.BindPFlags(flag.CommandLine) |
38 |
| - if err != nil { |
39 |
| - log.Errorf("Could not bind config to CLI flags: %v", err) |
40 |
| - } |
| 26 | + flag.String("sap-control-url", "localhost:50013", "The URL of the SAPControl SOAP web service, e.g. $HOST:$PORT") |
| 27 | + flag.StringP("config", "c", "", "The path to a custom configuration file. NOTE: it must be in yaml format.") |
41 | 28 | }
|
42 | 29 |
|
43 | 30 | func main() {
|
44 |
| - initConfig() |
45 |
| - |
46 | 31 | var err error
|
47 | 32 |
|
| 33 | + flag.Parse() |
| 34 | + |
| 35 | + config, err := config.New() |
| 36 | + if err != nil { |
| 37 | + log.Fatalf("Could not initialize config: %s", err) |
| 38 | + } |
| 39 | + |
48 | 40 | client := soap.NewClient(
|
49 | 41 | config.GetString("sap-control-url"),
|
50 | 42 | soap.WithBasicAuth(
|
@@ -99,49 +91,3 @@ func main() {
|
99 | 91 | log.Infof("Serving metrics on %s", fullListenAddress)
|
100 | 92 | log.Fatal(http.ListenAndServe(fullListenAddress, nil))
|
101 | 93 | }
|
102 |
| - |
103 |
| -func initConfig() { |
104 |
| - |
105 |
| - flag.Parse() |
106 |
| - |
107 |
| - // read configuration from custom path or defaults |
108 |
| - readExporterConf() |
109 |
| - |
110 |
| - internal.SetLogLevel(config.GetString("log-level")) |
111 |
| - |
112 |
| - if config.GetString("sap-control-url") == "" { |
113 |
| - log.Fatal("sap-control-url cannot be empty, please use the --sap-control-url flag or set a value in the config") |
114 |
| - } |
115 |
| -} |
116 |
| - |
117 |
| -func readExporterConf() { |
118 |
| - |
119 |
| - // read first the configuration from custom file. If not provided, read default. |
120 |
| - confFile := config.GetString("config") |
121 |
| - if confFile != "" { |
122 |
| - // hardcode for custom config file type to yaml |
123 |
| - // this workaround is needed otherwise viper return empty conf |
124 |
| - // see issue https://github.com/spf13/viper/issues/316 |
125 |
| - config.SetConfigType("yaml") |
126 |
| - confData, err := ioutil.ReadFile(confFile) |
127 |
| - if err != nil { |
128 |
| - log.Fatal("Could not read configuration file for exporter: ", err) |
129 |
| - } |
130 |
| - config.ReadConfig(bytes.NewBuffer(confData)) |
131 |
| - if err != nil { |
132 |
| - log.Fatal("Could not parse configuration:", err) |
133 |
| - } |
134 |
| - log.Info("Using custom configuration file provided by flag") |
135 |
| - return |
136 |
| - } |
137 |
| - |
138 |
| - // if no custom file given, read configuration from default paths |
139 |
| - err := config.ReadInConfig() |
140 |
| - if err != nil { |
141 |
| - log.Warn(err) |
142 |
| - log.Info("Default config values will be used") |
143 |
| - } else { |
144 |
| - log.Info("Using config file: ", config.ConfigFileUsed()) |
145 |
| - } |
146 |
| - |
147 |
| -} |
0 commit comments