From bb315948cd555ede71b0a9df4f9c4d0735ef71a0 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Mon, 19 Dec 2011 10:22:42 -0700 Subject: [PATCH 1/2] Parse disk space using df -P -k. --- lib/bash/safeguards.sh | 42 ++++++++++++++++------------- t/lib/bash/safeguards.sh | 19 ++++++++----- t/lib/samples/bash/diskspace001.txt | 4 +-- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/bash/safeguards.sh b/lib/bash/safeguards.sh index 5fd5850f..1e6df342 100644 --- a/lib/bash/safeguards.sh +++ b/lib/bash/safeguards.sh @@ -25,19 +25,21 @@ set -u disk_space() { local filesystem=${1:-"$PWD"} - # Filesystem 1M-blocks Used Available Capacity Mounted on - # /dev/disk0s2 115383 92637 22496 81% / - df -m $filesystem + # Filesystem 1024-blocks Used Available Capacity Mounted on + # /dev/disk0s2 118153176 94409664 23487512 81% / + df -P -k $filesystem } # Sub: check_disk_space -# Check if there is or will be enough disk space. +# Check if there is or will be enough disk space. Input is a file +# with output from , i.e. `df -P -k`. The df output +# must use 1k blocks, but the mb arg from the user is in MB. # # Arguments: -# file - File with output from . -# mb - Minimum MB free. -# pc - Minimum percent free. -# margin - Add this many MB to the real MB used. +# file - File with output from . +# mb - Minimum MB free. +# pc - Minimum percent free. +# mb_margin - Add this many MB to the real MB used. # # Returns: # 0 if there is/will be enough disk space, else 1. @@ -45,24 +47,28 @@ check_disk_space() { local file=$1 local mb=${2:-"0"} local pc=${3:-"0"} - local margin=${4:-"0"} + local mb_margin=${4:-"0"} - local mb_used=$(cat $file | awk '/^\//{print $3}'); - local mb_free=$(cat $file | awk '/^\//{print $4}'); + # Convert MB to KB because the df output should be in 1k blocks. + local kb=$(($mb * 1024)) + local kb_margin=$(($mb_margin * 1024)) + + local kb_used=$(cat $file | awk '/^\//{print $3}'); + local kb_free=$(cat $file | awk '/^\//{print $4}'); local pc_used=$(cat $file | awk '/^\//{print $5}' | sed -e 's/%//g'); - if [ "$margin" -gt "0" ]; then - local mb_total=$(($mb_used + $mb_free)) + if [ "$kb_margin" -gt "0" ]; then + local kb_total=$(($kb_used + $kb_free)) - mb_used=$(($mb_used + $margin)) - mb_free=$(($mb_free - $margin)) - pc_used=$(awk "BEGIN { printf(\"%d\", $mb_used/$mb_total * 100) }") + kb_used=$(($kb_used + $kb_margin)) + kb_free=$(($kb_free - $kb_margin)) + pc_used=$(awk "BEGIN { printf(\"%d\", $kb_used/$kb_total * 100) }") fi local pc_free=$((100 - $pc_used)) - if [ "$mb_free" -le "$mb" -o "$pc_free" -le "$pc" ]; then - warn "Not enough free disk space: ${pc_free}% free, ${mb_free} MB free; wanted more than ${pc}% free or ${mb} MB free" + if [ "$kb_free" -le "$kb" -o "$pc_free" -le "$pc" ]; then + warn "Not enough free disk space: ${pc_free}% free, ${kb_free} KB free; wanted more than ${pc}% free or ${kb} KB free" return 1 fi diff --git a/t/lib/bash/safeguards.sh b/t/lib/bash/safeguards.sh index 7bbc3ed1..c874498f 100644 --- a/t/lib/bash/safeguards.sh +++ b/t/lib/bash/safeguards.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -TESTS=10 +TESTS=11 source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/safeguards.sh" @@ -13,30 +13,35 @@ cmd_ok \ "grep -q Avail $TMPDIR/df-out" \ "disk_space()" -check_disk_space "$SAMPLE/diskspace001.txt" 22495 18 >$TMPDIR/out 2>&1 +is \ + "`wc -l $TMPDIR/df-out | awk '{print $1}'`" \ + "2" \ + "2-line df output" + +check_disk_space "$SAMPLE/diskspace001.txt" 22000 18 >$TMPDIR/out 2>&1 is "$?" "0" "Enough disk space" is \ "`cat $TMPDIR/out`" \ "" \ "No output if enough disk space" -check_disk_space "$SAMPLE/diskspace001.txt" 22496 18 >$TMPDIR/out 2>&1 +check_disk_space "$SAMPLE/diskspace001.txt" 24000 18 >$TMPDIR/out 2>&1 is "$?" "1" "Not enough MB free" cmd_ok \ - "grep -q '19% free, 22496 MB free; wanted more than 18% free or 22496 MB free' $TMPDIR/out" \ + "grep -q '19% free, 23487512 KB free; wanted more than 18% free or 24576000 KB free' $TMPDIR/out" \ "Warning if not enough disk space" -check_disk_space "$SAMPLE/diskspace001.txt" 22495 19 >$TMPDIR/out 2>&1 +check_disk_space "$SAMPLE/diskspace001.txt" 22000 19 >$TMPDIR/out 2>&1 is "$?" "1" "Not enough % free" # ########################################################################### # Check with a margin (amount we plan to use in the future). # ########################################################################### -check_disk_space "$SAMPLE/diskspace001.txt" 22395 18 100 +check_disk_space "$SAMPLE/diskspace001.txt" 22000 18 100 is "$?" "0" "Enough disk space with margin" -check_disk_space "$SAMPLE/diskspace001.txt" 22396 18 100 >$TMPDIR/out 2>&1 +check_disk_space "$SAMPLE/diskspace001.txt" 23000 18 100 >$TMPDIR/out 2>&1 is "$?" "1" "Not enough MB free with margin" check_disk_space "$SAMPLE/diskspace001.txt" 100 5 20000 >$TMPDIR/out 2>&1 diff --git a/t/lib/samples/bash/diskspace001.txt b/t/lib/samples/bash/diskspace001.txt index acc1d512..c68af8c9 100644 --- a/t/lib/samples/bash/diskspace001.txt +++ b/t/lib/samples/bash/diskspace001.txt @@ -1,2 +1,2 @@ -Filesystem 1M-blocks Used Available Capacity Mounted on -/dev/disk0s2 115383 92637 22496 81% / +Filesystem 1024-blocks Used Available Capacity Mounted on +/dev/disk0s2 118153176 94409664 23487512 81% / From 8439962b45a46830cf281ca2c4044b8f3f08f5f9 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Mon, 19 Dec 2011 10:44:36 -0700 Subject: [PATCH 2/2] Make tests more flexible. --- t/lib/bash/collect.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/lib/bash/collect.sh b/t/lib/bash/collect.sh index ad059e29..9d9249fa 100644 --- a/t/lib/bash/collect.sh +++ b/t/lib/bash/collect.sh @@ -62,11 +62,11 @@ cmd_ok \ "lsof" cmd_ok \ - "grep -q 'buf/buf0buf.c' $p-mutex-status1" \ + "grep -q 'buf0buf.c' $p-mutex-status1" \ "mutex-status1" cmd_ok \ - "grep -q 'buf/buf0buf.c' $p-mutex-status2" \ + "grep -q 'buf0buf.c' $p-mutex-status2" \ "mutex-status2" cmd_ok \ @@ -90,7 +90,7 @@ cmd_ok \ "ps" cmd_ok \ - "grep -qP '^warning_count\t\d' $p-variables" \ + "grep -qP '^wait_timeout\t\d' $p-variables" \ "variables" local iters=$(cat $p-df | grep -c '^TS ')