utils

package
v0.0.0-...-b71eeb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package utils defines parsing and run command functions that can be used outside nodeprofiler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseColumns

func ParseColumns(rows []string, allTitles []string, wantTitles ...string) (map[string][]string, error)

ParseColumns parses command outputs which are in a column table. It takes in a slice of strings which has all the column titles of the command in the correct order. This is necessary because some titles contain multiple strings, thus splitting by whitespaces will split row incorrectly. E.g, splitting the titles row in df's output based on whitespaces will result in:

"Filesystem Use% Mounted on" -> ["Filesystem", "Use%", "Mounted", "on"] instead of:

["Filesystem", "Use%", "Mounted on"]

The allTitles slice is used to give each title its correct index. If the titles in the slice are in wrong order, the function's output will be incorrect.

The function also takes in an optional want titles slice which specifies the columns to parse. If this argument is missing, then all columns are parsed.

Eg, ParseColumns(["r b swpd buff",

"10        0    14831128        0"],
["r", "b"]) = map[string][]string {
"r": ["10"]
"b": ["0"]}

The output needs to have titles on all its columns else the function will return an error:

Eg [FAIL] ParseColumns([" total used",

"Mem:          14520          12",
"Swap:             0           0"],
["total", "used"])
err : "row has different number of columns from header row"

Some edge cases that will be parsed by this function include:

rows with repeated headers, eg with the output of iostat: [] string {"Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn",

"vdb        2.39        57.39        69.83     855576    1041132",
"                                                               ",
"Device      tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn",
"                                                                ",
"Device      tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn"} =>
map[string][]string {
	"tps" : {"2.39"},
 "kB_read/s" : {"57.39"},
 ...
}

func ParseRows

func ParseRows(lines []string, delim string, titles ...string) (map[string][]string, error)

ParseRows parses command outputs which are in a row table. It takes in a string which specifies the delimiter that separates row title from values. The function does not support '\n' as a delimiter for now. It takes in an optional titles slice which specifies which rows to parse.

Eg, ParseRows(["avg-cpu: %user %nice %system %iowait %steal %idle"],

[avg-cpu]) = map[string][]string {
avg-cpu: [%user, %nice, %system, %iowait, %steal, %idle]}

If the wrong delimiter is passed in, the function returns an error:

Eg [FAIL] ParseRows(["Device tps kB_read/s kB_wrtn/s",

"vdb 	1.13	  19.48 	  33.61"
"vda    0.02       0.86       0.00"], ":", ["vda"])
err: "failed to split row into row title and value"

Some edge cases parsed by this function include:

Rows whose delimiter have whitespaces around it. For example, [] string { "processor: 7",

"CPU family:    6"} =>
map[string][]string {
	  "processor"  : {"7"}
   "cpu family" : {"6"}
}

OR

[] string { "processor : 7",

"cpu family     : 6"} =>
map[string][]string {
	  "processor" : {"7"}
   "cpu family" : {"6"}
}

func ParseRowsAndColumns

func ParseRowsAndColumns(lines []string, titles ...string) (map[string][]string, error)

ParseRowsAndColumns parses command outputs that have row and column headers. Specifically, it parses command outputs that have an empty string on row 0 column 0. For example, the output of free as seen below:

total        used        free

Mem: 14518 12 14479 Swap: 0 0 0

If the command output has a non-empty string on row 0 column 0 as with iostat below, the function will throw an error since this is an output that should be parsed by columns instead "Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn", "vdb 0.74 10.39 23.23 859900 1922916", "vda 0.01 0.46 0.00 37829 0"

The function takes in an optional titles slice which specifies the row column combination to parse. If the titles argument is missing, then an empty map is returned.

Eg, ParseRowsAndColumns([" total used free shared",

"Mem:   14520     12   14482        0",
"Swap:      0      0       0        "],
["Mem:used", "Swap:total"]) = map[string][]string {
"Mem:used": ["12"]
"Swap:total" : ["0"]}

The titles should be in the format "row:column". Else, an error is returned:

Eg [FAIL], ParseRowsAndColumns([" total used free shared",

"Mem:   14520     12   14482       0",
"Swap:      0      0       0        "],
["Mem+used", "Swap+total"])
err : "title string not well-formatted"

func RunCommand

func RunCommand(cmd string, args ...string) ([]byte, error)

RunCommand is a wrapper function for exec.Command that will run the command specified return its output and/or error.

func SumAtoi

func SumAtoi(a []string) (int, error)

SumAtoi converts all the strings in a slice to integers, sums them up and returns the result. A non-nil error is returned if an error occurred.

func SumParseFloat

func SumParseFloat(a []string) (float64, error)

SumParseFloat converts all the strings in a slice to floating points, sums them up, and returns the result as a floating point. A non-nil error is returned if an error occurred.

func TrimCharacter

func TrimCharacter(a []string, char string) []string

TrimCharacter trims the specified character from each string in a slice of strings and returns a slice with the trimmed strings.

Types

type ParsedOutput

type ParsedOutput map[string][]string

ParsedOutput is a data structure that holds the parsed output of certain shell commands whose output takes the form of a table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL