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

Add dmcache input plugin #1667

merged 19 commits into from
Apr 7, 2017

Conversation

vlasad
Copy link
Contributor

@vlasad vlasad commented Aug 24, 2016

Required for all PRs:

  • CHANGELOG.md updated (we recommend not updating this until the PR has been approved by a maintainer)
  • Sign CLA (if not already signed)
  • README.md updated (if adding a new plugin)

@jwilder jwilder added the feature request Requests for new plugin and for new features to existing plugins label Sep 1, 2016
@sebito91
Copy link
Contributor

Checking in on this PR, the CircleCI issues weren't fails from the dmcache code but from other components.

@sparrc sparrc added this to the 1.3.0 milestone Jan 21, 2017
@sparrc
Copy link
Contributor

sparrc commented Jan 21, 2017

yes, it does appear so, I'll slate this for 1.3 and try to get it reviewed soon

sparrc
sparrc previously requested changes Jan 24, 2017

```
[[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

}

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

}

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()

}
}()

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

values := strings.Split(line, " ")
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.

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

@@ -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

return &DMCache{
PerDevice: true,
rawStatus: func() ([]string, error) {
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.

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.

@vlasad
Copy link
Contributor Author

vlasad commented Mar 29, 2017

All requested changes were done.

return &DMCache{
PerDevice: true,
rawStatus: func() ([]string, error) {
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.

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.

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.

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.

status := make(map[string]interface{})
values := strings.Fields(line)
if len(values) < 15 {
return nil, errors.New("dmsetup status data have invalid format")
Copy link
Contributor

Choose a reason for hiding this comment

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

Grammar, I suggest "Output from dmsetup could not be parsed"

if err != nil {
return nil, err
}
status["cache_used"], err = strconv.Atoi(strings.Split(values[7], "/")[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

Split once, make sure it return len of 2


func parseDMSetupStatus(line string) (map[string]interface{}, error) {
var err error
status := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@vlasad vlasad Apr 3, 2017

Choose a reason for hiding this comment

The 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.

return status, nil
}

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

"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.


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.

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

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

I noticed while reading through the manpages for dmsetup that there is a stats subcommand. From what I gathered it requires the user to define a "region" before it can be used. Have you used this before? How do the stats it provides compare to the ones from the status command?

[[inputs.dmcache]]
## Whether to report per-device stats or not
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://raw.githubusercontent.com/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?

return "Provide a native collection for dmsetup based statistics for dm-cache"
}

func dmSetupStatus() ([]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 think this should go into dmcache_linux since it is linux specific.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine, how does one accomplish this on Windows (not a Windows user/fan).

return nil, parseError
}

status["device"] = strings.TrimRight(values[0], ":")
Copy link
Contributor

Choose a reason for hiding this comment

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

Still want these in a struct, this separates the responsibilities. Call it something appropriate that represents the data, maybe CacheStatus. I get why you did this but I think it will clear up some weirdness in the code, such as taking device from fields to add to tags. You won't need to do type conversions and summing will be more explicit. This open PR does a good job. #2031

"device": "cs-1",
}
fields1 := map[string]interface{}{
"device": "cs-1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Device shouldn't be both a tag and a field.

Copy link
Contributor

Choose a reason for hiding this comment

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

that is valid, in the actual output it's not a field but just a tag.

if err != nil {
return nil, err
}

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?

if err != nil {
return nil, err
}

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?

Copy link
Contributor

@sebito91 sebito91 left a comment

Choose a reason for hiding this comment

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

Appreciate the feedback on this, it's been a long while getting this collector on-boarded.

Some of the changes you mention are fair, but some of the others I'd have to disagree. In particular, using the dmsetup stats is separate altogether and requires its own configuration outside of a standard lvmcache entry. The data that comes back from the status command we've listed shows just the necessary data for the particular cache-disk.

https://www.kernel.org/doc/Documentation/device-mapper/cache.txt

[[inputs.dmcache]]
## Whether to report per-device stats or not
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.

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.

return "Provide a native collection for dmsetup based statistics for dm-cache"
}

func dmSetupStatus() ([]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.

This is fine, how does one accomplish this on Windows (not a Windows user/fan).

"device": "cs-1",
}
fields1 := map[string]interface{}{
"device": "cs-1",
Copy link
Contributor

Choose a reason for hiding this comment

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

that is valid, in the actual output it's not a field but just a tag.

if err != nil {
return nil, err
}

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?

@sebito91
Copy link
Contributor

sebito91 commented Apr 5, 2017

Here is some output to (hopefully) provide some clarity...

[root@hostname ~]# telegraf --test --config /etc/telegraf/telegraf.conf --input-filter dmcache
* Plugin: inputs.dmcache, Collection 1
> dmcache,bu=linux,cls=server,dc=colo,device=vg02-splunk_data_lv,env=production,host=hostname,sr=splunk_indexer,trd=false cache_free=0i,cache_used=1099511627776i,demotions=3265223720960i,dirty=0i,
metadata_free=1060880384i,metadata_used=12861440i,promotions=3265223720960i,read_hits=995134034411520i,read_misses=916807089127424i,write_hits=195107267543040i,write_misses=563725346013184i 1491362011000000000
[root@hostname ~]# dmsetup status --target=cache
vg02-splunk_data_lv: 0 70301769728 cache 8 3140/262144 2048 1048576/1048576 949033802 874335375 186072328 537610402 3113960 3113960 0 1 writethrough 2 migration_threshold 2048000 smq 0 rw -  

@sebito91
Copy link
Contributor

sebito91 commented Apr 5, 2017

@danielnelson, @sparrc we've made the proposed changes if you can please give it another look?

```
$ ./telegraf --test --config /etc/telegraf/telegraf.conf --input-filter dmcache
* Plugin: inputs.dmcache, Collection 1
> dmcache,bu=linux,cls=server,dc=colo,device=vg02-splunk_data_lv,env=production,host=hostname,sr=splunk_indexer,trd=false cache_free=0i,cache_used=1099511627776i,demotions=3265223720960i,dirty=0i,metadata_free=1060880384i,metadata_used=12861440i,promotions=3265223720960i,read_hits=995134034411520i,read_misses=916807089127424i,write_hits=195107267543040i,write_misses=563725346013184i 1491362011000000000
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 clean out the custom tags you have?

tags := map[string]string{"device": status.device}
acc.AddFields(metricName, fields, tags)
}
aggregateStats(total, fields)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

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

Just a minor style thing in the tests and then add this to the CHANGELOG.md.

getCurrentStatus: 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.

@danielnelson
Copy link
Contributor

I just noticed this, can you get it?
plugins/inputs/dmcache/dmcache.go:30: undefined: dmSetupStatus

@vlasad
Copy link
Contributor Author

vlasad commented Apr 7, 2017

I cannot get it.
dmSetupStatus is defined in plugins/inputs/dmcache/dmcache_linux.go:177

@danielnelson
Copy link
Contributor

On the non linux builds.

@danielnelson danielnelson merged commit aa722fa into influxdata:master Apr 7, 2017
@dswarbrick
Copy link

dswarbrick commented Apr 20, 2017

I was also working on a dmcache plugin for Telegraf, but put it aside whilst waiting for another plugin I wrote to be accepted, during what seems to have been quite a long moratorium on accepting new plugins.

I took a slightly different approach, and implemented the ioctls so that it does not require os.Exec(). However, it does mean that the telegraf process must be able to open /dev/mapper/control with O_RDWR. If people are interested, I could merge my approach into this plugin. See my unanswered comment last November in #1581.

@danielnelson
Copy link
Contributor

@dswarbrick I'll reply over on the issue.

@sebito91 sebito91 mentioned this pull request Apr 22, 2017
vlamug pushed a commit to vlamug/telegraf that referenced this pull request May 30, 2017
maxunt pushed a commit that referenced this pull request Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Requests for new plugin and for new features to existing plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants