Skip to content

Commit

Permalink
build: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss committed Aug 21, 2024
1 parent 7753ca6 commit b8fcef2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
16 changes: 11 additions & 5 deletions cmd/poller/collector/asup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/poller/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/poller/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit b8fcef2

Please sign in to comment.