forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add generator for sample-config includes (influxdata#13348)
- Loading branch information
Showing
12 changed files
with
255 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Set to true/false to enforce TLS being enabled/disabled. If not set, | ||
## enable TLS only if any of the other options are specified. | ||
# tls_enable = | ||
## Trusted root certificates for server | ||
# tls_ca = "/path/to/cafile" | ||
## Used for TLS client certificate authentication | ||
# tls_cert = "/path/to/certfile" | ||
## Used for TLS client certificate authentication | ||
# tls_key = "/path/to/keyfile" | ||
## Send the specified TLS server name via SNI | ||
# tls_server_name = "kubernetes.example.com" | ||
## Use TLS but skip chain & host verification | ||
# insecure_skip_verify = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Read formatted metrics from one or more HTTP endpoints | ||
[[inputs.http]] | ||
## One or more URLs from which to read formatted metrics | ||
urls = [ | ||
"http://localhost/metrics" | ||
] | ||
|
||
## HTTP method | ||
# method = "GET" | ||
|
||
## Optional HTTP headers | ||
# headers = {"X-Special-Header" = "Special-Value"} | ||
|
||
## HTTP entity-body to send with POST/PUT requests. | ||
# body = "" | ||
|
||
## HTTP Content-Encoding for write request body, can be set to "gzip" to | ||
## compress body or "identity" to apply no encoding. | ||
# content_encoding = "identity" | ||
|
||
## Optional file with Bearer token | ||
## file content is added as an Authorization header | ||
# bearer_token = "/path/to/file" | ||
|
||
## Optional HTTP Basic Auth Credentials | ||
# username = "username" | ||
# password = "pa$$word" | ||
|
||
## OAuth2 Client Credentials. The options 'client_id', 'client_secret', and 'token_url' are required to use OAuth2. | ||
# client_id = "clientid" | ||
# client_secret = "secret" | ||
# token_url = "https://indentityprovider/oauth2/v1/token" | ||
# scopes = ["urn:opc:idm:__myscopes__"] | ||
|
||
## HTTP Proxy support | ||
# use_system_proxy = false | ||
# http_proxy_url = "" | ||
|
||
## Optional TLS Config | ||
{{template "/plugins/common/tls/client.conf"}} | ||
|
||
## Optional Cookie authentication | ||
# cookie_auth_url = "https://localhost/authMe" | ||
# cookie_auth_method = "POST" | ||
# cookie_auth_username = "username" | ||
# cookie_auth_password = "pa$$word" | ||
# cookie_auth_headers = { Content-Type = "application/json", X-MY-HEADER = "hello" } | ||
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}' | ||
## cookie_auth_renewal not set or set to "0" will auth once and never renew the cookie | ||
# cookie_auth_renewal = "5m" | ||
|
||
## Amount of time allowed to complete the HTTP request | ||
# timeout = "5s" | ||
|
||
## List of success status codes | ||
# success_status_codes = [200] | ||
|
||
## Data format to consume. | ||
## Each data format has its own unique set of configuration options, read | ||
## more about them here: | ||
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md | ||
# data_format = "influx" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,4 +115,3 @@ | |
[inputs.modbus.request.tags] | ||
machine = "impresser" | ||
location = "main building" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"text/template/parse" | ||
) | ||
|
||
func extractIncludes(tmpl *template.Template) []string { | ||
var includes []string | ||
for _, node := range tmpl.Root.Nodes { | ||
if n, ok := node.(*parse.TemplateNode); ok { | ||
includes = append(includes, n.Name) | ||
} | ||
} | ||
return includes | ||
} | ||
|
||
func absolutePath(root, fn string) (string, error) { | ||
pwd, err := filepath.Abs(fn) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot determine absolute location of %q: %w", fn, err) | ||
} | ||
pwd, err = filepath.Rel(root, filepath.Dir(pwd)) | ||
if err != nil { | ||
return "", fmt.Errorf("Cannot determine location of %q relative to %q: %w", pwd, root, err) | ||
} | ||
return string(filepath.Separator) + pwd, nil | ||
} | ||
|
||
func main() { | ||
// Estimate Telegraf root to be able to handle absolute paths | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalf("Cannot get working directory: %v", err) | ||
} | ||
cwd, err = filepath.Abs(cwd) | ||
if err != nil { | ||
log.Fatalf("Cannot resolve working directory: %v", err) | ||
} | ||
|
||
var root string | ||
idx := strings.LastIndex(cwd, filepath.FromSlash("/plugins/")) | ||
if idx <= 0 { | ||
log.Fatalln("Cannot determine include root!") | ||
} | ||
root = cwd[:idx] | ||
|
||
var parent, inputFilename, outputFilename string | ||
switch len(os.Args) { | ||
case 1: | ||
parent = strings.TrimPrefix(filepath.ToSlash(cwd[idx:]), "/plugins/") | ||
parent = strings.ReplaceAll(parent, "/", ".") | ||
inputFilename = "sample.conf.in" | ||
outputFilename = "sample.conf" | ||
case 2: | ||
parent = os.Args[1] | ||
inputFilename = "sample.conf.in" | ||
outputFilename = "sample.conf" | ||
case 3: | ||
parent = os.Args[1] | ||
inputFilename = os.Args[2] | ||
if !strings.HasSuffix(inputFilename, ".in") { | ||
log.Fatalf("Template filename %q does not have '.in' suffix!", inputFilename) | ||
} | ||
outputFilename = strings.TrimSuffix(inputFilename, ".in") | ||
case 4: | ||
parent = os.Args[1] | ||
inputFilename = os.Args[2] | ||
outputFilename = os.Args[3] | ||
default: | ||
log.Fatalln("Invalid number of arguments") | ||
} | ||
|
||
roottmpl := template.New(inputFilename) | ||
known := make(map[string]bool) | ||
inroot, err := absolutePath(root, inputFilename) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
unresolved := map[string]string{inputFilename: filepath.Join(inroot, inputFilename)} | ||
for { | ||
if len(unresolved) == 0 { | ||
break | ||
} | ||
|
||
newUnresolved := make(map[string]string) | ||
for name, fn := range unresolved { | ||
if filepath.IsAbs(fn) { | ||
fn = filepath.Join(root, fn) | ||
} | ||
|
||
if known[name] { | ||
// Include already resolved, skipping | ||
continue | ||
} | ||
|
||
tmpl, err := template.ParseFiles(fn) | ||
if err != nil { | ||
log.Fatalf("Reading template %q failed: %v", fn, err) | ||
} | ||
known[name] = true | ||
if _, err := roottmpl.AddParseTree(name, tmpl.Tree); err != nil { | ||
log.Fatalf("Adding include %q failed: %v", fn, err) | ||
} | ||
|
||
// For relative paths we need to make it relative to the include | ||
pwd, err := filepath.Abs(fn) | ||
if err != nil { | ||
log.Fatalf("Cannot determine absolute location of %q: %v", fn, err) | ||
} | ||
pwd, err = filepath.Rel(root, filepath.Dir(pwd)) | ||
if err != nil { | ||
log.Fatalf("Cannot determine location of %q relative to %q: %v", pwd, root, err) | ||
} | ||
pwd = string(filepath.Separator) + pwd | ||
for _, iname := range extractIncludes(tmpl) { | ||
ifn := iname | ||
if !filepath.IsAbs(ifn) { | ||
ifn = filepath.Join(pwd, ifn) | ||
} | ||
newUnresolved[iname] = ifn | ||
} | ||
} | ||
unresolved = newUnresolved | ||
} | ||
|
||
defines := map[string]string{"parent": parent} | ||
var buf bytes.Buffer | ||
if err := roottmpl.Execute(&buf, defines); err != nil { | ||
log.Fatalf("Executing template failed: %v", err) | ||
} | ||
|
||
if err := os.WriteFile(outputFilename, buf.Bytes(), 0640); err != nil { | ||
log.Fatalf("Writing output %q failed: %v", outputFilename, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters