Skip to content

Commit

Permalink
Merge pull request #3 from p00rt/master
Browse files Browse the repository at this point in the history
Rewrite app/checks/disk-space to python, change the syntax
  • Loading branch information
blackandred committed May 12, 2019
2 parents 76a9c65 + 1bf5a6e commit 46cb9d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
10 changes: 5 additions & 5 deletions app/checks-tests/disk-space-test.sh
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
57 changes: 38 additions & 19 deletions app/checks/disk-space
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"]))

0 comments on commit 46cb9d2

Please sign in to comment.