-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SSL/TLS support to nginx input plugin #2883
Merged
danielnelson
merged 4 commits into
influxdata:master
from
bobmshannon:enhancement/nginx_ssl_config
Jun 8, 2017
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -12,16 +12,41 @@ import ( | |
"time" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/internal" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
type Nginx struct { | ||
// List of status URLs | ||
Urls []string | ||
// Path to CA file | ||
SSLCA string `toml:"ssl_ca"` | ||
// Path to client cert file | ||
SSLCert string `toml:"ssl_cert"` | ||
// Path to cert key file | ||
SSLKey string `toml:"ssl_key"` | ||
// Use SSL but skip chain & host verification | ||
InsecureSkipVerify bool | ||
// HTTP client | ||
client *http.Client | ||
// Response timeout | ||
ResponseTimeout internal.Duration | ||
} | ||
|
||
var sampleConfig = ` | ||
## An array of Nginx stub_status URI to gather stats. | ||
urls = ["http://localhost/status"] | ||
# Read Nginx's basic status information (ngx_http_stub_status_module) | ||
[[inputs.nginx]] | ||
# An array of Nginx stub_status URI to gather stats. | ||
urls = ["http://localhost/server_status"] | ||
|
||
# TLS/SSL configuration | ||
ssl_ca = "var/security/ca.pem" | ||
ssl_cert = "var/security/cert.cer" | ||
ssl_key = "var/security/key.key" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the paths to |
||
insecure_skip_verify = false | ||
|
||
# HTTP response timeout (default: 5s) | ||
response_timeout = "10s" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set this to 5s since that's the default |
||
` | ||
|
||
func (n *Nginx) SampleConfig() string { | ||
|
@@ -35,6 +60,16 @@ func (n *Nginx) Description() string { | |
func (n *Nginx) Gather(acc telegraf.Accumulator) error { | ||
var wg sync.WaitGroup | ||
|
||
// Create an HTTP client that is re-used for each | ||
// collection interval | ||
if n.client == nil { | ||
client, err := n.createHttpClient() | ||
if err != nil { | ||
return err | ||
} | ||
n.client = client | ||
} | ||
|
||
for _, u := range n.Urls { | ||
addr, err := url.Parse(u) | ||
if err != nil { | ||
|
@@ -52,17 +87,29 @@ func (n *Nginx) Gather(acc telegraf.Accumulator) error { | |
return nil | ||
} | ||
|
||
var tr = &http.Transport{ | ||
ResponseHeaderTimeout: time.Duration(3 * time.Second), | ||
} | ||
func (n *Nginx) createHttpClient() (*http.Client, error) { | ||
tlsCfg, err := internal.GetTLSConfig( | ||
n.SSLCert, n.SSLKey, n.SSLCA, n.InsecureSkipVerify) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if n.ResponseTimeout.Duration < time.Second { | ||
n.ResponseTimeout.Duration = time.Second * 5 | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: tlsCfg, | ||
}, | ||
Timeout: n.ResponseTimeout.Duration, | ||
} | ||
|
||
var client = &http.Client{ | ||
Transport: tr, | ||
Timeout: time.Duration(4 * time.Second), | ||
return client, nil | ||
} | ||
|
||
func (n *Nginx) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error { | ||
resp, err := client.Get(addr.String()) | ||
resp, err := n.client.Get(addr.String()) | ||
if err != nil { | ||
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the toml table header here, it is added automatically when outputting the sample config.