-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from p00rt/master
Rewrite app/checks/disk-space to python, change the syntax
- Loading branch information
Showing
2 changed files
with
43 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
#!/bin/bash | ||
|
||
echo " >> Assert that 3,2GB is not enough, 4GB at least required" | ||
MOCK_DF_OUTPUT="3,2GB" DIR=/ MIN_REQ_SPACE=4 ../checks/disk-space | ||
MOCK_FREE_SPACE="3.2" DIR=/ MIN_REQ_SPACE=4 ../checks/disk-space | ||
[[ "$?" == 0 ]] && echo " .. Test failed." && exit 1 | ||
|
||
echo " >> Assert 4GB is enough, when minimum is 4.5GB" | ||
MOCK_DF_OUTPUT="4GB" DIR=/ MIN_REQ_SPACE=4.5 ../checks/disk-space | ||
echo " >> Assert 4.5GB is enough, when minimum is 4GB" | ||
MOCK_FREE_SPACE="4.5" DIR=/ MIN_REQ_SPACE=4 ../checks/disk-space | ||
[[ "$?" == 1 ]] && echo " .. Test failed." && exit 1 | ||
|
||
echo " >> Assert 5GB is enough, when minimum is 10GB" | ||
MOCK_DF_OUTPUT="5GB" DIR=/ MIN_REQ_SPACE=10 ../checks/disk-space | ||
echo " >> Assert 50GB is enough, when minimum is 10GB" | ||
MOCK_FREE_SPACE="50" DIR=/ MIN_REQ_SPACE=10 ../checks/disk-space | ||
[[ "$?" == 1 ]] && echo " .. Test failed." && exit 1 | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
#!/bin/bash | ||
#!/usr/bin/python3 | ||
|
||
if [[ ! "${DIR}" ]] || [[ ! "${MIN_REQ_SPACE}" ]]; then | ||
echo "Missing one of parameters: DIR, MIN_REQ_SPACE" | ||
exit 1 | ||
fi | ||
""" | ||
This script checks if there is enough free space on a mountpoint. Usage: | ||
./disk-space.py DIR MIN_REQ_SPACE[bkMGT] | ||
accepted units are kibibytes - k, mebibytes - M etc. up to tebibytes. leave blank for value in gibibytes. | ||
""" | ||
import os | ||
from sys import argv,stderr | ||
if "DIR" not in os.environ or "MIN_REQ_SPACE" not in os.environ: | ||
print("Missing one of parameters.\nUsage: ./disk-space.py, env. variables are: DIR MIN_REQ_SPACE[bkMGT]", file=stderr) | ||
exit(127) | ||
|
||
df_output=$(df -h ${DIR} --output=size) | ||
def check(mntpoint, req_space): | ||
unit="G" | ||
if req_space[-1] in "bkMGT": | ||
unit = req_space[-1] | ||
req_space = float(req_space[:-1]) | ||
else: | ||
req_space = float(req_space) | ||
|
||
if [[ "${MOCK_DF_OUTPUT}" ]]; then | ||
df_output="${MOCK_DF_OUTPUT}" | ||
fi | ||
statvfs = os.statvfs(mntpoint) | ||
if "MOCK_FREE_SPACE" in os.environ: # check if we have mock data | ||
free_space = float(os.environ["MOCK_FREE_SPACE"]) * 1024**3 # mock data is in GiB | ||
else: | ||
free_space = statvfs.f_frsize * statvfs.f_bavail # block size * avail blocks | ||
if unit == "k": | ||
free_space /= 1024. | ||
elif unit == "M": | ||
free_space /= 1024. ** 2 | ||
elif unit == "G": | ||
free_space /= 1024. ** 3 | ||
elif unit == "T": | ||
free_space /= 1024. ** 4 | ||
|
||
space=$(echo ${df_output} | tail -n 1 | tr -d "[:space:]" | tr -dc '0-9\.') | ||
space=${space//,/.} | ||
if free_space >= req_space: | ||
print("There is {0:.1f}{1} disk space at {2}, nothing to worry about, defined minimum is {3}{1}".format(free_space, unit, mntpoint, req_space)) | ||
return 0 | ||
else: | ||
print("Failed asserting that {0:.1f}{1} is at least {2}{1} at {3}".format(free_space, unit, req_space, mntpoint)) | ||
return 1 | ||
|
||
if [[ $(python -c "print(${space}<${MIN_REQ_SPACE})") == "False" ]]; then | ||
echo "Failed asserting that ${space}GB is at least ${MIN_REQ_SPACE}GB at '${DIR}' mountpoint" | ||
exit 1 | ||
fi | ||
|
||
echo "There is ${space}GB disk space at '${DIR}', nothing to worry about, defined minimum is ${MIN_REQ_SPACE}GB" | ||
|
||
exit 0 | ||
exit(check(os.environ["DIR"], os.environ["MIN_REQ_SPACE"])) |