Skip to content

Commit

Permalink
Locale-safe ps output parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bakks committed Nov 4, 2022
1 parent 2c2dbc5 commit a10034c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions top.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ type PsProcess struct {
Command string
}

func parsePerc(field string) (float64, error) {
// In some locales the ps output uses a `,` rather than a `.`
cleanField := strings.Replace(field, ",", ".", 64)
return strconv.ParseFloat(cleanField, 64)
}

func GetPsProcesses(ctx context.Context) ([]*PsProcess, error) {
args := []string{"auxc"}
out, err := commandWithContext(ctx, "ps", args...)
Expand All @@ -98,12 +104,12 @@ func GetPsProcesses(ctx context.Context) ([]*PsProcess, error) {
return nil, err
}

cpuPerc, err := strconv.ParseFloat(fields[2], 64)
cpuPerc, err := parsePerc(fields[2])
if err != nil {
return nil, err
}

memPerc, err := strconv.ParseFloat(fields[3], 64)
memPerc, err := parsePerc(fields[3])
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a10034c

Please sign in to comment.