diff --git a/conf/check-connectivity.conf.BAK b/conf/check-connectivity.conf.BAK deleted file mode 100644 index 5b8a50f..0000000 --- a/conf/check-connectivity.conf.BAK +++ /dev/null @@ -1,33 +0,0 @@ -# ------------------------------------------- -# - Settings for script check-connectivity.sh -# ------------------------------------------- - -# - IPV6_PRESENT -# - -# - Possible values: true/false -# - -# - Example: -# - IPV6_PRESENT=false -# - -# - Default value: true -# - -#IPV6_PRESENT=true - - -# - DNS_CHECK_HOSTS -# - -# - Blank sepatated list of hostnames checked by lokal nameservice simply -# - for their ip-addresses -# - -# - Defaults to: DNS_CHECK_HOSTS="www.oopen.de www.google.com www.heise.de" -# - -#DNS_CHECK_HOSTS="www.oopen.de www.google.com www.heise.de www.debian.org" - - -# - HTTPS_CHECK_HOSTS -# - -# - Blank sepatated list of websites which will be checked for accessibility -# - -# - Defaults to: HTTPS_CHECK_HOSTS="www.oopen.de www.google.com www.heise.de" -# - -#HTTPS_CHECK_HOSTS="www.oopen.de www.google.com www.heise.de www.debian.org" diff --git a/conf/show-nfs-clients.conf.sample b/conf/show-nfs-clients.conf.sample new file mode 100644 index 0000000..ecea692 --- /dev/null +++ b/conf/show-nfs-clients.conf.sample @@ -0,0 +1,20 @@ +# ----------------------------------------- +# - Settings for script show-nfs-clients.sh +# ----------------------------------------- + +# NFS_SERVER_IPV4 +# +# The IP-Address where the nfs clients are connected +# +# If not given, script tries to determin th ip-address +# +#NFS_SERVER_IPV4="" + + +# NFS_SERVER_PORT +# +# The Port where the nfs service is listen +# +# Default: NFS_SERVER_PORT=2049 +# +#NFS_SERVER_PORT=2049 diff --git a/show-nfs-clients.sh b/show-nfs-clients.sh new file mode 100755 index 0000000..08659ed --- /dev/null +++ b/show-nfs-clients.sh @@ -0,0 +1,289 @@ +#!/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