Skip to content
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 support for basic auth in couchdb input #5160

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions plugins/inputs/couchdb/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# CouchDB Input Plugin
---

The CouchDB plugin gathers metrics of CouchDB using [_stats](http://docs.couchdb.org/en/1.6.1/api/server/common.html?highlight=stats#get--_stats) endpoint.
The CouchDB plugin gathers metrics of CouchDB using [_stats] endpoint.

### Configuration:
### Configuration

```
# Sample Config:
```toml
[[inputs.couchdb]]
hosts = ["http://localhost:5984/_stats"]
## Works with CouchDB stats endpoints out of the box
## Multiple Hosts from which to read CouchDB stats:
hosts = ["http://localhost:8086/_stats"]

## Use HTTP Basic Authentication.
# basic_username = "telegraf"
# basic_password = "p@ssw0rd"
```

### Measurements & Fields:
Expand Down Expand Up @@ -71,3 +75,5 @@ couchdb,server=http://couchdb22:5984/_node/_local/_stats couchdb_auth_cache_hits
```
couchdb,server=http://couchdb16:5984/_stats couchdb_request_time_sum=96,httpd_status_codes_200_sum=37,httpd_status_codes_200_min=0,httpd_requests_mean=0.005,httpd_requests_min=0,couchdb_request_time_stddev=3.833,couchdb_request_time_min=1,httpd_request_methods_get_stddev=0.073,httpd_request_methods_get_min=0,httpd_status_codes_200_mean=0.005,httpd_status_codes_200_max=1,httpd_requests_sum=37,couchdb_request_time_current=96,httpd_request_methods_get_sum=37,httpd_request_methods_get_mean=0.005,httpd_request_methods_get_max=1,httpd_status_codes_200_stddev=0.073,couchdb_request_time_mean=2.595,couchdb_request_time_max=25,httpd_request_methods_get_current=37,httpd_status_codes_200_current=37,httpd_requests_current=37,httpd_requests_stddev=0.073,httpd_requests_max=1 1536707179000000000
```

[_stats]: http://docs.couchdb.org/en/1.6.1/api/server/common.html?highlight=stats#get--_stats
20 changes: 18 additions & 2 deletions plugins/inputs/couchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ type (
}

CouchDB struct {
Hosts []string `toml:"hosts"`
Hosts []string `toml:"hosts"`
BasicUsername string `toml:"basic_username"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably would have just called it Username/Password to stay uniform with other plugins (elasticsearch, kibana, icinga2, rabbitmq, etc...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was on the fence about this but decided to be more explicit because CouchDB supports several authentication methods. We have a number plugins using this naming as well and I could go either way.

I would probably never consider implementing the cookie method, but the proxy method is a possibility. I guess if we added it we could provide another option auth_method? Which way do you think is best?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound logic. I say leave it, as you said, there are other plugins that use the basic_ prefix.

BasicPassword string `toml:"basic_password"`

client *http.Client
}
Expand All @@ -95,6 +97,10 @@ func (*CouchDB) SampleConfig() string {
## Works with CouchDB stats endpoints out of the box
## Multiple Hosts from which to read CouchDB stats:
hosts = ["http://localhost:8086/_stats"]
## Use HTTP Basic Authentication.
# basic_username = "telegraf"
# basic_password = "p@ssw0rd"
`
}

Expand Down Expand Up @@ -124,7 +130,17 @@ func (c *CouchDB) fetchAndInsertData(accumulator telegraf.Accumulator, host stri
Timeout: time.Duration(4 * time.Second),
}
}
response, error := c.client.Get(host)

req, err := http.NewRequest("GET", host, nil)
if err != nil {
return err
}

if c.BasicUsername != "" || c.BasicPassword != "" {
req.SetBasicAuth(c.BasicUsername, c.BasicPassword)
}

response, error := c.client.Do(req)
if error != nil {
return error
}
Expand Down