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: Telegraf CouchDB Plugin #652

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/aerospike"
_ "github.com/influxdata/telegraf/plugins/inputs/apache"
_ "github.com/influxdata/telegraf/plugins/inputs/bcache"
_ "github.com/influxdata/telegraf/plugins/inputs/couchdb"
_ "github.com/influxdata/telegraf/plugins/inputs/disque"
_ "github.com/influxdata/telegraf/plugins/inputs/docker"
_ "github.com/influxdata/telegraf/plugins/inputs/elasticsearch"
Expand Down
69 changes: 69 additions & 0 deletions plugins/inputs/couchdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Telegraf plugin: CouchDB
---

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.

### Configuration:

```
# Sample Config:
[[inputs.couchdb]]
hosts = ["http://localhost:5984/_stats"]
```

### Measurements & Fields:

Statistics specific to the internals of CouchDB:

- couchdb_auth_cache_misses
- couchdb_database_writes
- couchdb_open_databases
- couchdb_auth_cache_hits
- couchdb_request_time
- couchdb_database_reads
- couchdb_open_os_files

Statistics of HTTP requests by method:

- httpd_request_methods_put
- httpd_request_methods_get
- httpd_request_methods_copy
- httpd_request_methods_delete
- httpd_request_methods_post
- httpd_request_methods_head

Statistics of HTTP requests by response code:

- httpd_status_codes_200
- httpd_status_codes_201
- httpd_status_codes_202
- httpd_status_codes_301
- httpd_status_codes_304
- httpd_status_codes_400
- httpd_status_codes_401
- httpd_status_codes_403
- httpd_status_codes_404
- httpd_status_codes_405
- httpd_status_codes_409
- httpd_status_codes_412
- httpd_status_codes_500

httpd statistics:

- httpd_clients_requesting_changes
- httpd_temporary_view_reads
- httpd_requests
- httpd_bulk_requests
- httpd_view_reads

### Tags:

- server (url of the couchdb _stats endpoint)

### Example output:

```
➜ telegraf git:(master) ✗ ./telegraf -config ./config.conf -input-filter couchdb -test
* Plugin: couchdb, Collection 1
> couchdb,server=http://localhost:5984/_stats couchdb_auth_cache_hits_current=0,couchdb_auth_cache_hits_max=0,couchdb_auth_cache_hits_mean=0,couchdb_auth_cache_hits_min=0,couchdb_auth_cache_hits_stddev=0,couchdb_auth_cache_hits_sum=0,couchdb_auth_cache_misses_current=0,couchdb_auth_cache_misses_max=0,couchdb_auth_cache_misses_mean=0,couchdb_auth_cache_misses_min=0,couchdb_auth_cache_misses_stddev=0,couchdb_auth_cache_misses_sum=0,couchdb_database_reads_current=0,couchdb_database_reads_max=0,couchdb_database_reads_mean=0,couchdb_database_reads_min=0,couchdb_database_reads_stddev=0,couchdb_database_reads_sum=0,couchdb_database_writes_current=1102,couchdb_database_writes_max=131,couchdb_database_writes_mean=0.116,couchdb_database_writes_min=0,couchdb_database_writes_stddev=3.536,couchdb_database_writes_sum=1102,couchdb_open_databases_current=1,couchdb_open_databases_max=1,couchdb_open_databases_mean=0,couchdb_open_databases_min=0,couchdb_open_databases_stddev=0.01,couchdb_open_databases_sum=1,couchdb_open_os_files_current=2,couchdb_open_os_files_max=2,couchdb_open_os_files_mean=0,couchdb_open_os_files_min=0,couchdb_open_os_files_stddev=0.02,couchdb_open_os_files_sum=2,couchdb_request_time_current=242.21,couchdb_request_time_max=102,couchdb_request_time_mean=5.767,couchdb_request_time_min=1,couchdb_request_time_stddev=17.369,couchdb_request_time_sum=242.21,httpd_bulk_requests_current=0,httpd_bulk_requests_max=0,httpd_bulk_requests_mean=0,httpd_bulk_requests_min=0,httpd_bulk_requests_stddev=0,httpd_bulk_requests_sum=0,httpd_clients_requesting_changes_current=0,httpd_clients_requesting_changes_max=0,httpd_clients_requesting_changes_mean=0,httpd_clients_requesting_changes_min=0,httpd_clients_requesting_changes_stddev=0,httpd_clients_requesting_changes_sum=0,httpd_request_methods_copy_current=0,httpd_request_methods_copy_max=0,httpd_request_methods_copy_mean=0,httpd_request_methods_copy_min=0,httpd_request_methods_copy_stddev=0,httpd_request_methods_copy_sum=0,httpd_request_methods_delete_current=0,httpd_request_methods_delete_max=0,httpd_request_methods_delete_mean=0,httpd_request_methods_delete_min=0,httpd_request_methods_delete_stddev=0,httpd_request_methods_delete_sum=0,httpd_request_methods_get_current=31,httpd_request_methods_get_max=1,httpd_request_methods_get_mean=0.003,httpd_request_methods_get_min=0,httpd_request_methods_get_stddev=0.057,httpd_request_methods_get_sum=31,httpd_request_methods_head_current=0,httpd_request_methods_head_max=0,httpd_request_methods_head_mean=0,httpd_request_methods_head_min=0,httpd_request_methods_head_stddev=0,httpd_request_methods_head_sum=0,httpd_request_methods_post_current=1102,httpd_request_methods_post_max=131,httpd_request_methods_post_mean=0.116,httpd_request_methods_post_min=0,httpd_request_methods_post_stddev=3.536,httpd_request_methods_post_sum=1102,httpd_request_methods_put_current=1,httpd_request_methods_put_max=1,httpd_request_methods_put_mean=0,httpd_request_methods_put_min=0,httpd_request_methods_put_stddev=0.01,httpd_request_methods_put_sum=1,httpd_requests_current=1133,httpd_requests_max=130,httpd_requests_mean=0.118,httpd_requests_min=0,httpd_requests_stddev=3.512,httpd_requests_sum=1133,httpd_status_codes_200_current=31,httpd_status_codes_200_max=1,httpd_status_codes_200_mean=0.003,httpd_status_codes_200_min=0,httpd_status_codes_200_stddev=0.057,httpd_status_codes_200_sum=31,httpd_status_codes_201_current=1103,httpd_status_codes_201_max=130,httpd_status_codes_201_mean=0.116,httpd_status_codes_201_min=0,httpd_status_codes_201_stddev=3.532,httpd_status_codes_201_sum=1103,httpd_status_codes_202_current=0,httpd_status_codes_202_max=0,httpd_status_codes_202_mean=0,httpd_status_codes_202_min=0,httpd_status_codes_202_stddev=0,httpd_status_codes_202_sum=0,httpd_status_codes_301_current=0,httpd_status_codes_301_max=0,httpd_status_codes_301_mean=0,httpd_status_codes_301_min=0,httpd_status_codes_301_stddev=0,httpd_status_codes_301_sum=0,httpd_status_codes_304_current=0,httpd_status_codes_304_max=0,httpd_status_codes_304_mean=0,httpd_status_codes_304_min=0,httpd_status_codes_304_stddev=0,httpd_status_codes_304_sum=0,httpd_status_codes_400_current=0,httpd_status_codes_400_max=0,httpd_status_codes_400_mean=0,httpd_status_codes_400_min=0,httpd_status_codes_400_stddev=0,httpd_status_codes_400_sum=0,httpd_status_codes_401_current=0,httpd_status_codes_401_max=0,httpd_status_codes_401_mean=0,httpd_status_codes_401_min=0,httpd_status_codes_401_stddev=0,httpd_status_codes_401_sum=0,httpd_status_codes_403_current=0,httpd_status_codes_403_max=0,httpd_status_codes_403_mean=0,httpd_status_codes_403_min=0,httpd_status_codes_403_stddev=0,httpd_status_codes_403_sum=0,httpd_status_codes_404_current=0,httpd_status_codes_404_max=0,httpd_status_codes_404_mean=0,httpd_status_codes_404_min=0,httpd_status_codes_404_stddev=0,httpd_status_codes_404_sum=0,httpd_status_codes_405_current=0,httpd_status_codes_405_max=0,httpd_status_codes_405_mean=0,httpd_status_codes_405_min=0,httpd_status_codes_405_stddev=0,httpd_status_codes_405_sum=0,httpd_status_codes_409_current=0,httpd_status_codes_409_max=0,httpd_status_codes_409_mean=0,httpd_status_codes_409_min=0,httpd_status_codes_409_stddev=0,httpd_status_codes_409_sum=0,httpd_status_codes_412_current=0,httpd_status_codes_412_max=0,httpd_status_codes_412_mean=0,httpd_status_codes_412_min=0,httpd_status_codes_412_stddev=0,httpd_status_codes_412_sum=0,httpd_status_codes_500_current=0,httpd_status_codes_500_max=0,httpd_status_codes_500_mean=0,httpd_status_codes_500_min=0,httpd_status_codes_500_stddev=0,httpd_status_codes_500_sum=0,httpd_temporary_view_reads_current=0,httpd_temporary_view_reads_max=0,httpd_temporary_view_reads_mean=0,httpd_temporary_view_reads_min=0,httpd_temporary_view_reads_stddev=0,httpd_temporary_view_reads_sum=0,httpd_view_reads_current=0,httpd_view_reads_max=0,httpd_view_reads_mean=0,httpd_view_reads_min=0,httpd_view_reads_stddev=0,httpd_view_reads_sum=0 1454692257621938169
```
205 changes: 205 additions & 0 deletions plugins/inputs/couchdb/couchdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package couchdb

import (
"encoding/json"
"errors"
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"net/http"
"reflect"
"strings"
"sync"
)

// Schema:
type metaData struct {
Description string `json:"description"`
Current float64 `json:"current"`
Sum float64 `json:"sum"`
Mean float64 `json:"mean"`
Stddev float64 `json:"stddev"`
Min float64 `json:"min"`
Max float64 `json:"max"`
}

type Stats struct {
Couchdb struct {
AuthCacheMisses metaData `json:"auth_cache_misses"`
DatabaseWrites metaData `json:"database_writes"`
OpenDatabases metaData `json:"open_databases"`
AuthCacheHits metaData `json:"auth_cache_hits"`
RequestTime metaData `json:"request_time"`
DatabaseReads metaData `json:"database_reads"`
OpenOsFiles metaData `json:"open_os_files"`
} `json:"couchdb"`
HttpdRequestMethods struct {
Put metaData `json:"PUT"`
Get metaData `json:"GET"`
Copy metaData `json:"COPY"`
Delete metaData `json:"DELETE"`
Post metaData `json:"POST"`
Head metaData `json:"HEAD"`
} `json:"httpd_request_methods"`
HttpdStatusCodes struct {
Status200 metaData `json:"200"`
Status201 metaData `json:"201"`
Status202 metaData `json:"202"`
Status301 metaData `json:"301"`
Status304 metaData `json:"304"`
Status400 metaData `json:"400"`
Status401 metaData `json:"401"`
Status403 metaData `json:"403"`
Status404 metaData `json:"404"`
Status405 metaData `json:"405"`
Status409 metaData `json:"409"`
Status412 metaData `json:"412"`
Status500 metaData `json:"500"`
} `json:"httpd_status_codes"`
Httpd struct {
ClientsRequestingChanges metaData `json:"clients_requesting_changes"`
TemporaryViewReads metaData `json:"temporary_view_reads"`
Requests metaData `json:"requests"`
BulkRequests metaData `json:"bulk_requests"`
ViewReads metaData `json:"view_reads"`
} `json:"httpd"`
}

type CouchDB struct {
HOSTs []string `toml:"hosts"`
}

func (*CouchDB) Description() string {
return "Read CouchDB Stats from one or more servers"
}

func (*CouchDB) SampleConfig() string {
return `
# Works with CouchDB stats endpoints out of the box
# Multiple HOSTs from which to read CouchDB stats:
hosts = ["http://localhost:8086/_stats"]
`
}

func (c *CouchDB) Gather(accumulator telegraf.Accumulator) error {
errorChannel := make(chan error, len(c.HOSTs))
var wg sync.WaitGroup
for _, u := range c.HOSTs {
wg.Add(1)
go func(host string) {
defer wg.Done()
if err := c.fetchAndInsertData(accumulator, host); err != nil {
errorChannel <- fmt.Errorf("[host=%s]: %s", host, err)
}
}(u)
}

wg.Wait()
close(errorChannel)

// If there weren't any errors, we can return nil now.
if len(errorChannel) == 0 {
return nil
}

// There were errors, so join them all together as one big error.
errorStrings := make([]string, 0, len(errorChannel))
for err := range errorChannel {
errorStrings = append(errorStrings, err.Error())
}

return errors.New(strings.Join(errorStrings, "\n"))

}

func (c *CouchDB) fetchAndInsertData(accumulator telegraf.Accumulator, host string) error {

response, error := http.Get(host)
if error != nil {
return error
}
defer response.Body.Close()

var stats Stats
decoder := json.NewDecoder(response.Body)
decoder.Decode(&stats)

fields := map[string]interface{}{}

// CouchDB meta stats:
c.MapCopy(fields, c.generateFields("couchdb_auth_cache_misses", stats.Couchdb.AuthCacheMisses))
c.MapCopy(fields, c.generateFields("couchdb_database_writes", stats.Couchdb.DatabaseWrites))
c.MapCopy(fields, c.generateFields("couchdb_open_databases", stats.Couchdb.OpenDatabases))
c.MapCopy(fields, c.generateFields("couchdb_auth_cache_hits", stats.Couchdb.AuthCacheHits))
c.MapCopy(fields, c.generateFields("couchdb_request_time", stats.Couchdb.RequestTime))
c.MapCopy(fields, c.generateFields("couchdb_database_reads", stats.Couchdb.DatabaseReads))
c.MapCopy(fields, c.generateFields("couchdb_open_os_files", stats.Couchdb.OpenOsFiles))

// http request methods stats:
c.MapCopy(fields, c.generateFields("httpd_request_methods_put", stats.HttpdRequestMethods.Put))
c.MapCopy(fields, c.generateFields("httpd_request_methods_get", stats.HttpdRequestMethods.Get))
c.MapCopy(fields, c.generateFields("httpd_request_methods_copy", stats.HttpdRequestMethods.Copy))
c.MapCopy(fields, c.generateFields("httpd_request_methods_delete", stats.HttpdRequestMethods.Delete))
c.MapCopy(fields, c.generateFields("httpd_request_methods_post", stats.HttpdRequestMethods.Post))
c.MapCopy(fields, c.generateFields("httpd_request_methods_head", stats.HttpdRequestMethods.Head))

// status code stats:
c.MapCopy(fields, c.generateFields("httpd_status_codes_200", stats.HttpdStatusCodes.Status200))
c.MapCopy(fields, c.generateFields("httpd_status_codes_201", stats.HttpdStatusCodes.Status201))
c.MapCopy(fields, c.generateFields("httpd_status_codes_202", stats.HttpdStatusCodes.Status202))
c.MapCopy(fields, c.generateFields("httpd_status_codes_301", stats.HttpdStatusCodes.Status301))
c.MapCopy(fields, c.generateFields("httpd_status_codes_304", stats.HttpdStatusCodes.Status304))
c.MapCopy(fields, c.generateFields("httpd_status_codes_400", stats.HttpdStatusCodes.Status400))
c.MapCopy(fields, c.generateFields("httpd_status_codes_401", stats.HttpdStatusCodes.Status401))
c.MapCopy(fields, c.generateFields("httpd_status_codes_403", stats.HttpdStatusCodes.Status403))
c.MapCopy(fields, c.generateFields("httpd_status_codes_404", stats.HttpdStatusCodes.Status404))
c.MapCopy(fields, c.generateFields("httpd_status_codes_405", stats.HttpdStatusCodes.Status405))
c.MapCopy(fields, c.generateFields("httpd_status_codes_409", stats.HttpdStatusCodes.Status409))
c.MapCopy(fields, c.generateFields("httpd_status_codes_412", stats.HttpdStatusCodes.Status412))
c.MapCopy(fields, c.generateFields("httpd_status_codes_500", stats.HttpdStatusCodes.Status500))

// httpd stats:
c.MapCopy(fields, c.generateFields("httpd_clients_requesting_changes", stats.Httpd.ClientsRequestingChanges))
c.MapCopy(fields, c.generateFields("httpd_temporary_view_reads", stats.Httpd.TemporaryViewReads))
c.MapCopy(fields, c.generateFields("httpd_requests", stats.Httpd.Requests))
c.MapCopy(fields, c.generateFields("httpd_bulk_requests", stats.Httpd.BulkRequests))
c.MapCopy(fields, c.generateFields("httpd_view_reads", stats.Httpd.ViewReads))

tags := map[string]string{
"server": host,
}
accumulator.AddFields("couchdb", fields, tags)
return nil
}

func (*CouchDB) MapCopy(dst, src interface{}) {
dv, sv := reflect.ValueOf(dst), reflect.ValueOf(src)
for _, k := range sv.MapKeys() {
dv.SetMapIndex(k, sv.MapIndex(k))
}
}

func (*CouchDB) safeCheck(value interface{}) interface{} {
if value == nil {
return 0.0
}
return value
}

func (c *CouchDB) generateFields(prefix string, obj metaData) map[string]interface{} {
fields := map[string]interface{}{
prefix + "_current": c.safeCheck(obj.Current),
prefix + "_sum": c.safeCheck(obj.Sum),
prefix + "_mean": c.safeCheck(obj.Mean),
prefix + "_stddev": c.safeCheck(obj.Stddev),
prefix + "_min": c.safeCheck(obj.Min),
prefix + "_max": c.safeCheck(obj.Max),
}
return fields
}

func init() {
inputs.Add("couchdb", func() telegraf.Input {
return &CouchDB{}
})
}
Loading