-
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 4 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,51 @@ | ||
package dmcache | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
type DMCache struct { | ||
PerDevice bool `toml:"per_device"` | ||
getCurrentStatus func() ([]string, error) | ||
} | ||
|
||
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 dmSetupStatus() ([]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 think this should go into dmcache_linux since it is linux specific. 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 is fine, how does one accomplish this on Windows (not a Windows user/fan). |
||
out, err := exec.Command("/bin/sh", "-c", "sudo /sbin/dmsetup status --target cache").Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if string(out) == "No devices found\n" { | ||
return []string{}, nil | ||
} | ||
|
||
outString := strings.TrimRight(string(out), "\n") | ||
status := strings.Split(outString, "\n") | ||
|
||
return status, nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("dmcache", func() telegraf.Input { | ||
return &DMCache{ | ||
PerDevice: true, | ||
getCurrentStatus: dmSetupStatus, | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// +build linux | ||
|
||
package dmcache | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"errors" | ||
|
||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
const metricName = "dmcache" | ||
|
||
func (c *DMCache) Gather(acc telegraf.Accumulator) error { | ||
outputLines, err := c.getCurrentStatus() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
total := make(map[string]interface{}) | ||
|
||
for _, s := range outputLines { | ||
fields, err := parseDMSetupStatus(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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": fields["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 | ||
parseError := errors.New("Output from dmsetup could not be parsed") | ||
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, parseError | ||
} | ||
|
||
status["device"] = strings.TrimRight(values[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. 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 |
||
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 | ||
} | ||
metadata := strings.Split(values[5], "/") | ||
if len(metadata) != 2 { | ||
return nil, parseError | ||
} | ||
status["metadata_used"], err = strconv.Atoi(metadata[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["metadata_total"], err = strconv.Atoi(metadata[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_blocksize"], err = strconv.Atoi(values[6]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cache := strings.Split(values[7], "/") | ||
if len(cache) != 2 { | ||
return nil, parseError | ||
} | ||
status["cache_used"], err = strconv.Atoi(cache[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
status["cache_total"], err = strconv.Atoi(cache[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 | ||
} | ||
|
||
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 aggregateStats(total, fields map[string]interface{}) { | ||
for key, value := range fields { | ||
if _, ok := value.(int); ok { | ||
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 | ||
} |
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://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?
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?