From f309f9e4768dc83d6a492a01147fa83f2ad587c5 Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Thu, 14 Sep 2023 10:10:31 -0600 Subject: [PATCH 1/4] fix: debug for win_wmi input plugin --- go.mod | 2 +- go.sum | 3 ++- plugins/inputs/win_wmi/win_wmi.go | 8 ++++++++ plugins/inputs/win_wmi/win_wmi_test.go | 1 + 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index b951cad018ae0..dee9e51c5edd8 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/fatih/color v1.15.0 github.com/go-ldap/ldap/v3 v3.4.5 github.com/go-logfmt/logfmt v0.6.0 - github.com/go-ole/go-ole v1.2.6 + github.com/go-ole/go-ole v1.3.0 github.com/go-redis/redis/v7 v7.4.1 github.com/go-redis/redis/v8 v8.11.5 github.com/go-sql-driver/mysql v1.7.1 diff --git a/go.sum b/go.sum index a57a1b85b03b4..4a97705cbce22 100644 --- a/go.sum +++ b/go.sum @@ -563,8 +563,9 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-macaroon-bakery/macaroonpb v1.0.0 h1:It9exBaRMZ9iix1iJ6gwzfwsDE6ExNuwtAJ9e09v6XE= github.com/go-macaroon-bakery/macaroonpb v1.0.0/go.mod h1:UzrGOcbiwTXISFP2XDLDPjfhMINZa+fX/7A2lMd31zc= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= github.com/go-openapi/jsonpointer v0.20.0 h1:ESKJdU9ASRfaPNOPRx12IUyA1vn3R9GiE3KYD14BXdQ= github.com/go-openapi/jsonpointer v0.20.0/go.mod h1:6PGzBjjIIumbLYysB73Klnms1mwnU4G3YHOECG3CedA= diff --git a/plugins/inputs/win_wmi/win_wmi.go b/plugins/inputs/win_wmi/win_wmi.go index 59f7acba8efd8..a096a068e5090 100644 --- a/plugins/inputs/win_wmi/win_wmi.go +++ b/plugins/inputs/win_wmi/win_wmi.go @@ -32,6 +32,8 @@ type Query struct { Filter string `toml:"filter"` TagPropertiesInclude []string `toml:"tag_properties"` tagFilter filter.Filter + + Log telegraf.Logger } // Wmi struct @@ -139,6 +141,7 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { defer serviceRaw.Clear() // result is a SWBemObjectSet + q.Log.Infof("running query: %s\n", q.query) resultRaw, err := oleutil.CallMethod(service, "ExecQuery", q.query) if err != nil { return fmt.Errorf("failed calling method ExecQuery for query %s: %w", q.query, err) @@ -151,6 +154,11 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { return fmt.Errorf("failed getting Count: %w", err) } + q.Log.Info("getting count property:") + v, ok := result.GetProperty("Count") + q.Log.Info(ok) + q.Log.Info(v) + for i := int64(0); i < count; i++ { // item is a SWbemObject itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i) diff --git a/plugins/inputs/win_wmi/win_wmi_test.go b/plugins/inputs/win_wmi/win_wmi_test.go index 175268aa45db6..1e3ee35d902c6 100644 --- a/plugins/inputs/win_wmi/win_wmi_test.go +++ b/plugins/inputs/win_wmi/win_wmi_test.go @@ -24,6 +24,7 @@ var testQuery = Query{ //nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function Filter: fmt.Sprintf(`NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`, regexp.QuoteMeta(sysDrive)), TagPropertiesInclude: []string{"Name"}, + Log: new(testutil.Logger), tagFilter: nil, // this is filled in by CompileInputs() } From 1dfed4a2ee70e3e9ac6ea3502143dc8db08c715c Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Wed, 20 Sep 2023 07:17:14 -0600 Subject: [PATCH 2/4] additional logging --- plugins/inputs/win_wmi/win_wmi.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/win_wmi/win_wmi.go b/plugins/inputs/win_wmi/win_wmi.go index a096a068e5090..63785ef98ab1b 100644 --- a/plugins/inputs/win_wmi/win_wmi.go +++ b/plugins/inputs/win_wmi/win_wmi.go @@ -149,16 +149,18 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { result := resultRaw.ToIDispatch() defer resultRaw.Clear() - count, err := oleInt64(result, "Count") - if err != nil { - return fmt.Errorf("failed getting Count: %w", err) - } + q.Log.Info(result) q.Log.Info("getting count property:") v, ok := result.GetProperty("Count") q.Log.Info(ok) q.Log.Info(v) + count, err := oleInt64(result, "Count") + if err != nil { + return fmt.Errorf("failed getting Count: %w", err) + } + for i := int64(0); i < count; i++ { // item is a SWbemObject itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i) From 765248b23536c3a2af3e54ff726e210d3a2da442 Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Wed, 20 Sep 2023 08:06:18 -0600 Subject: [PATCH 3/4] more loggin --- plugins/inputs/win_wmi/win_wmi.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/win_wmi/win_wmi.go b/plugins/inputs/win_wmi/win_wmi.go index 63785ef98ab1b..acb0c477f8208 100644 --- a/plugins/inputs/win_wmi/win_wmi.go +++ b/plugins/inputs/win_wmi/win_wmi.go @@ -17,6 +17,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/filter" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/models" "github.com/influxdata/telegraf/plugins/inputs" ) @@ -141,6 +142,7 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { defer serviceRaw.Clear() // result is a SWBemObjectSet + q.Log = models.NewLogger("input", "query", "") q.Log.Infof("running query: %s\n", q.query) resultRaw, err := oleutil.CallMethod(service, "ExecQuery", q.query) if err != nil { @@ -149,12 +151,10 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { result := resultRaw.ToIDispatch() defer resultRaw.Clear() - q.Log.Info(result) - q.Log.Info("getting count property:") v, ok := result.GetProperty("Count") - q.Log.Info(ok) - q.Log.Info(v) + q.Log.Infof(" - error: %s\n", ok) + q.Log.Infof(" - value: %v\n", v) count, err := oleInt64(result, "Count") if err != nil { From 3e4b01ee9bc70f59158b228077c36bbbd697815d Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Thu, 28 Sep 2023 08:32:13 -0600 Subject: [PATCH 4/4] print raw result --- plugins/inputs/win_wmi/win_wmi.go | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/inputs/win_wmi/win_wmi.go b/plugins/inputs/win_wmi/win_wmi.go index acb0c477f8208..fbc58e82f8895 100644 --- a/plugins/inputs/win_wmi/win_wmi.go +++ b/plugins/inputs/win_wmi/win_wmi.go @@ -148,6 +148,7 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { if err != nil { return fmt.Errorf("failed calling method ExecQuery for query %s: %w", q.query, err) } + q.Log.Infof("raw result: %s\n", resultRaw.ToString()) result := resultRaw.ToIDispatch() defer resultRaw.Clear()