From 9feb639bbd20cfd09ead382e178614769e8f5e2f Mon Sep 17 00:00:00 2001
From: Patrick Hemmer <phemmer@users.noreply.github.com>
Date: Tue, 4 Oct 2016 11:22:15 -0400
Subject: [PATCH] fix translating snmp fields not in MIB (#1846)

---
 CHANGELOG.md                               |  1 +
 plugins/inputs/snmp/snmp.go                | 13 +++++++++----
 plugins/inputs/snmp/snmp_mocks_generate.go |  2 ++
 plugins/inputs/snmp/snmp_mocks_test.go     |  2 ++
 plugins/inputs/snmp/snmp_test.go           |  2 ++
 5 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 00112391..e297ae25 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,7 @@
 - [#1772](https://github.com/influxdata/telegraf/pull/1772): Windows remote management interactive service fix.
 - [#1702](https://github.com/influxdata/telegraf/issues/1702): sqlserver, fix issue when case sensitive collation is activated.
 - [#1823](https://github.com/influxdata/telegraf/issues/1823): Fix huge allocations in http_listener when dealing with huge payloads.
+- [#1833](https://github.com/influxdata/telegraf/issues/1833): Fix translating SNMP fields not in MIB.
 
 ## v1.0.1 [unreleased]
 
diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go
index b563fbbf..f83a4440 100644
--- a/plugins/inputs/snmp/snmp.go
+++ b/plugins/inputs/snmp/snmp.go
@@ -850,11 +850,16 @@ func snmpTranslate(oid string) (mibName string, oidNum string, oidText string, c
 
 	i := strings.Index(oidText, "::")
 	if i == -1 {
-		// was not found in MIB. Value is numeric
-		return "", oidText, oidText, "", nil
+		// was not found in MIB.
+		if bytes.Index(bb.Bytes(), []byte(" [TRUNCATED]")) >= 0 {
+			return "", oid, oid, "", nil
+		}
+		// not truncated, but not fully found. We still need to parse out numeric OID, so keep going
+		oidText = oid
+	} else {
+		mibName = oidText[:i]
+		oidText = oidText[i+2:]
 	}
-	mibName = oidText[:i]
-	oidText = oidText[i+2:]
 
 	if i := bytes.Index(bb.Bytes(), []byte("  -- TEXTUAL CONVENTION ")); i != -1 {
 		bb.Next(i + len("  -- TEXTUAL CONVENTION "))
diff --git a/plugins/inputs/snmp/snmp_mocks_generate.go b/plugins/inputs/snmp/snmp_mocks_generate.go
index 7bf8270a..59025798 100644
--- a/plugins/inputs/snmp/snmp_mocks_generate.go
+++ b/plugins/inputs/snmp/snmp_mocks_generate.go
@@ -21,6 +21,8 @@ var mockedCommands = [][]string{
 	{"snmptranslate", "-Td", "-Ob", "-m", "all", "1.0.0.1.1"},
 	{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.1"},
 	{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.1.0"},
+	{"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.2.3"},
+	{"snmptranslate", "-Td", "-Ob", ".iso.2.3"},
 	{"snmptranslate", "-Td", "-Ob", "-m", "all", ".999"},
 	{"snmptranslate", "-Td", "-Ob", "TEST::server"},
 	{"snmptranslate", "-Td", "-Ob", "TEST::server.0"},
diff --git a/plugins/inputs/snmp/snmp_mocks_test.go b/plugins/inputs/snmp/snmp_mocks_test.go
index f2f551e6..1c41f29e 100644
--- a/plugins/inputs/snmp/snmp_mocks_test.go
+++ b/plugins/inputs/snmp/snmp_mocks_test.go
@@ -67,6 +67,8 @@ var mockedCommandResults = map[string]mockedCommandResult{
 	"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x001.0.0.1.1":                   mockedCommandResult{stdout: "TEST::hostname\nhostname OBJECT-TYPE\n  -- FROM\tTEST\n  SYNTAX\tOCTET STRING\n  MAX-ACCESS\tread-only\n  STATUS\tcurrent\n::= { iso(1) 0 testOID(0) 1 1 }\n", stderr: "", exitError: false},
 	"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.1":                mockedCommandResult{stdout: "TEST::server\nserver OBJECT-TYPE\n  -- FROM\tTEST\n  SYNTAX\tOCTET STRING\n  MAX-ACCESS\tread-only\n  STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 1 }\n", stderr: "", exitError: false},
 	"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.1.0":              mockedCommandResult{stdout: "TEST::server.0\nserver OBJECT-TYPE\n  -- FROM\tTEST\n  SYNTAX\tOCTET STRING\n  MAX-ACCESS\tread-only\n  STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) server(1) 0 }\n", stderr: "", exitError: false},
+	"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.2.3":                      mockedCommandResult{stdout: "iso.2.3\niso OBJECT-TYPE\n  -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false},
+	"snmptranslate\x00-Td\x00-Ob\x00.iso.2.3":                                 mockedCommandResult{stdout: "iso.2.3\niso OBJECT-TYPE\n  -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false},
 	"snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.999":                        mockedCommandResult{stdout: ".999\n [TRUNCATED]\n", stderr: "", exitError: false},
 	"snmptranslate\x00-Td\x00-Ob\x00TEST::server":                             mockedCommandResult{stdout: "TEST::server\nserver OBJECT-TYPE\n  -- FROM\tTEST\n  SYNTAX\tOCTET STRING\n  MAX-ACCESS\tread-only\n  STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 1 }\n", stderr: "", exitError: false},
 	"snmptranslate\x00-Td\x00-Ob\x00TEST::server.0":                           mockedCommandResult{stdout: "TEST::server.0\nserver OBJECT-TYPE\n  -- FROM\tTEST\n  SYNTAX\tOCTET STRING\n  MAX-ACCESS\tread-only\n  STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) server(1) 0 }\n", stderr: "", exitError: false},
diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go
index 45ba7681..9dbdb43e 100644
--- a/plugins/inputs/snmp/snmp_test.go
+++ b/plugins/inputs/snmp/snmp_test.go
@@ -128,6 +128,8 @@ func TestFieldInit(t *testing.T) {
 		expectedName       string
 		expectedConversion string
 	}{
+		{".1.2.3", "foo", "", ".1.2.3", "foo", ""},
+		{".iso.2.3", "foo", "", ".1.2.3", "foo", ""},
 		{".1.0.0.0.1.1", "", "", ".1.0.0.0.1.1", "server", ""},
 		{".1.0.0.0.1.1.0", "", "", ".1.0.0.0.1.1.0", "server.0", ""},
 		{".999", "", "", ".999", ".999", ""},
-- 
GitLab