Merge pull request #1243 from DiniFarb/master

Load config file from URL
This commit is contained in:
Ben Reedy 2023-07-17 20:26:34 +10:00 committed by GitHub
commit 89cb5439b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View File

@ -89,6 +89,8 @@ Flag | Description | Default value
`--collectors.print` | If true, print available collectors and exit. |
`--scrape.timeout-margin` | Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads. | `0.5`
`--web.config.file` | A [web config][web_config] for setting up TLS and Auth | None
`--config.file` | [Using a config file](#using-a-configuration-file) from path or URL | None
`--config.file.insecure-skip-verify` | Skip TLS when loading config file from URL | false
## Installation
The latest release can be downloaded from the [releases page](https://github.com/prometheus-community/windows_exporter/releases).
@ -173,6 +175,10 @@ This enables the additional process and container collectors on top of the defau
YAML configuration files can be specified with the `--config.file` flag. e.g. `.\windows_exporter.exe --config.file=config.yml`. If you are using the absolute path, make sure to quote the path, e.g. `.\windows_exporter.exe --config.file="C:\Program Files\windows_exporter\config.yml"`
It is also possible to load the configuration from a URL. e.g. `.\windows_exporter.exe --config.file="https://example.com/config.yml"`
If you need to skip TLS verification, you can use the `--config.file.insecure-skip-verify` flag. e.g. `.\windows_exporter.exe --config.file="https://example.com/config.yml" --config.file.insecure-skip-verify`
```yaml
collectors:
enabled: cpu,cs,net,service

View File

@ -14,7 +14,11 @@
package config
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"
"github.com/alecthomas/kingpin/v2"
@ -33,19 +37,44 @@ type Resolver struct {
}
// NewResolver returns a Resolver structure.
func NewResolver(file string, logger log.Logger) (*Resolver, error) {
func NewResolver(file string, logger log.Logger, insecure_skip_verify bool) (*Resolver, error) {
flags := map[string]string{}
_ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file: %v", file))
if _, err := os.Stat(file); err != nil {
return nil, err
}
b, err := os.ReadFile(file)
var fileBytes []byte
url, err := url.ParseRequestURI(file)
if err != nil {
return nil, err
}
if url.Scheme == "http" || url.Scheme == "https" {
_ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file from URL: %v", file))
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure_skip_verify},
}
if insecure_skip_verify {
_ = level.Warn(logger).Log("msg", "Loading configuration file with TLS verification disabled")
}
client := &http.Client{Transport: tr}
resp, err := client.Get(file)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fileBytes, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
} else {
_ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file: %v", file))
if _, err := os.Stat(file); err != nil {
return nil, err
}
fileBytes, err = os.ReadFile(file)
if err != nil {
return nil, err
}
}
var rawValues map[string]interface{}
err = yaml.Unmarshal(b, &rawValues)
err = yaml.Unmarshal(fileBytes, &rawValues)
if err != nil {
return nil, err
}

View File

@ -105,6 +105,10 @@ func main() {
"config.file",
"YAML configuration file to use. Values set in this file will be overridden by CLI flags.",
).String()
insecure_skip_verify = app.Flag(
"config.file.insecure-skip-verify",
"Skip TLS verification in loading YAML configuration.",
).Default("false").Bool()
webConfig = webflag.AddFlags(app, ":9182")
metricsPath = app.Flag(
"telemetry.path",
@ -152,7 +156,7 @@ func main() {
_ = level.Debug(logger).Log("msg", "Logging has Started")
if *configFile != "" {
resolver, err := config.NewResolver(*configFile, logger)
resolver, err := config.NewResolver(*configFile, logger, *insecure_skip_verify)
if err != nil {
_ = level.Error(logger).Log("msg", "could not load config file", "err", err)
os.Exit(1)