-
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 dmcache input plugin #1667
Add dmcache input plugin #1667
Changes from 2 commits
67952f1
7143b60
f5e762f
e5943bb
5eadca5
dc4c419
b8d6d96
039d558
2091671
edd631a
525fc05
c7a7ab6
b696320
9044638
6ab3f04
95e01e0
7981757
0fc7149
bc220fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# DMCache Input Plugin | ||
|
||
This plugin provide a native collection for dmsetup based statistics for dm-cache. | ||
|
||
This plugin requires sudo, that is why you should setup and be sure that the telegraf is able to execute sudo without a password. | ||
|
||
`sudo /sbin/dmsetup status --target cache` is the full command that telegraf will run for debugging purposes. | ||
|
||
## Configuration | ||
|
||
``` | ||
[[inputs.dmcache]] | ||
## Whether to report per-device stats or not | ||
per_device = true | ||
``` | ||
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) | ||
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. I feel like the name |
||
} | ||
|
||
var sampleConfig = ` | ||
## Whether to report per-device stats or not | ||
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) { | ||
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. 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() | ||
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. 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 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. Why is this being ran in a shell instead of just being directly executed? 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. Maybe it is required for sudo? 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. Yes. It's required for sudo. |
||
if err != nil { | ||
return nil, err | ||
} | ||
if string(out) == "No devices found\n" { | ||
return nil, nil | ||
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. Return an empty slice instead of nil |
||
} | ||
|
||
status := strings.Split(string(out), "\n") | ||
status = status[:len(status)-1] // removing last empty line | ||
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. 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 | ||
}, | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// +build linux | ||
|
||
package dmcache | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"errors" | ||
|
||
"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 | ||
} | ||
|
||
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 { | ||
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. 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) | ||
} | ||
aggregateStats(total, fields) | ||
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. Switch to fields at the last moment. Overall flow should be get the cacheStatus, aggregate, then convert to fields and emit as needed. |
||
} | ||
|
||
acc.AddFields(metricName, total, map[string]string{"device": "all"}) | ||
|
||
return nil | ||
} | ||
|
||
func parseDMSetupStatus(line string) (map[string]interface{}, error) { | ||
var err error | ||
status := make(map[string]interface{}) | ||
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. You should define a struct for these values, that way less casting is needed later 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. But, in this case, function "calculateSize" will require to use reflection to get value by key. |
||
values := strings.Fields(line) | ||
if len(values) < 15 { | ||
return nil, errors.New("dmsetup status data have invalid format") | ||
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. Grammar, I suggest "Output from dmsetup could not be parsed" |
||
} | ||
|
||
status["device"] = values[0][:len(values[0])-1] | ||
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. all of these should check length of the array to avoid panicking. 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. or you can check that the array is of a minimum length, and just ignore all metrics if it isn't. 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. If this is trimming the colon on the right, use TrimRight |
||
status["length"], err = strconv.Atoi(values[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["target"] = values[3] | ||
status["metadata_blocksize"], err = strconv.Atoi(values[4]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["metadata_used"], err = strconv.Atoi(strings.Split(values[5], "/")[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["metadata_total"], err = strconv.Atoi(strings.Split(values[5], "/")[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_blocksize"], err = strconv.Atoi(values[6]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_used"], err = strconv.Atoi(strings.Split(values[7], "/")[0]) | ||
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. Split once, make sure it return len of 2 |
||
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_total"], err = strconv.Atoi(strings.Split(values[7], "/")[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["read_hits"], err = strconv.Atoi(values[8]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["read_misses"], err = strconv.Atoi(values[9]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["write_hits"], err = strconv.Atoi(values[10]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["write_misses"], err = strconv.Atoi(values[11]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["demotions"], err = strconv.Atoi(values[12]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["promotions"], err = strconv.Atoi(values[13]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["dirty"], err = strconv.Atoi(values[14]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["blocksize"] = 512 | ||
|
||
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. What about the values after dirty, why do we not collect them? 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. What about the values after dirty, do we need to collect them? 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. Not sure what you're asking here, are you questioning why we're collecting the "dirty" blocks? 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. 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 status, nil | ||
} | ||
|
||
func calculateSize(data map[string]interface{}, key string) (value int) { | ||
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. 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. 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. #1581 - this is the source of this solution. 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. 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 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. 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) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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) { | ||
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. Can you move the DMCache initialization into the function and rename the tests like |
||
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) | ||
|
||
tags3 := map[string]string{ | ||
"device": "all", | ||
} | ||
|
||
fields3 := 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", fields3, tags3) | ||
} | ||
|
||
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{ | ||
"device": "all", | ||
} | ||
|
||
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) | ||
} | ||
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. 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. |
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.
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?
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.
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.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.
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?