fix: added check_disk_space.sh

This commit is contained in:
Denis Evers 2023-06-24 10:42:28 +08:00
commit beea227cb3
Signed by: denis-ev
GPG Key ID: 10BFC1EB323A6CA8
1 changed files with 47 additions and 0 deletions

47
check_disk_space.sh Executable file
View File

@ -0,0 +1,47 @@
# check_disk_space.sh
#!/bin/bash
# Define the URLs for pinging
ping_url="https://healthchecks.io/ping/{uuid}"
fail_url="https://healthchecks.io/ping/{uuid}/fail"
# Define the minimum free space threshold (15%)
minimum_free_space=15
# Define directory to be checked
dir_to_check=${1:-"."}
# Function to check available disk space percentage
check_disk_space() {
local disk_info=( $(df -P $1 | awk 'NR==2 {print $2 " " $3 " " $4}') )
local total_space=${disk_info[0]}
local used_space=${disk_info[1]}
local available_space=${disk_info[2]}
local percentage_used=$(( used_space * 100 / total_space ))
local percentage_free=$(( 100 - percentage_used ))
echo "Percentage of free space: $percentage_free%"
if [ $percentage_free -lt $minimum_free_space ]; then
return 1 # Insufficient disk space
else
return 0 # Sufficient disk space
fi
}
# Function to perform curl operation with error handling
perform_curl() {
if curl -fsS -m 10 --retry 5 -o /dev/null "$1"; then
echo "Curl to $1 was successful."
else
echo "Curl to $1 failed with error: $?"
fi
}
# Check available disk space
if check_disk_space "$dir_to_check"; then
echo "Sufficient disk space. Pinging $ping_url"
perform_curl "$ping_url"
else
echo "Insufficient disk space. Pinging $fail_url"
perform_curl "$fail_url"
fi