From da7b5494540fe71e07e648c631231743d4521f6a Mon Sep 17 00:00:00 2001 From: rahulguptajss Date: Wed, 21 Aug 2024 13:21:32 +0530 Subject: [PATCH] build: fix lint issues --- cmd/poller/collector/asup.go | 16 +++++++++++----- cmd/poller/collector/collector.go | 2 +- cmd/poller/exporter/exporter.go | 2 +- cmd/poller/poller.go | 8 +++++++- pkg/color/color.go | 3 ++- pkg/util/util.go | 8 ++++++++ 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cmd/poller/collector/asup.go b/cmd/poller/collector/asup.go index 0cf073601..1b82b32f6 100644 --- a/cmd/poller/collector/asup.go +++ b/cmd/poller/collector/asup.go @@ -12,6 +12,7 @@ import ( "github.com/netapp/harvest/v2/pkg/conf" "github.com/netapp/harvest/v2/pkg/logging" "github.com/netapp/harvest/v2/pkg/matrix" + "github.com/netapp/harvest/v2/pkg/util" "github.com/shirou/gopsutil/v4/cpu" "github.com/shirou/gopsutil/v4/host" "github.com/shirou/gopsutil/v4/mem" @@ -240,15 +241,20 @@ func BuildAndWriteAutoSupport(collectors []Collector, status *matrix.Matrix, pol // Get the PID and RSS in bytes of the current process. // If there is an error, rssBytes will be zero pid := os.Getpid() - newProcess, err := process.NewProcess(int32(pid)) + pid32, err := util.SafeConvertToInt32(pid) if err != nil { - logging.Get().Err(err).Msg("failed to get process info") + logging.Get().Err(err).Int("pid", pid).Send() } else { - memInfo, err := newProcess.MemoryInfo() + newProcess, err := process.NewProcess(pid32) if err != nil { - logging.Get().Err(err).Int("pid", pid).Msg("failed to get memory info") + logging.Get().Err(err).Msg("failed to get process info") } else { - rssBytes = memInfo.RSS + memInfo, err := newProcess.MemoryInfo() + if err != nil { + logging.Get().Err(err).Int("pid", pid).Msg("failed to get memory info") + } else { + rssBytes = memInfo.RSS + } } } diff --git a/cmd/poller/collector/collector.go b/cmd/poller/collector/collector.go index a5c0bcff9..7dc4fb693 100644 --- a/cmd/poller/collector/collector.go +++ b/cmd/poller/collector/collector.go @@ -637,7 +637,7 @@ func (c *AbstractCollector) GetStatus() (uint8, string, string) { // of the values defined by CollectorStatus func (c *AbstractCollector) SetStatus(status uint8, msg string) { if status >= uint8(len(Status)) { - panic("invalid status code " + strconv.Itoa(int(status))) + panic("invalid status code " + strconv.FormatUint(uint64(status), 10)) } c.Status = status c.Message = msg diff --git a/cmd/poller/exporter/exporter.go b/cmd/poller/exporter/exporter.go index c6b7d34eb..b8d87a474 100644 --- a/cmd/poller/exporter/exporter.go +++ b/cmd/poller/exporter/exporter.go @@ -155,7 +155,7 @@ func (e *AbstractExporter) GetStatus() (uint8, string, string) { // SetStatus sets the current state of exporter func (e *AbstractExporter) SetStatus(code uint8, msg string) { if code >= uint8(len(status)) { - panic("invalid status code " + strconv.Itoa(int(code))) + panic("invalid status code " + strconv.FormatUint(uint64(code), 10)) } e.Status = code e.Message = msg diff --git a/cmd/poller/poller.go b/cmd/poller/poller.go index 9125dfc67..20be8c529 100644 --- a/cmd/poller/poller.go +++ b/cmd/poller/poller.go @@ -1316,7 +1316,13 @@ func (p *Poller) mergeConfPath() { func (p *Poller) addMemoryMetadata() { pid := os.Getpid() - proc, err := process.NewProcess(int32(pid)) + pid32, err := util.SafeConvertToInt32(pid) + if err != nil { + logger.Warn().Int("pid", pid).Msg(err.Error()) + return + } + + proc, err := process.NewProcess(pid32) if err != nil { logger.Error().Err(err).Int("pid", pid).Msg("Failed to lookup process for poller") return diff --git a/pkg/color/color.go b/pkg/color/color.go index 828020772..cf8bec432 100644 --- a/pkg/color/color.go +++ b/pkg/color/color.go @@ -29,7 +29,8 @@ func DetectConsole(option string) { case "always": withColor = true default: - if term.IsTerminal(int(os.Stdout.Fd())) { + fd := int(os.Stdout.Fd()) // #nosec G115 + if term.IsTerminal(fd) { withColor = true } } diff --git a/pkg/util/util.go b/pkg/util/util.go index b9006f4b5..c47913f67 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -12,6 +12,7 @@ import ( "github.com/shirou/gopsutil/v4/process" "golang.org/x/sys/unix" "gopkg.in/yaml.v3" + "math" "net" "net/http" "net/url" @@ -453,3 +454,10 @@ func HandleArrayFormat(name string) string { } return name } + +func SafeConvertToInt32(in int) (int32, error) { + if in > math.MaxInt32 { + return 0, fmt.Errorf("input %d is too large to convert to int32", in) + } + return int32(in), nil // #nosec G115 +}