Modify how the header row of df's output is split
This CL modified how the columns title of df is split.
This is necessary because df's columns titles row is
split by a regular expression and changing the call of df from
df -h to df -k changed pattern of the titles
Change-Id: Ic793b987c2ed8922edfc41b5b04b074a04c71b88
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/20930
Reviewed-by: Dexter Rivera <riverade@google.com>
Tested-by: Dexter Rivera <riverade@google.com>
Cloud-Build: Dexter Rivera <riverade@google.com>
diff --git a/src/pkg/nodeprofiler/profiler/commands.go b/src/pkg/nodeprofiler/profiler/commands.go
index 1a6fe37..7573607 100644
--- a/src/pkg/nodeprofiler/profiler/commands.go
+++ b/src/pkg/nodeprofiler/profiler/commands.go
@@ -223,6 +223,7 @@
// df represents the command 'df'
type df struct {
name string
+ flags string
titles []string
}
@@ -235,7 +236,7 @@
// map of title(s) to their values.
func (fs *df) Run() (map[string][]string, error) {
// get output in 1K size to make summing values direct
- out, err := utils.RunCommand(fs.Name(), "-k")
+ out, err := utils.RunCommand(fs.Name(), "-k", fs.flags)
if err != nil {
cmd := fs.Name() + " " + "-k"
return nil, fmt.Errorf("failed to run the command %q: %v",
@@ -250,7 +251,7 @@
//
// "Filesystem Size Used Avail Use% Mounted on" ->
// ["Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on"]
- re := regexp.MustCompile(`[A-Z][^A-Z]*`)
+ re := regexp.MustCompile(`[0-9]?[A-Z]{1}[^A-Z0-9]*`)
allTitles := re.FindAllString(lines[0], -1)
// trim trailing or leading white spaces in all titles.
allTitles = utils.TrimCharacter(allTitles, " ")
diff --git a/src/pkg/nodeprofiler/profiler/commands_test.go b/src/pkg/nodeprofiler/profiler/commands_test.go
index a9063f9..31e6f82 100644
--- a/src/pkg/nodeprofiler/profiler/commands_test.go
+++ b/src/pkg/nodeprofiler/profiler/commands_test.go
@@ -65,10 +65,22 @@
name: "df",
fakeCmd: &df{
name: "testdata/df.sh",
+ flags: "1",
titles: []string{"Use%"},
},
want: map[string][]string{
- "Use%": {"68%", "0%", "2%", "1%", "100%"},
+ "Use%": {"76%", "0%", "1%", "100%"},
+ },
+ },
+ {
+ name: "df's output with column titles mixed up",
+ fakeCmd: &df{
+ name: "testdata/df.sh",
+ flags: "2",
+ titles: []string{"Mounted on"},
+ },
+ want: map[string][]string{
+ "Mounted on": {"/", "/dev", "/dev/tty", "/dev/kvm", "/dev/.ssh/sshd_config"},
},
},
{
diff --git a/src/pkg/nodeprofiler/profiler/testdata/df.sh b/src/pkg/nodeprofiler/profiler/testdata/df.sh
index 736195d..5d82228 100755
--- a/src/pkg/nodeprofiler/profiler/testdata/df.sh
+++ b/src/pkg/nodeprofiler/profiler/testdata/df.sh
@@ -1,13 +1,27 @@
#!/bin/bash
main() {
+ case "$2" in
+ "1")
cat <<EOF
- Filesystem Size Used Avail Use% Mounted on
- /dev/vdb 7.5G 4.6G 2.2G 68% /
- tmpfs 100K 0 100K 0% /dev/lxd
- tmpfs 7.1G 121M 7.0G 2% /dev/shm
- run 7.1G 28K 7.1G 1% /dev/.host_ip
- /dev/root 173M 170M 0 100% /dev/.ssh/sshd_config
+ Filesystem 1K-blocks Used Available Use% Mounted on
+ /dev/vdb 7864320 5401876 1738492 76% /
+ none 492 0 492 0% /dev
+ run 7433436 28 7433408 1% /dev/.host_ip
+ /dev/root 176176 173936 0 100% /dev/.ssh/sshd_config
EOF
+ ;;
+ "2")
+ cat <<EOF
+ 1K-blocks Filesystem Used Available Use% Mounted on
+ /dev/vdb 7864320 5401876 1738492 76% /
+ none 492 0 492 0% /dev
+ devtmpfs 7430944 0 7430944 0% /dev/tty
+ /dev/vdb 7864320 5401876 1738492 76% /dev/kvm
+ /dev/root 176176 173936 0 100% /dev/.ssh/sshd_config
+EOF
+ ;;
+ esac
}
-main "$#"
\ No newline at end of file
+
+main "$@"
\ No newline at end of file