Skip to content

Commit

Permalink
Merge pull request #1243 from DiniFarb/master
Browse files Browse the repository at this point in the history
Load config file from URL
  • Loading branch information
breed808 authored Jul 17, 2023
2 parents 80cf16e + b08ce06 commit 89cb543
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
43 changes: 36 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
package config

import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 89cb543

Please sign in to comment.