diff --git a/check_mount_count.sh b/check_mount_count.sh new file mode 100755 index 0000000..0ad91cd --- /dev/null +++ b/check_mount_count.sh @@ -0,0 +1,307 @@ +#!/usr/bin/env bash + +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) + +\033[1mDescription\033[m + + Script checks if 'Mount count' of a given partition exceed 'Maximum mount count' of + that partition. If 'Mount count' is greater as 'Maximum mount count', a warning is + printed out. + +\033[1mOptions\033[m + + No Options available + +\033[1mExample:\033[m + + Check mount count of partition /dev/mapper/backup + + $(basename $0) /dev/mapper/backup + +" + + clean_up 1 +} + + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + +echononl(){ + if $terminal && $LOGGING ; 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 ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + 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[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" +} + +info (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + +echo_ok() { + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[32mok\033[m ]" + else + if $LOGGING ; then + echo " [ ok ]" + fi + fi +} +echo_failed(){ + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + else + if $LOGGING ; then + echo " [ failed ]" + fi + fi +} +echo_skipped() { + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" + else + if $LOGGING ; then + echo " [ skipped ]" + fi + fi +} +echo_warning() { + if $terminal && $LOGGING ; then + echo -e "\033[75G[ \033[33m\033[1mwarning\033[m ]" + else + if $LOGGING ; then + echo " [ skipped ]" + fi + fi +} + + +is_number() { + + return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); + + # - also possible + # - + #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 + #return $([[ ! -z "${1##*[!0-9]*}" ]]) +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +# ---------- +# - 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 "" + + for _email in ${alert_email_arr[@]} ; do + echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ + | sendmail -F "Error `hostname -f`" -f $sender_address $_email + done + + exit 1 + +fi + + + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + +if [[ -n "$1" ]] ; then + part_name=$1 +else + error "No Partition given!" + usage +fi + + +#--------------------------------------- +#----------------------------- +# Check mount count. +#----------------------------- +#--------------------------------------- + +if $LOGGING ; then + declare -i _length="${#part_name}" + echo -e "\n Check mount count of partition $part_name.." + echo -en " =================================" + declare -i i=0 + while [[ $i -lt $_length ]] ; do + echo -n "=" + ((i++)) + done + echo "" +fi + + +blank_line +echononl "Determin actual mount count of partitin $part_name .." +declare -i mount_count=$(tune2fs -l $part_name | grep "Mount count" | cut -d ":" -f 2) +if [[ $? -ne 0 ]]; then + echo_failed + fatal "Cannot determin mount count of partitin ${part_name}!" +else + if $(is_number $mount_count) ; then + echo_ok + else + echo_failed + fatal "Cannot determin mount count of partitin ${part_name}!" + fi +fi + +echononl "Determin maximal mount count of partitin $part_name .." +declare -i max_mount_count=$(tune2fs -l $part_name | grep "Maximum mount count" | cut -d ":" -f 2) +if [[ $? -ne 0 ]]; then + echo_failed + fatal "Cannot determin maximal mount count of partitin ${part_name}!" +else + if $(is_number $max_mount_count) ; then + if [[ $max_mount_count -lt 2 ]] ; then + echo_warning + fatal "Maximal mount count of partitin ${part_name} is set incorrect (value: $max_mount_count)!" + else + echo_ok + fi + else + if [[ $max_mount_count -eq -1 ]] ; then + echo_warning + fatal "Maximal mount count of partitin ${part_name} not set (actual value: $max_mount_count)!" + else + echo_failed + fatal "Cannot determin maximl mount count of partitin ${part_name}!" + fi + fi +fi + +blank_line +if [[ $mount_count -gt max_mount_count ]] ; then + warn "Mount count (${mount_count}) of partition ${part_name} exceeds Maximal mount count (${max_mount_count})" +else + info "Mount count (${mount_count}) of partition ${part_name} is less than Maximal mount count (${max_mount_count}) + + \033[1mAll is fine.\033[m" +fi + +clean_up 0