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 dmcache input plugin #1667

Merged
merged 19 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,6 +14,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/couchbase"
_ "github.com/influxdata/telegraf/plugins/inputs/couchdb"
_ "github.com/influxdata/telegraf/plugins/inputs/disque"
_ "github.com/influxdata/telegraf/plugins/inputs/dmcache"
_ "github.com/influxdata/telegraf/plugins/inputs/dns_query"
_ "github.com/influxdata/telegraf/plugins/inputs/docker"
_ "github.com/influxdata/telegraf/plugins/inputs/dovecot"
Expand Down
11 changes: 11 additions & 0 deletions plugins/inputs/dmcache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# DMCache Input Plugin

This plugin provide a native collection for dmsetup based statistics for dm-cache

## Configuration

```
[[inputs.dmcache]]
## Whether to report per-device stats or not
Copy link
Contributor

Choose a reason for hiding this comment

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

these lines should both be indented by two spaces

per_device = true
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Make sure you document the tags/fields and stick to the format of the example as closely as possible (for instance little things such as three #'s for Configuration.)

https://github.com/raw/influxdata/telegraf/master/plugins/inputs/EXAMPLE_README.md

How did you go about naming the fields? Can you link to any documentation you may have used?

Copy link
Contributor

Choose a reason for hiding this comment

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

All of the fields are basically taken from the output of the dmsetup status --target=cache output for the given disks. In this case, we want to split all the data into multiple lines per disk we set up.

Copy link
Contributor

Choose a reason for hiding this comment

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

So the output of that command, based on the test cases, is more or less a table of values. Where did you find out what each column represents?

49 changes: 49 additions & 0 deletions plugins/inputs/dmcache/dmcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dmcache

import (
"os/exec"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)

type DMCache struct {
PerDevice bool `toml:"per_device"`
rawStatus func() ([]string, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like the name rawStatus is not fitting, since this functions both executes the command and does some processing on the output.

}

var sampleConfig = `
## Whether to report per-device stats or not
Copy link
Contributor

Choose a reason for hiding this comment

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

indent by 2 spaces instead of tabs

per_device = true
`

func (c *DMCache) SampleConfig() string {
return sampleConfig
}

func (c *DMCache) Description() string {
return "Provide a native collection for dmsetup based statistics for dm-cache"
}

func init() {
inputs.Add("dmcache", func() telegraf.Input {
return &DMCache{
PerDevice: true,
rawStatus: func() ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather have this be a named function that you pass in to the struct.

out, err := exec.Command("/bin/sh", "-c", "sudo /sbin/dmsetup status --target cache").Output()
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to document that this plugin requires sudo, and that the telegraf user needs to be setup to be able to execute sudo without a password.

You should also document that sudo /sbin/dmsetup status --target cache is the full command that telegraf will run for debugging purposes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being ran in a shell instead of just being directly executed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it is required for sudo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It's required for sudo.

if err != nil {
return nil, err
}
if string(out) == "No devices found\n" {
return nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Return an empty slice instead of nil

}

status := strings.Split(string(out), "\n")
status = status[:len(status)-1] // removing last empty line
Copy link
Contributor

Choose a reason for hiding this comment

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

This will panic if there is status is not at least length 1, I suggest you strings.TrimRight(out, "\n") before splitting lines.


return status, nil
},
}
})
}
131 changes: 131 additions & 0 deletions plugins/inputs/dmcache/dmcache_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// +build linux

package dmcache

import (
"strconv"
"strings"

"github.com/influxdata/telegraf"
)

const metricName = "dmcache"

var fieldNames = [...]string{
"metadata_used",
"metadata_free",
"cache_used",
"cache_free",
"read_hits",
"read_misses",
"write_hits",
"write_misses",
"demotions",
"promotions",
"dirty",
}

func (c *DMCache) Gather(acc telegraf.Accumulator) error {
outputLines, err := c.rawStatus()
if err != nil {
return err
}

var total map[string]interface{}
if !c.PerDevice {
total = make(map[string]interface{})
}

for _, s := range outputLines {
fields := make(map[string]interface{})
data, err := parseDMSetupStatus(s)
if err != nil {
return err
}

for _, f := range fieldNames {
fields[f] = calculateSize(data, f)
}

if c.PerDevice {
Copy link
Contributor

Choose a reason for hiding this comment

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

this if-statement doesn't seem correct to me.

If the user sets per_device = true, then it should report the per-device stats AND total stats.

Is there a reason users would not want total stats? If so it should be a separate config option.

tags := map[string]string{"device": data["device"].(string)}
acc.AddFields(metricName, fields, tags)
} else {
aggregateStats(total, fields)
}
}

if !c.PerDevice {
acc.AddFields(metricName, total, nil)
}

return nil
}

func parseDMSetupStatus(line string) (status map[string]interface{}, err error) {
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

why would this function panic? you should write your code to avoid panics rather than write a recover()

if r := recover(); r != nil {
status = nil
err = r.(error)
}
}()

values := strings.Split(line, " ")
Copy link
Contributor

Choose a reason for hiding this comment

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

you might want to use strings.Fields here in case of multiple spaces or tabs

status = make(map[string]interface{})

status["device"] = values[0][:len(values[0])-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

all of these should check length of the array to avoid panicking.

Copy link
Contributor

Choose a reason for hiding this comment

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

or you can check that the array is of a minimum length, and just ignore all metrics if it isn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is trimming the colon on the right, use TrimRight

status["length"] = toInt(values[2])
status["target"] = values[3]
status["metadata_blocksize"] = toInt(values[4])
status["metadata_used"] = toInt(strings.Split(values[5], "/")[0])
status["metadata_total"] = toInt(strings.Split(values[5], "/")[1])
status["cache_blocksize"] = toInt(values[6])
status["cache_used"] = toInt(strings.Split(values[7], "/")[0])
status["cache_total"] = toInt(strings.Split(values[7], "/")[1])
status["read_hits"] = toInt(values[8])
status["read_misses"] = toInt(values[9])
status["write_hits"] = toInt(values[10])
status["write_misses"] = toInt(values[11])
status["demotions"] = toInt(values[12])
status["promotions"] = toInt(values[13])
status["dirty"] = toInt(values[14])
status["blocksize"] = 512

return status, nil
}

func toInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

don't panic, return error

}
return i
}

func calculateSize(data map[string]interface{}, key string) (value int) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand this correctly, we are synthesizing a few aggregate values in this function? Why not just provide the values dmsetup returns, since the user can always calculate these at query time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#1581 - this is the source of this solution.
@daviesalex could you comment?

Copy link

Choose a reason for hiding this comment

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

Thats exactly what is going on. I had a look at it and I agree, these should be do-able at query time without drama (in very old versions of InfluxDB it was a challenge to do everything at query time if you wanted to select if I recall, which is why I think this started off this way).

@vlasad I suggest we remove this. If we did want to do this, the neat way to do it is probably a output plugin in telegraf.

@sebito91 FYI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

if key == "metadata_free" {
value = data["metadata_total"].(int) - data["metadata_used"].(int)
} else if key == "cache_free" {
value = data["cache_total"].(int) - data["cache_used"].(int) - data["dirty"].(int)
} else {
value = data[key].(int)
}

if key == "metadata_free" || key == "metadata_used" {
value = value * data["blocksize"].(int) * data["metadata_blocksize"].(int)
} else {
value = value * data["blocksize"].(int) * data["cache_blocksize"].(int)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

What about the values after dirty, why do we not collect them?

Copy link
Contributor

Choose a reason for hiding this comment

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

What about the values after dirty, do we need to collect them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what you're asking here, are you questioning why we're collecting the "dirty" blocks?

Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed in the sample output included in the tests that there are more values after the column containing "dirty". Are these metrics important, should we be collecting them, and why?

return
}

func aggregateStats(total, fields map[string]interface{}) {
for key, value := range fields {
if _, ok := total[key]; ok {
total[key] = total[key].(int) + value.(int)
} else {
total[key] = value.(int)
}
}
}
11 changes: 11 additions & 0 deletions plugins/inputs/dmcache/dmcache_notlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !linux

package dmcache

import (
"github.com/influxdata/telegraf"
)

func (c *DMCache) Gather(acc telegraf.Accumulator) error {
return nil
}
94 changes: 94 additions & 0 deletions plugins/inputs/dmcache/dmcache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// +build linux
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you can run these tests on non-linux system as well


package dmcache

import (
"testing"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func output2Devices() ([]string, error) {
return []string{
"cs-1: 0 4883791872 cache 8 1018/1501122 512 7/464962 139 352643 15 46 0 7 0 1 writeback 2 migration_threshold 2048 mq 10 random_threshold 4 sequential_threshold 512 discard_promote_adjustment 1 read_promote_adjustment 4 write_promote_adjustment 8",
"cs-2: 0 4294967296 cache 8 72352/1310720 128 26/24327168 2409 286 265 524682 0 0 0 1 writethrough 2 migration_threshold 2048 mq 10 random_threshold 4 sequential_threshold 512 discard_promote_adjustment 1 read_promote_adjustment 4 write_promote_adjustment 8",
}, nil
}

var dmc1 = &DMCache{
PerDevice: true,
rawStatus: output2Devices,
}

func TestDMCacheStats_1(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move the DMCache initialization into the function and rename the tests like TestWhateverThisTests.

var acc testutil.Accumulator

err := dmc1.Gather(&acc)
require.NoError(t, err)

tags1 := map[string]string{
"device": "cs-1",
}
fields1 := map[string]interface{}{
"metadata_used": 4169728,
"metadata_free": 6144425984,
"cache_used": 1835008,
"cache_free": 121885163520,
"read_hits": 36438016,
"read_misses": 92443246592,
"write_hits": 3932160,
"write_misses": 12058624,
"demotions": 0,
"promotions": 1835008,
"dirty": 0,
}
acc.AssertContainsTaggedFields(t, "dmcache", fields1, tags1)

tags2 := map[string]string{
"device": "cs-2",
}
fields2 := map[string]interface{}{
"metadata_used": 296353792,
"metadata_free": 5072355328,
"cache_used": 1703936,
"cache_free": 1594303578112,
"read_hits": 157876224,
"read_misses": 18743296,
"write_hits": 17367040,
"write_misses": 34385559552,
"demotions": 0,
"promotions": 0,
"dirty": 0,
}
acc.AssertContainsTaggedFields(t, "dmcache", fields2, tags2)
}

var dmc2 = &DMCache{
PerDevice: false,
rawStatus: output2Devices,
}

func TestDMCacheStats_2(t *testing.T) {
var acc testutil.Accumulator

err := dmc2.Gather(&acc)
require.NoError(t, err)

tags := map[string]string{}

fields := map[string]interface{}{
"metadata_used": 300523520,
"metadata_free": 11216781312,
"cache_used": 3538944,
"cache_free": 1716188741632,
"read_hits": 194314240,
"read_misses": 92461989888,
"write_hits": 21299200,
"write_misses": 34397618176,
"demotions": 0,
"promotions": 1835008,
"dirty": 0,
}
acc.AssertContainsTaggedFields(t, "dmcache", fields, tags)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The tests need to be made more comprehensive. Please try to think of some more corner cases and error conditions to test for. Some things that come to mind is the "No devices found" error and errors from rawOutput(). Try checking the coverage for more ideas.