admin-stuff/check-disk-usage.sh

417 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# ----------
# - Some script variables (Default values other variables see below)
# ----------
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# ----------
# Base Function(s)
# ----------
usage() {
[[ -n "$1" ]] && error "$1"
[[ $terminal ]] && echo -e "
\033[1mUsage:\033[m
$(basename $0) [-c <integer>}
\033[1mDescription\033[m
Script wwarns if disk usage exceeds a given or default value (percent)
\033[1mOptions\033[m
-c <integer)
a critical value in percent. if disc usage reaches this value, a warn e-mail
to given e-mail adresse(s) will be send.
Default e-mail adresse(s):
${DEFAULT_ALERT_EMAIL_ADDRESSES}
-h
Prints this help.
\033[1mFiles\033[m
$conf_file: Configuration file
\033[1mExample:\033[m
warn if disc usage exceeds 80%
$(basename $0) -c 80
"
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n " $*\\c" 1>&2
else
echo -e -n " $*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo "[ Error ]: $*"
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
else
echo "[ Warning ]: $*"
fi
echo ""
}
warn_only_terminal () {
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
echo ""
fi
}
ok (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
echo ""
fi
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_warn() {
if $terminal ; then
echo -e "\033[75G[ \033[33mwarn\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]"
fi
}
echo_wait(){
if $terminal ; then
echo -en "\033[75G[ \033[5m\033[1m...\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
is_number() {
return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1);
# - also possible
# -
i#[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1
#retuon $([[ ! -z "${1##*[!0-9]*}" ]])
}
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
terminal=false
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
clear
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
DEFAULT_ADMIN_EMAIL="argus@oopen.de"
DEFAULT_COMPANY="O.OPEN"
DEFAULT_CONTENT_TYPE='Content-Type: text/plain;\n charset="utf-8"'
DEFAULT_SENDER_ADDRESS="${script_name%%.*}@$(hostname -f)"
DEFAULT_ALERT_EMAIL_ADDRESSES="ckubu@oopen.de"
DEFAULT_WARN_VALUE=85%
# - Print help?
# -
if [[ "$(trim $*)" = "--help" ]] || [[ "$(trim $*)" = "-help" ]] || [[ "$(trim $*)" = "help" ]] ; then
usage
fi
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
warn_only_terminal "No configuration file '$conf_file' present.\n
Loading default values.."
fi
[[ -n "${WARN_VALUE}" ]] || WARN_VALUE="${DEFAULT_WARN_VALUE}"
[[ -n "${ADMIN_EMAIL}" ]] || ADMIN_EMAIL="${DEFAULT_ADMIN_EMAIL}"
[[ -n "${COMPANY}" ]] || COMPANY="${DEFAULT_COMPANY}"
[[ -n "${ALERT_EMAIL_ADDRESSES}" ]] || ALERT_EMAIL_ADDRESSES="${DEFAULT_ALERT_EMAIL_ADDRESSES}"
[[ -n "${CONTENT_TYPE}" ]] || CONTENT_TYPE="${DEFAULT_CONTENT_TYPE}"
[[ -n "${SENDER_ADDRESS}" ]] || SENDER_ADDRESS="${DEFAULT_SENDER_ADDRESS}"
WARN_VALUE="${WARN_VALUE%\%}"
if ! $(is_number ${WARN_VALUE}) ; then
fatal "Given value for \033[1mWARN_VALUE\033[m (\033[1m${WARN_VALUE}\033[m) isn't a number!"
fi
SUBJECT="[ Warning ] Hard disk storage on $(hostname -f) exceeds ${WARN_VALUE}%"
# ----------
# - Read commandline parameter
# ----------
while getopts c:h opt ; do
case $opt in
c) WARN_VALUE="${OPTARG}"
WARN_VALUE="${WARN_VALUE%\%}"
;;
h) usage
;;
\?) usage ;;
esac
done
# - Commandline parameter overwrites those given at configuration file
# -
if ! $(is_number ${WARN_VALUE}) ; then
fatal "Given value for \033[1mWARN_VALUE\033[m at commandline option \033[1m-c\033[m (\033[1m${WARN_VALUE}\033[m) isn't a number!"
fi
if [[ -n "$(trim ${ALERT_EMAIL_ADDRESSES})" ]] ; then
for _email in $ALERT_EMAIL_ADDRESSES ; do
alert_email_arr+=("$_email")
done
fi
# ----------
# - Jobhandling
# ----------
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
echo -e "To:${ADMIN_EMAIL}\n${CONTENT_TYPE}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f ${SENDER_ADDRESS} ${ADMIN_EMAIL}
exit 1
fi
blank_line
msg=""
found=false
while IFS='' read -r _line || [[ -n $_line ]] ; do
read -ra _addr <<< "$_line"
_device="${_addr[1]}"
_dir="${_addr[2]}"
_tmp_percent="${_addr[0]}"
declare -i _percent="${_tmp_percent%\%}"
if [[ ${_percent} -gt ${WARN_VALUE} ]] ; then
#echo -e "\033[3G${_device}\033[15G -> ${_dir}\033[25G ${_percent}%"
if [[ -z "${msg}" ]]; then
msg="\n The hard disk space on Device '${_device}' mounted at '${_dir}' is '${_percent}'% full!\n"
else
msg+="\n The hard disk space on Device '${_device}' mounted at '${_dir}' is '${_percent}'% full!\n"
fi
if ! ${found} && ${terminal} ; then
echo -e " Host: \033[1m$(hostname -f)\033[m"
fi
warn_only_terminal "The hard disk space on Device \033[1m${_device}\033[m mounted at \033[1m${_dir}\033[m is \033[1;31m${_percent}% full!\033[m"
found=true
fi
done < <(df -H | grep -vE '^Dateisystem|Filesystem|tmpfs|cdrom|loop|udev' | awk '{ print $5 " " $1 " " $6 }')
if $found ; then
if $(which lxc-ls > /dev/null) ; then
_guest_machines="$(lxc-ls)"
if [[ ${#_guest_machines} -gt 0 ]]; then
if ${terminal}; then
echo -e " LX Guest Systems:"
fi
msg_guests="LX Guest Systems:"
for _name in ${_guest_machines} ; do
_state="$(lxc-info -n ${_name} | grep -i 'State:' | awk '{print$2}')"
msg_guests="${msg_guests}\n (${_state}:) ${_name}"
if ${terminal}; then
echo -e " (${_state}:) ${_name}"
fi
done
fi # if [[ ${#_guest_machines} -gt 0 ]]
fi # if $(which lxc-ls > /dev/null)
SUBJECT="[ Warning ] Hard disk storage on $(hostname -f) exceeds ${WARN_VALUE}%"
SUBJECT_UTF8="$(echo "${SUBJECT}" | iconv -t UTF8)"
SUBJECT_UTF8_ENCODED="=?utf-8?B?$(echo ${SUBJECT_UTF8} | base64 --wrap=0)?="
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${CONTENT_TYPE}\nSubject:${SUBJECT_UTF8_ENCODED}\n\nHost: $(hostname -f)\n${msg}\n${msg_guests}" | /usr/sbin/sendmail -F "${COMPANY} -- hard disk usage" -f ${SENDER_ADDRESS} ${_email}
done
else
info "Harddisk usage on host \033[1m$(hostname -f)\033[m is fine."
fi
clean_up 0