Skip to content

Commit

Permalink
Add parser for the output of remote vmstat command
Browse files Browse the repository at this point in the history
Normally you would use `vmstat 1 2` as the parameters
(1 second wait time, 2 samples - startup and actual)

The code itself will just look at the last line of it,
so you can do larger times and more reports if needed.
  • Loading branch information
afbjorklund committed Nov 1, 2020
1 parent 88df534 commit 4c78a65
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ func ConvertUnsignedBytesToMB(byteSize uint64) int64 {
return int64(byteSize / units.MiB)
}

// ParseVMStat parses the output of the `vmstat` command
// returns: busy, idle (%)
func ParseVMStat(out string) (int, int, error) {
//procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
//r b swpd free buff cache si so bi bo in cs us sy id wa st
//0 0 0 24562996 667340 3463592 0 0 53 40 279 222 6 4 90 0 0
//0 0 0 24562680 667348 3463632 0 0 0 68 1192 7571 2 1 97 0 0
outlines := strings.Split(out, "\n")
l := len(outlines)
for _, line := range outlines[l-2 : l-1] {
parsedLine := strings.Fields(line)
if len(parsedLine) < 17 {
continue
}
us, err := strconv.Atoi(parsedLine[12])
if err != nil {
return 0, 0, err
}
sy, err := strconv.Atoi(parsedLine[13])
if err != nil {
return 0, 0, err
}
id, err := strconv.Atoi(parsedLine[14])
if err != nil {
return 0, 0, err
}
wa, err := strconv.Atoi(parsedLine[15])
if err != nil {
return 0, 0, err
}
st, err := strconv.Atoi(parsedLine[16])
if err != nil {
return 0, 0, err
}
return us + sy + wa + st, id, nil
}
return 0, 0, errors.New("No matching data found")
}

// ParseMemFree parses the output of the `free -m` command
// returns: total, available
func ParseMemFree(out string) (uint64, uint64, error) {
Expand Down

0 comments on commit 4c78a65

Please sign in to comment.