admin-stuff/show-nfs-clients.sh

290 lines
5.1 KiB
Bash
Executable File

#!/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/$(basename $0).$$.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# ----------
# Base Function(s)
# ----------
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
usage() {
[[ -n "$1" ]] && error "$1"
[[ $terminal ]] && echo -e "
\033[1mUsage:\033[m
$(basename $0)
\033[1mDescription\033[m
Script lists all connected NFS client ip-addresses.
\033[1mOptions\033[m
No Options present
\033[1mFiles\033[m
$conf_file: Configuration file
\033[1mExample:\033[m
List connected NFS clients
$(basename $0)
"
clean_up 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
echo -e -n " $*\\c"
else
#echo -e -n " $*" 1>&2
echo -e -n " $*"
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
echo ""
rm -rf $LOCK_DIR
exit 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
else
echo " [ Error ] $*"
fi
echo ""
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
echo ""
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[37m\033[1mskipped\033[m ]"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
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
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# - Print help?
# -
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
usage
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
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"
echo ""
fi
# Redirect all errors to $log_file
#
exec 2> "$log_file"
echononl "Redirect all errors to '\$log_file'.."
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
DEFAULT_NFS_SERVER_PORT="2049"
echononl "Load Configurations from file '$(basename "$conf_file")'."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
echo_ok
else
echo_skipped
msg="No configuration file found. Script default values are loaded.."
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarn\033[m ] $msg"
echo ""
fi
fi
if [[ -z "$NFS_SERVER_IPV4" ]] ; then
NFS_SERVER_IPV4="$(ip a | grep "inet " | grep -v "127\.0" | awk '{print$2}' | cut -d '/' -f1)"
if [[ -z "$NFS_SERVER_IPV4" ]] ; then
fatal "No NFS Server IP Address present."
fi
fi
[[ -z "$NFS_SERVER_PORT" ]] && NFS_SERVER_PORT="$DEFAULT_NFS_SERVER_PORT"
# Get NFS client list
#
# netstat -an | grep 192.168.112.10:2049 \
# | awk '{print" "$5}' \
# | cut -d ':' -f1 \
# | sort
#
echononl "Get NFS client list.."
netstat -an \
| grep ${NFS_SERVER_IPV4}:$NFS_SERVER_PORT \
| awk '{print" "$5}' \
| cut -d ':' -f1 \
| sort >> ${LOCK_DIR}/nfs-clients.list
if [[ -s "$log_file" ]]; then
echo_failed
error "$(cat "$log_file")"
> "$log_file"
else
echo_ok
fi
# Create array of connected clients:
#
echononl "Create array of connected clients.."
declare -a NFS_CLIENT_ARR=()
while read _nfs_client ; do
NFS_CLIENT_ARR+=("$(trim "$_nfs_client")")
done < "${LOCK_DIR}/nfs-clients.list"
if [[ -s "$log_file" ]]; then
echo_failed
error "$(cat "$log_file")"
> "$log_file"
else
echo_ok
fi
if $terminal ; then
echo ""
echo ""
echo " ----------------------"
echo -e " \033[37m\033[1mConnected NFS Clients:\033[m"
echo " ----------------------"
echo ""
fi
if [[ ${#NFS_CLIENT_ARR[@]} -gt 0 ]]; then
for _nfs_client in "${NFS_CLIENT_ARR[@]}" ; do
echo -e " \033[33m$_nfs_client\033[m"
done
info "Number of connected clients: \033[1m${#NFS_CLIENT_ARR[@]}\033[m"
else
info "No NFS Client connected."
fi
clean_up 0