diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 9ac2f1035..c1a835985 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -4,10 +4,10 @@ package agent import "os" -func watchForFlushSignal(flushRequested chan os.Signal) { +func watchForFlushSignal(_ chan os.Signal) { // not supported } -func stopListeningForFlushSignal(flushRequested chan os.Signal) { +func stopListeningForFlushSignal(_ chan os.Signal) { // not supported } diff --git a/cmd/telegraf/telegraf_windows.go b/cmd/telegraf/telegraf_windows.go index f89690565..5b50484e8 100644 --- a/cmd/telegraf/telegraf_windows.go +++ b/cmd/telegraf/telegraf_windows.go @@ -75,7 +75,7 @@ type program struct { *Telegraf } -func (p *program) Start(s service.Service) error { +func (p *program) Start(_ service.Service) error { go func() { stop = make(chan struct{}) err := p.reloadLoop() @@ -87,14 +87,7 @@ func (p *program) Start(s service.Service) error { return nil } -func (p *program) run(errChan chan error) { - stop = make(chan struct{}) - err := p.reloadLoop() - errChan <- err - close(stop) -} - -func (p *program) Stop(s service.Service) error { +func (p *program) Stop(_ service.Service) error { var empty struct{} stop <- empty // signal reloadLoop to finish (context cancel) <-stop // wait for reloadLoop to finish and close channel diff --git a/logger/event_logger.go b/logger/event_logger.go index 965a57854..7f6877b5a 100644 --- a/logger/event_logger.go +++ b/logger/event_logger.go @@ -48,7 +48,7 @@ type eventLoggerCreator struct { logger *eventlog.Log } -func (e *eventLoggerCreator) CreateLogger(config LogConfig) (io.Writer, error) { +func (e *eventLoggerCreator) CreateLogger(_ LogConfig) (io.Writer, error) { return wlog.NewWriter(&eventLogger{logger: e.logger}), nil } diff --git a/plugins/inputs/diskio/README.md b/plugins/inputs/diskio/README.md index d67ab8167..c9573678d 100644 --- a/plugins/inputs/diskio/README.md +++ b/plugins/inputs/diskio/README.md @@ -15,7 +15,6 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ```toml @sample.conf # Read metrics about disk IO by device -# This plugin ONLY supports Linux [[inputs.diskio]] ## By default, telegraf will gather stats for all devices including ## disk partitions. diff --git a/plugins/inputs/diskio/sample.conf b/plugins/inputs/diskio/sample.conf index 76eb27dd6..99ff4aa71 100644 --- a/plugins/inputs/diskio/sample.conf +++ b/plugins/inputs/diskio/sample.conf @@ -1,5 +1,4 @@ # Read metrics about disk IO by device -# This plugin ONLY supports Linux [[inputs.diskio]] ## By default, telegraf will gather stats for all devices including ## disk partitions. diff --git a/plugins/inputs/execd/execd_windows.go b/plugins/inputs/execd/execd_windows.go index fc8d402f2..76ca84581 100644 --- a/plugins/inputs/execd/execd_windows.go +++ b/plugins/inputs/execd/execd_windows.go @@ -12,7 +12,7 @@ import ( "github.com/influxdata/telegraf" ) -func (e *Execd) Gather(acc telegraf.Accumulator) error { +func (e *Execd) Gather(_ telegraf.Accumulator) error { if e.process == nil { return nil } diff --git a/plugins/inputs/filecount/filesystem_helpers.go b/plugins/inputs/filecount/filesystem_helpers.go index f43bb4ad5..b39bccaa7 100644 --- a/plugins/inputs/filecount/filesystem_helpers.go +++ b/plugins/inputs/filecount/filesystem_helpers.go @@ -1,15 +1,13 @@ package filecount import ( - "errors" "io" "os" - "time" ) /* The code below is lifted from numerous articles and originates from Andrew Gerrand's 10 things you (probably) don't know about Go. - it allows for mocking a filesystem; this allows for consistent testing of this code across platforms (directory sizes reported + It allows for mocking a filesystem; this allows for consistent testing of this code across platforms (directory sizes reported differently by different platforms, for example), while preserving the rest of the functionality as-is, without modification. */ @@ -31,42 +29,3 @@ type osFS struct{} func (osFS) Open(name string) (file, error) { return os.Open(name) } func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } - -/* - The following are for mocking the filesystem - this allows us to mock Stat() files. This means that we can set file attributes, and know that they - will be the same regardless of the platform sitting underneath our tests (directory sizes vary see https://github.com/influxdata/telegraf/issues/6011) - - NOTE: still need the on-disk file structure to mirror this because the 3rd party library ("github.com/karrick/godirwalk") uses its own - walk functions, that we cannot mock from here. -*/ - -type fakeFileSystem struct { - files map[string]fakeFileInfo -} - -type fakeFileInfo struct { - name string - size int64 - filemode uint32 - modtime time.Time - isdir bool - sys interface{} -} - -func (f fakeFileInfo) Name() string { return f.name } -func (f fakeFileInfo) Size() int64 { return f.size } -func (f fakeFileInfo) Mode() os.FileMode { return os.FileMode(f.filemode) } -func (f fakeFileInfo) ModTime() time.Time { return f.modtime } -func (f fakeFileInfo) IsDir() bool { return f.isdir } -func (f fakeFileInfo) Sys() interface{} { return f.sys } - -func (f fakeFileSystem) Open(name string) (file, error) { - return nil, &os.PathError{Op: "Open", Path: name, Err: errors.New("Not implemented by fake filesystem")} -} - -func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) { - if fakeInfo, found := f.files[name]; found { - return fakeInfo, nil - } - return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("No such file or directory")} -} diff --git a/plugins/inputs/filecount/filesystem_helpers_notwindows.go b/plugins/inputs/filecount/filesystem_helpers_notwindows.go new file mode 100644 index 000000000..9a187d1a8 --- /dev/null +++ b/plugins/inputs/filecount/filesystem_helpers_notwindows.go @@ -0,0 +1,52 @@ +//go:build !windows + +// TODO: These types are not used in Windows tests because they are disabled for Windows. +// They can be moved to filesystem_helpers.go when following bug is fixed: +// https://github.com/influxdata/telegraf/issues/6248 + +package filecount + +import ( + "errors" + "os" + "time" +) + +/* + The following are for mocking the filesystem - this allows us to mock Stat() files. This means that we can set file attributes, and know that they + will be the same regardless of the platform sitting underneath our tests (directory sizes vary see https://github.com/influxdata/telegraf/issues/6011) + + NOTE: still need the on-disk file structure to mirror this because the 3rd party library ("github.com/karrick/godirwalk") uses its own + walk functions, that we cannot mock from here. +*/ + +type fakeFileSystem struct { + files map[string]fakeFileInfo +} + +type fakeFileInfo struct { + name string + size int64 + filemode uint32 + modtime time.Time + isdir bool + sys interface{} +} + +func (f fakeFileInfo) Name() string { return f.name } +func (f fakeFileInfo) Size() int64 { return f.size } +func (f fakeFileInfo) Mode() os.FileMode { return os.FileMode(f.filemode) } +func (f fakeFileInfo) ModTime() time.Time { return f.modtime } +func (f fakeFileInfo) IsDir() bool { return f.isdir } +func (f fakeFileInfo) Sys() interface{} { return f.sys } + +func (f fakeFileSystem) Open(name string) (file, error) { + return nil, &os.PathError{Op: "Open", Path: name, Err: errors.New("not implemented by fake filesystem")} +} + +func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) { + if fakeInfo, found := f.files[name]; found { + return fakeInfo, nil + } + return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("no such file or directory")} +} diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 8ef602276..2487d8056 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -70,7 +70,7 @@ type Ping struct { Binary string // Arguments for ping command. When arguments is not empty, system binary will be used and - // other options (ping_interval, timeout, etc) will be ignored + // other options (ping_interval, timeout, etc.) will be ignored Arguments []string // Whether to resolve addresses using ipv6 or not. @@ -88,20 +88,6 @@ type Ping struct { Size *int } -type roundTripTimeStats struct { - min float64 - avg float64 - max float64 - stddev float64 -} - -type stats struct { - trans int - recv int - ttl int - roundTripTimeStats -} - func (*Ping) SampleConfig() string { return sampleConfig } diff --git a/plugins/inputs/ping/ping_notwindows.go b/plugins/inputs/ping/ping_notwindows.go index 5b93aba60..1ba0026a6 100644 --- a/plugins/inputs/ping/ping_notwindows.go +++ b/plugins/inputs/ping/ping_notwindows.go @@ -15,6 +15,20 @@ import ( "github.com/influxdata/telegraf" ) +type roundTripTimeStats struct { + min float64 + avg float64 + max float64 + stddev float64 +} + +type statistics struct { + packetsTransmitted int + packetsReceived int + ttl int + roundTripTimeStats +} + func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) { tags := map[string]string{"url": u} fields := map[string]interface{}{"result_code": 0} @@ -67,11 +81,11 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) { } // Calculate packet loss percentage - loss := float64(stats.trans-stats.recv) / float64(stats.trans) * 100.0 + percentPacketLoss := float64(stats.packetsTransmitted-stats.packetsReceived) / float64(stats.packetsTransmitted) * 100.0 - fields["packets_transmitted"] = stats.trans - fields["packets_received"] = stats.recv - fields["percent_packet_loss"] = loss + fields["packets_transmitted"] = stats.packetsTransmitted + fields["packets_received"] = stats.packetsReceived + fields["percent_packet_loss"] = percentPacketLoss if stats.ttl >= 0 { fields["ttl"] = stats.ttl } @@ -165,11 +179,11 @@ func (p *Ping) args(url string, system string) []string { // round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms // // It returns (, , ) -func processPingOutput(out string) (stats, error) { - stats := stats{ - trans: 0, - recv: 0, - ttl: -1, +func processPingOutput(out string) (statistics, error) { + stats := statistics{ + packetsTransmitted: 0, + packetsReceived: 0, + ttl: -1, roundTripTimeStats: roundTripTimeStats{ min: -1.0, avg: -1.0, @@ -186,7 +200,7 @@ func processPingOutput(out string) (stats, error) { if stats.ttl == -1 && (strings.Contains(line, "ttl=") || strings.Contains(line, "hlim=")) { stats.ttl, err = getTTL(line) } else if strings.Contains(line, "transmitted") && strings.Contains(line, "received") { - stats.trans, stats.recv, err = getPacketStats(line) + stats.packetsTransmitted, stats.packetsReceived, err = getPacketStats(line) if err != nil { return stats, err } diff --git a/plugins/inputs/ping/ping_test.go b/plugins/inputs/ping/ping_test.go index 24a256bea..6b6fe61be 100644 --- a/plugins/inputs/ping/ping_test.go +++ b/plugins/inputs/ping/ping_test.go @@ -82,8 +82,8 @@ func TestProcessPingOutput(t *testing.T) { stats, err := processPingOutput(bsdPingOutput) require.NoError(t, err) require.Equal(t, 55, stats.ttl, "ttl value is 55") - require.Equal(t, 5, stats.trans, "5 packets were transmitted") - require.Equal(t, 5, stats.recv, "5 packets were received") + require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted") + require.Equal(t, 5, stats.packetsReceived, "5 packets were received") require.InDelta(t, 15.087, stats.min, 0.001) require.InDelta(t, 20.224, stats.avg, 0.001) require.InDelta(t, 27.263, stats.max, 0.001) @@ -92,8 +92,8 @@ func TestProcessPingOutput(t *testing.T) { stats, err = processPingOutput(freebsdPing6Output) require.NoError(t, err) require.Equal(t, 117, stats.ttl, "ttl value is 117") - require.Equal(t, 5, stats.trans, "5 packets were transmitted") - require.Equal(t, 5, stats.recv, "5 packets were received") + require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted") + require.Equal(t, 5, stats.packetsReceived, "5 packets were received") require.InDelta(t, 35.727, stats.min, 0.001) require.InDelta(t, 53.211, stats.avg, 0.001) require.InDelta(t, 93.870, stats.max, 0.001) @@ -102,8 +102,8 @@ func TestProcessPingOutput(t *testing.T) { stats, err = processPingOutput(linuxPingOutput) require.NoError(t, err) require.Equal(t, 63, stats.ttl, "ttl value is 63") - require.Equal(t, 5, stats.trans, "5 packets were transmitted") - require.Equal(t, 5, stats.recv, "5 packets were received") + require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted") + require.Equal(t, 5, stats.packetsReceived, "5 packets were received") require.InDelta(t, 35.225, stats.min, 0.001) require.InDelta(t, 43.628, stats.avg, 0.001) require.InDelta(t, 51.806, stats.max, 0.001) @@ -112,8 +112,8 @@ func TestProcessPingOutput(t *testing.T) { stats, err = processPingOutput(busyBoxPingOutput) require.NoError(t, err) require.Equal(t, 56, stats.ttl, "ttl value is 56") - require.Equal(t, 4, stats.trans, "4 packets were transmitted") - require.Equal(t, 4, stats.recv, "4 packets were received") + require.Equal(t, 4, stats.packetsTransmitted, "4 packets were transmitted") + require.Equal(t, 4, stats.packetsReceived, "4 packets were received") require.InDelta(t, 15.810, stats.min, 0.001) require.InDelta(t, 17.611, stats.avg, 0.001) require.InDelta(t, 22.559, stats.max, 0.001) @@ -139,8 +139,8 @@ func TestProcessPingOutputWithVaryingTTL(t *testing.T) { stats, err := processPingOutput(linuxPingOutputWithVaryingTTL) require.NoError(t, err) require.Equal(t, 63, stats.ttl, "ttl value is 63") - require.Equal(t, 5, stats.trans, "5 packets were transmitted") - require.Equal(t, 5, stats.recv, "5 packets were transmitted") + require.Equal(t, 5, stats.packetsTransmitted, "5 packets were transmitted") + require.Equal(t, 5, stats.packetsReceived, "5 packets were transmitted") require.InDelta(t, 35.225, stats.min, 0.001) require.InDelta(t, 43.628, stats.avg, 0.001) require.InDelta(t, 51.806, stats.max, 0.001) diff --git a/plugins/inputs/ping/ping_windows.go b/plugins/inputs/ping/ping_windows.go index ed400c331..2e9d595df 100644 --- a/plugins/inputs/ping/ping_windows.go +++ b/plugins/inputs/ping/ping_windows.go @@ -12,6 +12,19 @@ import ( "github.com/influxdata/telegraf" ) +type roundTripTimeStats struct { + min int + avg int + max int +} + +type statistics struct { + packetsTransmitted int + replyReceived int + packetsReceived int + roundTripTimeStats +} + func (p *Ping) pingToURL(host string, acc telegraf.Accumulator) { tags := map[string]string{"url": host} fields := map[string]interface{}{"result_code": 0} @@ -23,14 +36,13 @@ func (p *Ping) pingToURL(host string, acc telegraf.Accumulator) { } out, err := p.pingHost(p.Binary, totalTimeout, args...) - // ping host return exitcode != 0 also when there was no response from host - // but command was execute successfully + // ping host return exitcode != 0 also when there was no response from host but command was executed successfully var pendingError error if err != nil { // Combine go err + stderr output pendingError = errors.New(strings.TrimSpace(out) + ", " + err.Error()) } - trans, recReply, receivePacket, avg, min, max, err := processPingOutput(out) + stats, err := processPingOutput(out) if err != nil { // fatal error if pendingError != nil { @@ -45,22 +57,22 @@ func (p *Ping) pingToURL(host string, acc telegraf.Accumulator) { return } // Calculate packet loss percentage - lossReply := float64(trans-recReply) / float64(trans) * 100.0 - lossPackets := float64(trans-receivePacket) / float64(trans) * 100.0 + lossReply := float64(stats.packetsTransmitted-stats.replyReceived) / float64(stats.packetsTransmitted) * 100.0 + lossPackets := float64(stats.packetsTransmitted-stats.packetsReceived) / float64(stats.packetsTransmitted) * 100.0 - fields["packets_transmitted"] = trans - fields["reply_received"] = recReply - fields["packets_received"] = receivePacket + fields["packets_transmitted"] = stats.packetsTransmitted + fields["reply_received"] = stats.replyReceived + fields["packets_received"] = stats.packetsReceived fields["percent_packet_loss"] = lossPackets fields["percent_reply_loss"] = lossReply - if avg >= 0 { - fields["average_response_ms"] = float64(avg) + if stats.avg >= 0 { + fields["average_response_ms"] = float64(stats.avg) } - if min >= 0 { - fields["minimum_response_ms"] = float64(min) + if stats.min >= 0 { + fields["minimum_response_ms"] = float64(stats.min) } - if max >= 0 { - fields["maximum_response_ms"] = float64(max) + if stats.max >= 0 { + fields["maximum_response_ms"] = float64(stats.max) } acc.AddFields("ping", fields, tags) } @@ -83,61 +95,80 @@ func (p *Ping) args(url string) []string { } // processPingOutput takes in a string output from the ping command -// based on linux implementation but using regex ( multilanguage support ) +// based on linux implementation but using regex (multi-language support) // It returns (, , , , , ) -func processPingOutput(out string) (int, int, int, int, int, int, error) { +func processPingOutput(out string) (statistics, error) { // So find a line contain 3 numbers except reply lines - var stats, aproxs []string = nil, nil + var statsLine, aproxs []string = nil, nil err := errors.New("fatal error processing ping output") stat := regexp.MustCompile(`=\W*(\d+)\D*=\W*(\d+)\D*=\W*(\d+)`) aprox := regexp.MustCompile(`=\W*(\d+)\D*ms\D*=\W*(\d+)\D*ms\D*=\W*(\d+)\D*ms`) tttLine := regexp.MustCompile(`TTL=\d+`) lines := strings.Split(out, "\n") - var receivedReply int = 0 + var replyReceived = 0 for _, line := range lines { if tttLine.MatchString(line) { - receivedReply++ + replyReceived++ } else { - if stats == nil { - stats = stat.FindStringSubmatch(line) + if statsLine == nil { + statsLine = stat.FindStringSubmatch(line) } - if stats != nil && aproxs == nil { + if statsLine != nil && aproxs == nil { aproxs = aprox.FindStringSubmatch(line) } } } - // stats data should contain 4 members: entireExpression + ( Send, Receive, Lost ) - if len(stats) != 4 { - return 0, 0, 0, -1, -1, -1, err + stats := statistics{ + packetsTransmitted: 0, + replyReceived: 0, + packetsReceived: 0, + roundTripTimeStats: roundTripTimeStats{ + min: -1, + avg: -1, + max: -1, + }, } - trans, err := strconv.Atoi(stats[1]) + + // statsLine data should contain 4 members: entireExpression + ( Send, Receive, Lost ) + if len(statsLine) != 4 { + return stats, err + } + packetsTransmitted, err := strconv.Atoi(statsLine[1]) if err != nil { - return 0, 0, 0, -1, -1, -1, err + return stats, err } - receivedPacket, err := strconv.Atoi(stats[2]) + packetsReceived, err := strconv.Atoi(statsLine[2]) if err != nil { - return 0, 0, 0, -1, -1, -1, err + return stats, err } + stats.packetsTransmitted = packetsTransmitted + stats.replyReceived = replyReceived + stats.packetsReceived = packetsReceived + // aproxs data should contain 4 members: entireExpression + ( min, max, avg ) if len(aproxs) != 4 { - return trans, receivedReply, receivedPacket, -1, -1, -1, err + return stats, err } min, err := strconv.Atoi(aproxs[1]) if err != nil { - return trans, receivedReply, receivedPacket, -1, -1, -1, err + return stats, err } max, err := strconv.Atoi(aproxs[2]) if err != nil { - return trans, receivedReply, receivedPacket, -1, -1, -1, err + return stats, err } avg, err := strconv.Atoi(aproxs[3]) if err != nil { - return 0, 0, 0, -1, -1, -1, err + return statistics{}, err } - return trans, receivedReply, receivedPacket, avg, min, max, err + stats.avg = avg + stats.min = min + stats.max = max + + return stats, err } func (p *Ping) timeout() float64 { diff --git a/plugins/inputs/ping/ping_windows_test.go b/plugins/inputs/ping/ping_windows_test.go index 408321e3b..e92817b98 100644 --- a/plugins/inputs/ping/ping_windows_test.go +++ b/plugins/inputs/ping/ping_windows_test.go @@ -42,26 +42,26 @@ Approximate round trip times in milli-seconds: ` func TestHost(t *testing.T) { - trans, recReply, recPacket, avg, min, max, err := processPingOutput(winPLPingOutput) + stats, err := processPingOutput(winPLPingOutput) require.NoError(t, err) - require.Equal(t, 4, trans, "4 packets were transmitted") - require.Equal(t, 4, recReply, "4 packets were reply") - require.Equal(t, 4, recPacket, "4 packets were received") - require.Equal(t, 50, avg, "Average 50") - require.Equal(t, 46, min, "Min 46") - require.Equal(t, 57, max, "max 57") + require.Equal(t, 4, stats.packetsTransmitted, "4 packets were transmitted") + require.Equal(t, 4, stats.replyReceived, "4 packets were reply") + require.Equal(t, 4, stats.packetsReceived, "4 packets were received") + require.Equal(t, 50, stats.avg, "Average 50") + require.Equal(t, 46, stats.min, "Min 46") + require.Equal(t, 57, stats.max, "max 57") - trans, recReply, recPacket, avg, min, max, err = processPingOutput(winENPingOutput) + stats, err = processPingOutput(winENPingOutput) require.NoError(t, err) - require.Equal(t, 4, trans, "4 packets were transmitted") - require.Equal(t, 4, recReply, "4 packets were reply") - require.Equal(t, 4, recPacket, "4 packets were received") - require.Equal(t, 50, avg, "Average 50") - require.Equal(t, 50, min, "Min 50") - require.Equal(t, 52, max, "Max 52") + require.Equal(t, 4, stats.packetsTransmitted, "4 packets were transmitted") + require.Equal(t, 4, stats.replyReceived, "4 packets were reply") + require.Equal(t, 4, stats.packetsReceived, "4 packets were received") + require.Equal(t, 50, stats.avg, "Average 50") + require.Equal(t, 50, stats.min, "Min 50") + require.Equal(t, 52, stats.max, "Max 52") } -func mockHostPinger(binary string, timeout float64, args ...string) (string, error) { +func mockHostPinger(_ string, _ float64, _ ...string) (string, error) { return winENPingOutput, nil } @@ -104,7 +104,7 @@ Statystyka badania ping dla 195.187.242.157: (100% straty), ` -func mockErrorHostPinger(binary string, timeout float64, args ...string) (string, error) { +func mockErrorHostPinger(_ string, _ float64, _ ...string) (string, error) { return errorPingOutput, errors.New("No packets received") } @@ -165,7 +165,7 @@ Szacunkowy czas błądzenia pakietów w millisekundach: Minimum = 114 ms, Maksimum = 119 ms, Czas średni = 115 ms ` -func mockLossyHostPinger(binary string, timeout float64, args ...string) (string, error) { +func mockLossyHostPinger(_ string, _ float64, _ ...string) (string, error) { return lossyPingOutput, nil } @@ -228,7 +228,7 @@ Options: ` -func mockFatalHostPinger(binary string, timeout float64, args ...string) (string, error) { +func mockFatalHostPinger(_ string, _ float64, _ ...string) (string, error) { return fatalPingOutput, errors.New("So very bad") } @@ -273,7 +273,7 @@ Ping statistics for 8.8.8.8: Packets: Sent = 4, Received = 1, Lost = 3 (75% loss), ` -func mockUnreachableHostPinger(binary string, timeout float64, args ...string) (string, error) { +func mockUnreachableHostPinger(_ string, _ float64, _ ...string) (string, error) { return UnreachablePingOutput, errors.New("So very bad") } @@ -324,7 +324,7 @@ Ping statistics for 8.8.8.8: Packets: Sent = 4, Received = 1, Lost = 3 (75% loss), ` -func mockTTLExpiredPinger(binary string, timeout float64, args ...string) (string, error) { +func mockTTLExpiredPinger(_ string, _ float64, _ ...string) (string, error) { return TTLExpiredPingOutput, errors.New("So very bad") } diff --git a/plugins/inputs/postfix/stat_none.go b/plugins/inputs/postfix/stat_none.go deleted file mode 100644 index 07c82a92b..000000000 --- a/plugins/inputs/postfix/stat_none.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build !dragonfly && !linux && !netbsd && !openbsd && !solaris && !darwin && !freebsd - -package postfix - -import ( - "time" -) - -func statCTime(_ interface{}) time.Time { - return time.Time{} -} diff --git a/plugins/inputs/processes/processes_windows.go b/plugins/inputs/processes/processes_windows.go index 04e63034e..4287f2c41 100644 --- a/plugins/inputs/processes/processes_windows.go +++ b/plugins/inputs/processes/processes_windows.go @@ -16,7 +16,7 @@ func (e *Processes) Init() error { return nil } -func (e *Processes) Gather(acc telegraf.Accumulator) error { +func (e *Processes) Gather(_ telegraf.Accumulator) error { return nil } diff --git a/plugins/inputs/procstat/native_finder_windows_test.go b/plugins/inputs/procstat/native_finder_windows_test.go index f6068ac26..2a90344fa 100644 --- a/plugins/inputs/procstat/native_finder_windows_test.go +++ b/plugins/inputs/procstat/native_finder_windows_test.go @@ -36,11 +36,11 @@ func TestGather_RealUserIntegration(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode") } - user, err := user.Current() + currentUser, err := user.Current() require.NoError(t, err) pg, err := NewNativeFinder() require.NoError(t, err) - pids, err := pg.UID(user.Username) + pids, err := pg.UID(currentUser.Username) require.NoError(t, err) fmt.Println(pids) require.Equal(t, len(pids) > 0, true) diff --git a/plugins/inputs/win_eventlog/syscall_windows.go b/plugins/inputs/win_eventlog/syscall_windows.go index 28beea839..b29a6b168 100644 --- a/plugins/inputs/win_eventlog/syscall_windows.go +++ b/plugins/inputs/win_eventlog/syscall_windows.go @@ -34,9 +34,8 @@ type EvtRenderFlag uint32 // EVT_RENDER_FLAGS enumeration // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385563(v=vs.85).aspx const ( - // Render the event as an XML string. For details on the contents of the - // XML string, see the Event schema. - EvtRenderEventXml EvtRenderFlag = 1 + // Render the event as an XML string. For details on the contents of the XML string, see the Event schema. + EvtRenderEventXML EvtRenderFlag = 1 // Render bookmark EvtRenderBookmark EvtRenderFlag = 2 ) diff --git a/plugins/inputs/win_eventlog/util.go b/plugins/inputs/win_eventlog/util.go index 4f6477575..9c1e324f8 100644 --- a/plugins/inputs/win_eventlog/util.go +++ b/plugins/inputs/win_eventlog/util.go @@ -19,7 +19,6 @@ import ( // DecodeUTF16 to UTF8 bytes func DecodeUTF16(b []byte) ([]byte, error) { - if len(b)%2 != 0 { return nil, fmt.Errorf("must have even length byte slice") } @@ -42,28 +41,28 @@ func DecodeUTF16(b []byte) ([]byte, error) { } // GetFromSnapProcess finds information about process by the given pid -// Returns process parent pid, threads info handle and process name -func GetFromSnapProcess(pid uint32) (uint32, uint32, string, error) { +// Returns process name +func GetFromSnapProcess(pid uint32) (string, error) { snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, pid) if err != nil { - return 0, 0, "", err + return "", err } defer windows.CloseHandle(snap) var pe32 windows.ProcessEntry32 pe32.Size = uint32(unsafe.Sizeof(pe32)) //nolint:gosec // G103: Valid use of unsafe call to determine the size of the struct if err = windows.Process32First(snap, &pe32); err != nil { - return 0, 0, "", err + return "", err } for { if pe32.ProcessID == pid { szexe := windows.UTF16ToString(pe32.ExeFile[:]) - return pe32.ParentProcessID, pe32.Threads, szexe, nil + return szexe, nil } if err = windows.Process32Next(snap, &pe32); err != nil { break } } - return 0, 0, "", fmt.Errorf("couldn't find pid: %d", pid) + return "", fmt.Errorf("couldn't find pid: %d", pid) } type xmlnode struct { diff --git a/plugins/inputs/win_eventlog/win_eventlog.go b/plugins/inputs/win_eventlog/win_eventlog.go index e6bed8508..142bcf9ba 100644 --- a/plugins/inputs/win_eventlog/win_eventlog.go +++ b/plugins/inputs/win_eventlog/win_eventlog.go @@ -153,7 +153,7 @@ func (w *WinEventLog) Gather(acc telegraf.Accumulator) error { fieldName = "ProcessID" // Look up Process Name from pid if should, _ := w.shouldProcessField("ProcessName"); should { - _, _, processName, err := GetFromSnapProcess(fieldValue) + processName, err := GetFromSnapProcess(fieldValue) if err == nil { computedValues["ProcessName"] = processName } @@ -395,7 +395,7 @@ func (w *WinEventLog) renderEvent(eventHandle EvtHandle) (Event, error) { buf := make([]byte, bufferSize) event := Event{} - err := _EvtRender(0, eventHandle, EvtRenderEventXml, uint32(len(buf)), &buf[0], &bufferUsed, &propertyCount) + err := _EvtRender(0, eventHandle, EvtRenderEventXML, uint32(len(buf)), &buf[0], &bufferUsed, &propertyCount) if err != nil { return event, err } diff --git a/plugins/inputs/win_eventlog/zsyscall_windows.go b/plugins/inputs/win_eventlog/zsyscall_windows.go index 7172af6a4..eb836cd74 100644 --- a/plugins/inputs/win_eventlog/zsyscall_windows.go +++ b/plugins/inputs/win_eventlog/zsyscall_windows.go @@ -76,6 +76,7 @@ var ( procEvtUpdateBookmark = modwevtapi.NewProc("EvtUpdateBookmark") ) +//nolint:revive //argument-limit conditionally more arguments allowed func _EvtSubscribe( session EvtHandle, signalEvent uintptr, @@ -110,6 +111,7 @@ func _EvtSubscribe( return handle, err } +//nolint:revive //argument-limit conditionally more arguments allowed func _EvtRender( context EvtHandle, fragment EvtHandle, @@ -176,6 +178,7 @@ func _EvtNext(resultSet EvtHandle, eventArraySize uint32, eventArray *EvtHandle, return err } +//nolint:revive //argument-limit conditionally more arguments allowed func _EvtFormatMessage( publisherMetadata EvtHandle, event EvtHandle, diff --git a/plugins/inputs/win_services/win_services.go b/plugins/inputs/win_services/win_services.go index 6fa231351..05e00f463 100644 --- a/plugins/inputs/win_services/win_services.go +++ b/plugins/inputs/win_services/win_services.go @@ -81,9 +81,8 @@ func (rmr *MgProvider) Connect() (WinServiceManager, error) { scmgr, err := mgr.Connect() if err != nil { return nil, err - } else { - return &WinSvcMgr{scmgr}, nil } + return &WinSvcMgr{scmgr}, nil } // WinServices is an implementation if telegraf.Input interface, providing info about Windows Services diff --git a/plugins/inputs/win_services/win_services_test.go b/plugins/inputs/win_services/win_services_test.go index 3c8d1e33f..4305e6ec8 100644 --- a/plugins/inputs/win_services/win_services_test.go +++ b/plugins/inputs/win_services/win_services_test.go @@ -48,9 +48,8 @@ func (m *FakeSvcMgr) OpenService(name string) (WinService, error) { if s.serviceName == name { if s.serviceOpenError != nil { return nil, s.serviceOpenError - } else { - return &FakeWinSvc{s}, nil } + return &FakeWinSvc{s}, nil } } return nil, fmt.Errorf("cannot find service %q", name) @@ -59,9 +58,8 @@ func (m *FakeSvcMgr) OpenService(name string) (WinService, error) { func (m *FakeSvcMgr) ListServices() ([]string, error) { if m.testData.mgrListServicesError != nil { return nil, m.testData.mgrListServicesError - } else { - return m.testData.queryServiceList, nil } + return m.testData.queryServiceList, nil } type FakeMgProvider struct { @@ -71,9 +69,8 @@ type FakeMgProvider struct { func (m *FakeMgProvider) Connect() (WinServiceManager, error) { if m.testData.mgrConnectError != nil { return nil, m.testData.mgrConnectError - } else { - return &FakeSvcMgr{m.testData}, nil } + return &FakeSvcMgr{m.testData}, nil } type FakeWinSvc struct { @@ -86,33 +83,31 @@ func (m *FakeWinSvc) Close() error { func (m *FakeWinSvc) Config() (mgr.Config, error) { if m.testData.serviceConfigError != nil { return mgr.Config{}, m.testData.serviceConfigError - } else { - return mgr.Config{ - ServiceType: 0, - StartType: uint32(m.testData.startUpMode), - ErrorControl: 0, - BinaryPathName: "", - LoadOrderGroup: "", - TagId: 0, - Dependencies: nil, - ServiceStartName: m.testData.serviceName, - DisplayName: m.testData.displayName, - Password: "", - Description: "", - }, nil } + return mgr.Config{ + ServiceType: 0, + StartType: uint32(m.testData.startUpMode), + ErrorControl: 0, + BinaryPathName: "", + LoadOrderGroup: "", + TagId: 0, + Dependencies: nil, + ServiceStartName: m.testData.serviceName, + DisplayName: m.testData.displayName, + Password: "", + Description: "", + }, nil } func (m *FakeWinSvc) Query() (svc.Status, error) { if m.testData.serviceQueryError != nil { return svc.Status{}, m.testData.serviceQueryError - } else { - return svc.Status{ - State: svc.State(m.testData.state), - Accepts: 0, - CheckPoint: 0, - WaitHint: 0, - }, nil } + return svc.Status{ + State: svc.State(m.testData.state), + Accepts: 0, + CheckPoint: 0, + WaitHint: 0, + }, nil } var testErrors = []testData{ diff --git a/plugins/inputs/win_wmi/win_wmi.go b/plugins/inputs/win_wmi/win_wmi.go index 821beccf0..59f7acba8 100644 --- a/plugins/inputs/win_wmi/win_wmi.go +++ b/plugins/inputs/win_wmi/win_wmi.go @@ -106,9 +106,6 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - tags := map[string]string{} - fields := map[string]interface{}{} - // init COM if err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED); err != nil { var oleCode *ole.OleError @@ -161,48 +158,67 @@ func (q *Query) doQuery(acc telegraf.Accumulator) error { return fmt.Errorf("failed calling method ItemIndex: %w", err) } - item := itemRaw.ToIDispatch() - defer item.Release() - - for _, wmiProperty := range q.Properties { - prop, err := oleutil.GetProperty(item, wmiProperty) - if err != nil { - return fmt.Errorf("failed GetProperty: %w", err) - } - defer prop.Clear() - - if q.tagFilter.Match(wmiProperty) { - valStr, err := internal.ToString(prop.Value()) - if err != nil { - return fmt.Errorf("converting property %q failed: %w", wmiProperty, err) - } - tags[wmiProperty] = valStr - } else { - var fieldValue interface{} - switch v := prop.Value().(type) { - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: - fieldValue = v - case string: - fieldValue = v - case bool: - fieldValue = v - case []byte: - fieldValue = string(v) - case fmt.Stringer: - fieldValue = v.String() - case nil: - fieldValue = nil - default: - return fmt.Errorf("property %q of type \"%T\" unsupported", wmiProperty, v) - } - fields[wmiProperty] = fieldValue - } + err = q.extractProperties(itemRaw, acc) + if err != nil { + return err } - acc.AddFields(q.ClassName, fields, tags) } return nil } +func (q *Query) extractProperties(itemRaw *ole.VARIANT, acc telegraf.Accumulator) error { + tags, fields := map[string]string{}, map[string]interface{}{} + + item := itemRaw.ToIDispatch() + defer item.Release() + + for _, wmiProperty := range q.Properties { + propertyValue, err := getPropertyValue(item, wmiProperty) + if err != nil { + return err + } + + if q.tagFilter.Match(wmiProperty) { + valStr, err := internal.ToString(propertyValue) + if err != nil { + return fmt.Errorf("converting property %q failed: %w", wmiProperty, err) + } + tags[wmiProperty] = valStr + } else { + var fieldValue interface{} + switch v := propertyValue.(type) { + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + fieldValue = v + case string: + fieldValue = v + case bool: + fieldValue = v + case []byte: + fieldValue = string(v) + case fmt.Stringer: + fieldValue = v.String() + case nil: + fieldValue = nil + default: + return fmt.Errorf("property %q of type \"%T\" unsupported", wmiProperty, v) + } + fields[wmiProperty] = fieldValue + } + } + acc.AddFields(q.ClassName, fields, tags) + return nil +} + +func getPropertyValue(item *ole.IDispatch, wmiProperty string) (interface{}, error) { + prop, err := oleutil.GetProperty(item, wmiProperty) + if err != nil { + return nil, fmt.Errorf("failed GetProperty: %w", err) + } + defer prop.Clear() + + return prop.Value(), nil +} + // Gather function func (s *Wmi) Gather(acc telegraf.Accumulator) error { var wg sync.WaitGroup diff --git a/plugins/inputs/win_wmi/win_wmi_test.go b/plugins/inputs/win_wmi/win_wmi_test.go index 5ebf29954..18b9c2c76 100644 --- a/plugins/inputs/win_wmi/win_wmi_test.go +++ b/plugins/inputs/win_wmi/win_wmi_test.go @@ -17,7 +17,7 @@ import ( var sysDrive = fmt.Sprintf(`%s\`, os.Getenv("SystemDrive")) // C:\ // include Name as a tag, FreeSpace as a field, and Purpose as a known-null class property -var testQuery Query = Query{ +var testQuery = Query{ Namespace: "ROOT\\cimv2", ClassName: "Win32_Volume", Properties: []string{"Name", "FreeSpace", "Purpose"},