New snmp plugin a bit slow
I have a few problems with performance of the new SNMP plugin.
When doing snmpwalk of EtherLike-MIB::dot3StatsTable and IF-MIB::ifXTable and IF-MIB::ifTable on a Cisco router they complete in ~2, ~3 and ~3.3 seconds respectively (8.3 sec combined +/- 10%).
When polling with the snmp plugin it takes 17-19 seconds for a single run.
I'm unsure if the snmp plugins polls every host in parallel or in sequence. I only have one host to test against and even when I put each of the three tables in separate [[inputs.snmp]] sections they are polled sequentially and not in parallel.
Our needs are polling hundreds of devices with hundreds of interfaces every 5 or 10 seconds (which collectd and libsnmp does easily).
How many records do you have in those tables?
It is true that the plugin doesn't do multiple agents in parallel, but the old one didn't either. Did the old version of the plugin perform faster? Or did you not use it? Doing multiple agents in parallel would be rather easy to implement. It also might be possible to do some parallelization for multiple fields/tables within a host (without making the code stupidly complex), but this will be a little challenging due to limitations with the snmp library the plugin uses. But that said, I'd be interested in increasing performance in serial runs before trying to parallelize. Parallelization would just hide the underlying issue without fixing it.
Not many. 6 interfaces only. I never tried the old plugin since I saw there was a new one around the corner.
I agree that increasing serial performance is important to be able to query a single host/table fast enough. But at some point I think parallelizing would become necessary to be able to query enough hosts within the allotted interval. Of course a workaround would be to split up the config and run dozens of telegrafs simultaneous.
Hrm, is this a wan link then? I'm just trying to figure out why it would be slow. Like even 2-3 seconds for snmpwalk is slow. I was just assuming it was due to massive amounts of data.
Oh, I'm not saying we shouldn't do parallelization, just that fixing the serial performance should be prioritized.
Agreed.
Yes, this is over a WAN link so that is why even snmpwalk is rather slow.
Thanks, I'll look into simulating a high latency link and getting the performance on par with the net-snmp tools.
Great. I will hopefully have access to the low-latency environment where we will be using it next week and give you some performance numbers from there as soon as I can.
I've done some experimentation, and while I'm not sure how snmpwalk is faster than this plugin, I do have a few ideas which might speed things up for you. Try setting these parameters:
max_repetitions = 10
timeout = "10s"
retries = 1
These settings should work better than the defaults on a high latency link. You might also be able to tweak them some more to get even better performance for your specific link. And changing the timeout does have a performance impact, as a retry is sent every $timeout / ( $retries + 1 ).
However I do have some code change ideas to speed things up which I'm trying out right now.
@jwilder I wouldn't consider this a bug. Everything works as it's supposed to. This is just a request to make it faster. Nor is more info needed. Thanks :-)
My case will of course be atypical from most, but I'm polling roughly ~600 clients at a time and pulling maybe 3-4 tables and a few odd OIDs. The Plugin is completely too slow to accomplish this. I've had to fall back to a suite of BASH scripts making forked snmpget/snmptable calls to make up the difference.
Just for a comparison between the two, I'm using BASH to call snmptable on 2 tables with roughly 8 columns each as well as pulling down 7 OIDs using snmpget for 10 hosts. It's pulled together into InfluxDB line protocol and echoed back. Unfortunately I can't release the data being pulled but I could potentially release the code being called if interested.
# /usr/bin/time telegraf -input-filter exec -test
<redacted>
1.60user 0.17system 0:00.62elapsed 285%CPU (0avgtext+0avgdata 14924maxresident)k
0inputs+0outputs (0major+253427minor)pagefaults 0swaps
Using the plugin to do exactly the same: My Config:
# /usr /bin/time telegraf -input-plugin snmp -test > /dev/null
<redacted>
28.02user 0.31system 0:28.42elapsed 99%CPU (0avgtext+0avgdata 19604maxresident)k
0inputs+0outputs (0major+5607minor)pagefaults 0swaps
When I look through the plugin code, I see some attempts to use an SNMP library for some calls but then the much faster C built utilities in Linux are used as well. If the goal was to limit dependencies, it didn't work. Not to mention, the SNMP project seems to be relatively in it's infancy and probably not well suited for production collection.
A lot of the slow downs in the code are caused by executing all operations serially. Why are channels/parallel functions not being used?
I was able to get an improvement of almost 1/3 of the time simply by parallelizing that first piece of agent code in the Gather() function.
# /root/go/bin/telegraf -config /root/go/bin/telegraf.snmp -test
* Plugin: inputs.snmp, Collection 1
<redacted>
31.80user 0.12system 0:09.51elapsed 335%CPU (0avgtext+0avgdata 25592maxresident)k
0inputs+0outputs (0major+26600minor)pagefaults 0swaps
Code that I changed:
# git diff master snmpTest
diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go
index cc750e7..3cac1fa 100644
--- a/plugins/inputs/snmp/snmp.go
+++ b/plugins/inputs/snmp/snmp.go
@@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"time"
+ "sync"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
@@ -372,6 +373,33 @@ func (s *Snmp) Description() string {
return description
}
+func(s *Snmp) cleanGather(acc telegraf.Accumulator, agent string, wg *sync.WaitGroup) error {
+ defer wg.Done()
+ gs, err := s.getConnection(agent)
+ if err != nil {
+ acc.AddError(Errorf(err, "agent %s", agent))
+ return nil
+ }
+
+ // First is the top-level fields. We treat the fields as table prefixes with an empty index.
+ t := Table{
+ Name: s.Name,
+ Fields: s.Fields,
+ }
+ topTags := map[string]string{}
+ if err := s.gatherTable(acc, gs, t, topTags, false); err != nil {
+ acc.AddError(Errorf(err, "agent %s", agent))
+ }
+
+ // Now is the real tables.
+ for _, t := range s.Tables {
+ if err := s.gatherTable(acc, gs, t, topTags, true); err != nil {
+ acc.AddError(Errorf(err, "agent %s", agent))
+ }
+ }
+ return nil
+}
+
// Gather retrieves all the configured fields and tables.
// Any error encountered does not halt the process. The errors are accumulated
// and returned at the end.
@@ -380,30 +408,12 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
return err
}
+ var wg sync.WaitGroup
for _, agent := range s.Agents {
- gs, err := s.getConnection(agent)
- if err != nil {
- acc.AddError(Errorf(err, "agent %s", agent))
- continue
- }
-
- // First is the top-level fields. We treat the fields as table prefixes with an empty index.
- t := Table{
- Name: s.Name,
- Fields: s.Fields,
- }
- topTags := map[string]string{}
- if err := s.gatherTable(acc, gs, t, topTags, false); err != nil {
- acc.AddError(Errorf(err, "agent %s", agent))
- }
-
- // Now is the real tables.
- for _, t := range s.Tables {
- if err := s.gatherTable(acc, gs, t, topTags, true); err != nil {
- acc.AddError(Errorf(err, "agent %s", agent))
- }
- }
+ wg.Add(1)
+ go s.cleanGather(acc,agent,&wg)
}
+ wg.Wait()
return nil
}
A lot of the slow downs in the code are caused by executing all operations serially. Why are channels/parallel functions not being used?
Because the underlying gosnmp library does not support it. We would have to spawn of dozens of copies of it to achieve parallelism. And doing so in such a manner that is controllable is difficult. We'd basically have to create a pool. I've attempted to make the gosnmp library able to handle parallel requests, but design issues in the library have made this very difficult.
@phemmer Seems we're both looking at this. See my obviously quick + dirty test code above. We don't need to necessarily parallelize the gosnmp library but all the calls that are happening serially and being waited on.
You code will cause problems because you are reusing the same gosnmp object. It is not parallel safe. Doing so will result in receive errors.
Posting the full code this time instead of the diffs... but no, I'm instantiating a separate gosnmp object in each parallel call:
func(s *Snmp) cleanGather(acc telegraf.Accumulator, agent string, wg *sync.WaitGroup) error {
defer wg.Done()
gs, err := s.getConnection(agent)
if err != nil {
acc.AddError(Errorf(err, "agent %s", agent))
return nil
}
// First is the top-level fields. We treat the fields as table prefixes with an empty index.
t := Table{
Name: s.Name,
Fields: s.Fields,
}
topTags := map[string]string{}
if err := s.gatherTable(acc, gs, t, topTags, false); err != nil {
acc.AddError(Errorf(err, "agent %s", agent))
}
// Now is the real tables.
for _, t := range s.Tables {
if err := s.gatherTable(acc, gs, t, topTags, true); err != nil {
acc.AddError(Errorf(err, "agent %s", agent))
}
}
return nil
}
// Gather retrieves all the configured fields and tables.
// Any error encountered does not halt the process. The errors are accumulated
// and returned at the end.
func (s *Snmp) Gather(acc telegraf.Accumulator) error {
if err := s.init(); err != nil {
return err
}
var wg sync.WaitGroup
for _, agent := range s.Agents {
wg.Add(1)
go s.cleanGather(acc,agent,&wg)
}
wg.Wait()
return nil
}
Yes, that should in theory not cause any problems. But it is not how I would recommend addressing the issue. Much better results can be obtained by sending multiple simultaneous requests per-agent. For people requesting a large number of OIDs from one agent, your change won't help. The only way to send parallel requests per agent is to either create multiple gosnmp objects, or fix the gosnmp library so it's parallel safe. The latter is a much better solution as it scales far better than a pool.
Agreed, but I'm on the scale of using ~600 agents so parallelizing this makes a huge difference.
One question, why are you using this gosnmp library? The code makes calls to net-snmp-utils programs already for snmptranslate/snmptable/etc, why not just use them throughout? Making parallel calls to these programs would be parallel safe.
The only reason I can think to stay with the gosnmp library would be to reduce dependencies however the dependencies are already implicit by using the aforementioned programs.
The code makes calls to net-snmp-utils programs already for snmptranslate/snmptable/etc, why not just use them throughout?
These utilities are optional. They add additional functionality to the plugin, but the plugin does not require them. They are basically just used for parsing MIB files. But yes, the ultimate reason is so that telegraf can be used without having to install external dependencies.
Hi @StianOvrevage we are working in a snmp colector tool for influxdb that has a good behaviour with lots of metrics.
Its different from telegraph because it is focused only on snmp devicss and It has also a web-ui interface which help us to configure in a easy way.
Perhaps would you like to test its performance.
https://github.com/toni-moreno/snmpcollector
Thank you and sorry for the spam
@toni-moreno Awesome! I will have a look at it when I have time. I would love to give you some feedback and performance numbers from real-world testing at a few different setups I have available.
@phemmer we have a customer who is polling hundreds of SNMP agents. Like @Will-Beninger, parallelizing just the agent connections would make a huge difference in performance.
Currently they workaround this by simply creating a different [[inputs.snmp]] instance for each agent, but it's a bit unwieldly because the config file is hundreds of thousands of lines long, and it has also created problems because it means that each instance independently tries to translate/lookup OIDs.
If you think that @Will-Beninger's change could help with multiple agents without causing parallel access issues, I'd like to get it into a PR.
@Will-Beninger do you think you could submit a PR with the diff that you've copied above?
If you think that @Will-Beninger's change could help with multiple agents without causing parallel access issues, I'd like to get it into a PR.
If we want to put that in as a temporary solution until the underlying gosnmp issues are fixed, I would be ok with that. Just as long as we rip it out once gosnmp is parallelized.
~Just note that it is kinda dangerous in large scale. Because each instance of the snmp object consumes memory, launching many hundreds of them could result in telegraf exhausting the box's memory. It could also result in running out of file descriptors as is the issue in #2104 since each snmp object instance would consume a file descriptor.~
Edit: Nevermind, brain not working right since I just woke up. I don't think it would result in any more resource utilization. I'd have to look at it in detail, but the resources would still be allocated, this just causes them to be used at the same time instead of serially.