Fix and test safeguards.sh. Implement disk space+margin check.

This commit is contained in:
Daniel Nichter
2011-12-14 16:12:43 -07:00
parent 90fbfaf41f
commit 92c8635c4a
3 changed files with 85 additions and 10 deletions

View File

@@ -25,27 +25,50 @@ set -u
disk_space() {
local filesystem=${1:-"$PWD"}
# Filesystem 512-blocks Used Available Capacity Mounted on
# /dev/disk0s2 236306352 190223184 45571168 81% /
df -m -P $filesystem
# Filesystem 1M-blocks Used Available Capacity Mounted on
# /dev/disk0s2 115383 92637 22496 81% /
df -m $filesystem
}
# Sub: check_disk_space
# Check if there is or will be enough disk space.
#
# Arguments:
# file - File with output from <disk_space()>.
# mb - Minimum MB free.
# pc - Minimum percent free.
# margin - Add this many MB to the real MB used.
#
# Returns:
# 0 if there is/will be enough disk space, else 1.
check_disk_space() {
local file=$1
local mb=${2:-"0"}
local pct=${3:-"0"}
local pc=${3:-"0"}
local margin=${4:-"0"}
local avail=$(cat $file | awk '/^\//{print $4}');
local full=$(cat $file | awk '/^\//{print $5}' | sed -e 's/%//g');
if [ "${avail}" -le "$mb" -o "$full" -le "$pct" ]; then
echo "Not enough free space (${full}% full, ${avail}MB free)"
echo "Wanted less than ${pct}% full and more than ${mb}MB"
local mb_used=$(cat $file | awk '/^\//{print $3}');
local mb_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))
mb_used=$(($mb_used + $margin))
mb_free=$(($mb_free - $margin))
pc_used=$(awk "BEGIN { printf(\"%d\", $mb_used/$mb_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"
return 1
fi
return 0
}
# ###########################################################################
# End safeguards package
# ###########################################################################

50
t/lib/bash/safeguards.sh Normal file
View File

@@ -0,0 +1,50 @@
#!/usr/bin/env bash
TESTS=10
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/safeguards.sh"
TMPDIR="$TEST_TMPDIR"
SAMPLE="$T_LIB_DIR/samples/bash"
disk_space "/" > $TMPDIR/df-out
cmd_ok \
"grep -q Avail $TMPDIR/df-out" \
"disk_space()"
check_disk_space "$SAMPLE/diskspace001.txt" 22495 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
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" \
"Warning if not enough disk space"
check_disk_space "$SAMPLE/diskspace001.txt" 22495 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
is "$?" "0" "Enough disk space with margin"
check_disk_space "$SAMPLE/diskspace001.txt" 22396 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
is "$?" "1" "Not enough % free with margin"
cmd_ok \
"grep -q '3% free,' $TMPDIR/out" \
"Calculates % free with margin"
# ###########################################################################
# Done
# ###########################################################################

View File

@@ -0,0 +1,2 @@
Filesystem 1M-blocks Used Available Capacity Mounted on
/dev/disk0s2 115383 92637 22496 81% /