Skip to content

Commit

Permalink
Adding rack and unit to snmp input
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Gorman committed Jun 21, 2017
1 parent e723f5d commit 33c9af3
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"

"github.com/soniah/gosnmp"
"github.com/soniah/gosnmp"

)

const description = `Retrieves SNMP values from remote agents`
Expand Down Expand Up @@ -45,6 +46,9 @@ const sampleConfig = `
## measurement name
name = "system"
[[inputs.snmp.top_tags]]
blah = "blerg"
[[inputs.snmp.field]]
name = "hostname"
oid = ".1.0.0.1.1"
Expand Down Expand Up @@ -354,14 +358,23 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
Name: s.Name,
Fields: s.Fields,
}

defaultTags := map[string]string{}
octets := strings.Split(agent, ".")
// validate that it's an ip and not localhost or a DNS name
if len(octets) == 4 {
defaultTags["rack"] = octets[2]
defaultTags["unit"] = octets[3]
}

topTags := map[string]string{}
if err := s.gatherTable(acc, gs, t, topTags, false); err != nil {
if err := s.gatherTable(acc, gs, t, defaultTags, 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 {
if err := s.gatherTable(acc, gs, t, defaultTags, topTags, true); err != nil {
acc.AddError(Errorf(err, "agent %s: gathering table %s", agent, t.Name))
}
}
Expand All @@ -370,7 +383,7 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
return nil
}

func (s *Snmp) gatherTable(acc telegraf.Accumulator, gs snmpConnection, t Table, topTags map[string]string, walk bool) error {
func (s *Snmp) gatherTable(acc telegraf.Accumulator, gs snmpConnection, t Table, defaultTags map[string]string, topTags map[string]string, walk bool) error {
rt, err := t.Build(gs, walk)
if err != nil {
return err
Expand All @@ -393,6 +406,9 @@ func (s *Snmp) gatherTable(acc telegraf.Accumulator, gs snmpConnection, t Table,
if _, ok := tr.Tags["agent_host"]; !ok {
tr.Tags["agent_host"] = gs.Host()
}
for k, v := range defaultTags {
tr.Tags[k] = v
}
acc.AddFields(rt.Name, tr.Fields, tr.Tags, rt.Time)
}

Expand Down

0 comments on commit 33c9af3

Please sign in to comment.