From 5ff18f387435f2682e26dbef874c5479556b7d6a Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 25 Feb 2022 06:47:56 -0800 Subject: [PATCH] restructured, added installer, new functions --- install.sh | 81 ++++ lib/quickinfo.cron | 2 + .bash_aliases => lib/skel/.bash_aliases | 0 .bashrc => lib/skel/.bashrc | 0 .../skel/.bashrc.d}/11-quickinfo.bashrc | 0 functions => lib/skel/.bashrc.d/functions | 272 +++++++------- .vimrc => lib/skel/.vimrc | 0 .../vimfiles/crystallite.vim | 0 vimrc.local => lib/vimfiles/vimrc.local | 2 +- scriptexample.sh | 354 ++++++++++++++++++ 10 files changed, 568 insertions(+), 143 deletions(-) create mode 100644 install.sh create mode 100644 lib/quickinfo.cron rename .bash_aliases => lib/skel/.bash_aliases (100%) rename .bashrc => lib/skel/.bashrc (100%) mode change 100755 => 100644 rename {.bashrc.d => lib/skel/.bashrc.d}/11-quickinfo.bashrc (100%) mode change 100755 => 100644 rename functions => lib/skel/.bashrc.d/functions (74%) mode change 100755 => 100644 rename .vimrc => lib/skel/.vimrc (100%) mode change 100755 => 100644 rename crystallite.vim => lib/vimfiles/crystallite.vim (100%) mode change 100755 => 100644 rename vimrc.local => lib/vimfiles/vimrc.local (90%) create mode 100644 scriptexample.sh diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..03807cb --- /dev/null +++ b/install.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# initial vars +rundir=${0%/*} +source ${rundir}/functions +scriptname=${0##*/} +runuser=$(whoami) + +#-----------------------------------------------------------------# +# Script-specific Parameters +#-----------------------------------------------------------------# + +VERSION=1.0 + +#-----------------------------------------------------------------# +# Script-specific Funcitons +#-----------------------------------------------------------------# + +usage(){ + boxtop + boxline "${lyl}$scriptname - v${VERSION}:${dfl}" + boxline "${lbl}Usage:${dfl}" + boxline "${lyl}$scriptname ${bld}[args]" + boxseparator + boxline "[args:]" + boxline " -i [--install]" + boxline " -r [--remove]" + boxline " -u [--update]" + boxline " -h [--help]" + boxbottom +} + +install(){ + scp -r $rundir/lib/skel/* $HOME + cp $rundir/lib/vimfiles/crystallite.vim /usr/share/vim/vim*/colors/crystallite.vim + cp $rundir/lib/vimfiles/vimrc.local /etc/vim/vimrc.local +} + +remove(){ + echo -e "\n" +} + +update(){ + pushd $rundir + remove + git fetch && git pull + install + popd +} + +#------------------------------------------------------# +# Options and Arguments Parser +#------------------------------------------------------# + +if [[ $runuser == root ]] ; then + case $1 in + -i | --install) + install + ;; + -r | --remove) + remove + ;; + -u | --update) + update + #sleep $ratelimit + exit 0 + ;; + -h | --help) + usage + ;; + *) + warn "Invalid argument $@" + usage + #exit 2 + ;; + esac +else + fail "Must be run as user 'root'" +fi +#------------------------------------------------------# +# Script begins here +#------------------------------------------------------# \ No newline at end of file diff --git a/lib/quickinfo.cron b/lib/quickinfo.cron new file mode 100644 index 0000000..defb3ab --- /dev/null +++ b/lib/quickinfo.cron @@ -0,0 +1,2 @@ +# quickinfo cronjob +0 */1 * * * $HOME/.bashrc.d/11-quickinfo.bashrc -c \ No newline at end of file diff --git a/.bash_aliases b/lib/skel/.bash_aliases similarity index 100% rename from .bash_aliases rename to lib/skel/.bash_aliases diff --git a/.bashrc b/lib/skel/.bashrc old mode 100755 new mode 100644 similarity index 100% rename from .bashrc rename to lib/skel/.bashrc diff --git a/.bashrc.d/11-quickinfo.bashrc b/lib/skel/.bashrc.d/11-quickinfo.bashrc old mode 100755 new mode 100644 similarity index 100% rename from .bashrc.d/11-quickinfo.bashrc rename to lib/skel/.bashrc.d/11-quickinfo.bashrc diff --git a/functions b/lib/skel/.bashrc.d/functions old mode 100755 new mode 100644 similarity index 74% rename from functions rename to lib/skel/.bashrc.d/functions index d47b88e..96704a8 --- a/functions +++ b/lib/skel/.bashrc.d/functions @@ -1,142 +1,130 @@ -#!/bin/bash -# pyr0ball script functions library -# -# Utilities (Setting up colors and bounding boxes) -if [[ "$TERM" != "linux" ]] ; then - red=$(echo -e "\033[38;5;1m") # red - grn=$(echo -e "\033[38;5;2m") # green - ylw=$(echo -e "\033[38;5;3m") # yellow - blu=$(echo -e "\033[38;5;27m") # blue - lbl=$(echo -e "\033[38;5;69m") # light blue - mag=$(echo -e "\033[38;5;5m") # magenta - cyn=$(echo -e "\033[38;5;6m") # cyan - pur=$(echo -e "\033[38;5;135m") # purple - ong=$(echo -e "\033[38;5;166m") # orange - lyl=$(echo -e "\033[38;5;228m") # light yellow - lrd=$(echo -e "\033[38;5;196m") # light red - norm=$(echo -e "\033[38;5;9m") # default/normal - # - bld=$(echo -e "\033[1m") # bold - unl=$(echo -e "\033[4m") # underline - blk=$(echo -e "\033[7m") # blinking not supported in our xterm, so reverse instead - nln=$(echo -e "\033[24") - dfl=$(echo -e "\e[0m") -fi - -# For drawing pretty menus and prompts -terminal_width=$(tput cols) -if [ $terminal_width -le 81 ] ; then - BOXWIDTH=$((terminal_width - 1)) -else - BOXWIDTH=80 -fi -top_border=$(echo -e '\u2501') -left_border=$(echo -e '\u2503') -right_border=$(echo -e '\u2503') -left_top_border=$(echo -e '\u250f') -right_top_border=$(echo -e '\u2513') -left_bottom_border=$(echo -e '\u2517') -right_bottom_border=$(echo -e '\u251b') - -repchar(){ - n=1 - while [ $n -le $2 ] ; do - echo -n "$1" - n=$((n+1)) - done -} - -boxtop(){ - echo -n "$left_top_border" - repchar "$top_border" $((BOXWIDTH-1)) - echo -n "$right_top_border" - echo -} - -boxbottom(){ - echo -n "$left_bottom_border" - repchar "$top_border" $((BOXWIDTH-1)) - echo -n "$right_bottom_border" - echo -} - -boxline(){ - echo -e "$left_border $1\r\033[${BOXWIDTH}C$right_border" -} - -boxseparator(){ - echo -n "$left_border" - repchar "-" $((BOXWIDTH-1)) - echo -n "$right_border" - echo -} - -success(){ - echo -e "\n" - boxtop - boxline " ${script_name} ${grn}SUCCESS${dfl}$@" - boxbottom - exit 0 -} - -warn(){ - echo -e "\n" - ec=$? - boxtop - boxline "${lrd}WARNING${lyl}[${ong}code=${red}$ec${lyl}]: $@${dfl}" - boxbottom -} - -fail(){ - ec=$? - echo -e "\n" - boxtop - boxline "${lrd}FAILED${lyl}[${ong}code=${red}$ec${lyl}]: $@${dfl}" - boxbottom - exit $ec -} - -pushd(){ - command pushd "$@" > /dev/null -} - -popd(){ - command popd "$@" > /dev/null -} - -popdfail(){ - ec=$? - popd - $(exit $ec) #restore the exit code - fail "$@" -} - -ctrl_c(){ - fail "User interrupted with Ctrl-C" -} - -# ensure ctrl-c to cancel script ends the entire process and not just the current function -trap ctrl_c INT - -# ensure runuser is service -#[ "$(whoami)" == "service" ] || fail "Must be run as service user (no sudo)" - -#-----------------------------------------------------------------# -# Script-specific Funcitons -#-----------------------------------------------------------------# - - -#------------------------------------------------------# -# Setup -#------------------------------------------------------# - - -#------------------------------------------------------# -# Options and Arguments Parser -#------------------------------------------------------# - - -#------------------------------------------------------# -# Script begins here -#------------------------------------------------------# - +#!/bin/bash +# pyr0ball script functions library + +# Initial Vars +scriptname=${0##*/} +rundir=${0%/*} +runuser=$(whoami) + +# Utilities (Setting up colors and bounding boxes) +if [[ "$TERM" != "linux" ]] ; then + red=$(echo -e "\033[38;5;1m") # red + grn=$(echo -e "\033[38;5;2m") # green + ylw=$(echo -e "\033[38;5;3m") # yellow + blu=$(echo -e "\033[38;5;27m") # blue + lbl=$(echo -e "\033[38;5;69m") # light blue + mag=$(echo -e "\033[38;5;5m") # magenta + cyn=$(echo -e "\033[38;5;6m") # cyan + pur=$(echo -e "\033[38;5;135m") # purple + ong=$(echo -e "\033[38;5;166m") # orange + lyl=$(echo -e "\033[38;5;228m") # light yellow + lrd=$(echo -e "\033[38;5;196m") # light red + norm=$(echo -e "\033[38;5;9m") # default/normal + # + bld=$(echo -e "\033[1m") # bold + unl=$(echo -e "\033[4m") # underline + blk=$(echo -e "\033[7m") # blinking not supported in our xterm, so reverse instead + nln=$(echo -e "\033[24") + dfl=$(echo -e "\e[0m") +fi + +# For drawing pretty boxes +terminal_width=$(tput cols) +if [ $terminal_width -le 81 ] ; then + BOXWIDTH=$((terminal_width - 1)) +else + BOXWIDTH=80 +fi +top_border=$(echo -e '\u2501') +left_border=$(echo -e '\u2503') +right_border=$(echo -e '\u2503') +left_top_border=$(echo -e '\u250f') +right_top_border=$(echo -e '\u2513') +left_bottom_border=$(echo -e '\u2517') +right_bottom_border=$(echo -e '\u251b') + +repchar() { + n=1 + while [ $n -le $2 ] ; do + echo -n "$1" + n=$((n+1)) + done +} + +boxtop() { + echo -n "$left_top_border" + repchar "$top_border" $((BOXWIDTH-1)) + echo -n "$right_top_border" + echo +} + +boxbottom() { + echo -n "$left_bottom_border" + repchar "$top_border" $((BOXWIDTH-1)) + echo -n "$right_bottom_border" + echo +} + +boxline() { + echo -e "$left_border $1\r\033[${BOXWIDTH}C$right_border" +} + +boxseparator(){ + echo -n "$left_border" + repchar "-" $((BOXWIDTH-1)) + echo -n "$right_border" + echo +} + +success(){ + echo -e "\n" + boxtop + boxline " ${scriptname} ${grn}SUCCESS${dfl}$@" + boxbottom + exit 0 +} + +warn(){ + echo -e "\n" + ec=$? + boxtop + boxline "${lrd}WARNING${lyl}[${ong}code=${red}$ec${lyl}]: $@${dfl}" + boxbottom +} + +fail(){ + ec=$? + echo -e "\n" + boxtop + boxline "${lrd}FAILED${lyl}[${ong}code=${red}$ec${lyl}]: $@${dfl}" + boxbottom + exit $ec +} + +pushd(){ + command pushd "$@" > /dev/null +} + +popd(){ + command popd "$@" > /dev/null +} + +popdfail(){ + ec=$? + popd + $(exit $ec) #restore the exit code + fail "$@" +} + +logger(){ + #$@ 2>&1 | tee >(ts "[$scriptname][%d-%m-%y %H_%M_%S]" > $logfile) # This version of prepend requires the 'ts' utility from 'moreutils' package + $@ 2>&1 | tee >(while IFS= read -r line; do printf '[%s] %s\n' "${scriptname}][$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done >> $logfile) +} + +ctrl_c(){ + echo -e "\n" + fail "User interrupted with Ctrl-C" +} + +# ensure ctrl-c to cancel script ends the entire process and not just the current function +trap ctrl_c INT diff --git a/.vimrc b/lib/skel/.vimrc old mode 100755 new mode 100644 similarity index 100% rename from .vimrc rename to lib/skel/.vimrc diff --git a/crystallite.vim b/lib/vimfiles/crystallite.vim old mode 100755 new mode 100644 similarity index 100% rename from crystallite.vim rename to lib/vimfiles/crystallite.vim diff --git a/vimrc.local b/lib/vimfiles/vimrc.local similarity index 90% rename from vimrc.local rename to lib/vimfiles/vimrc.local index aa3294d..a7df484 100644 --- a/vimrc.local +++ b/lib/vimfiles/vimrc.local @@ -1,7 +1,7 @@ if $TERM !~ "linux" syntax on set t_Co=256 - colorscheme bdv_crystallite + colorscheme crystallite else syntax on colorscheme default diff --git a/scriptexample.sh b/scriptexample.sh new file mode 100644 index 0000000..3700125 --- /dev/null +++ b/scriptexample.sh @@ -0,0 +1,354 @@ +#!/bin/bash + +# Moxa NPort Driver loader and watchdog script + +# initial vars +scriptname=${0##*/} +rundir=${0%/*} + +source ${rundir}/functions + +#-----------------------------------------------------------------# +# Script-specific Parameters +#-----------------------------------------------------------------# + +VERSION=1.0 +logfile=/var/log/bdv/moxaloader.log +ratelimit=15 +rst_threshold=1 +god=/opt/ruby-enterprise/bin/god +pingfile=/usr/lib/npreal2/driver/pingup +daemonpid=$(ps -ef | grep -v grep | grep moxaloader.*daemon | awk {'print $2'}) +config_sh=$(grep DNS /.AVC-X/etc/networking/profiles/default/ifcfg-Public) +resolv_conf=$(grep nameserver /.AVC-X/etc/networking/profiles/default/resolv.conf) +config_dev=$(grep DNS /.AVC-X/etc/networking/devices/ifcfg-Public) +sys_resolv_conf=$(grep nameserver /etc/resolv.conf) +etc_net_sh=$(grep DNS /etc/network-scripts/ifcfg-Public) +dns=$(grep DNS1 /.AVC-X/etc/networking/profiles/default/ifcfg-Public | cut -c 6-) + +#-----------------------------------------------------------------# +# Script-specific Funcitons +#-----------------------------------------------------------------# + + +usage(){ + boxtop + boxline "${lyl}Moxa NPort Watchdog & Recovery v$VERSION${dfl}" + boxseparator + boxline "Usage:" + boxline "$scriptname [FUNCTIONS][-d|--daemon|--usage|--version]" + boxseparator + boxline "-s | --start - Initializes the Moxa NPort Driver only and exits" + boxline "-S | --stop - stops the NPort driver and any ongoing daemonized watchdog" + boxline "-r | --restart - Restarts all BDV hardware services, and re-initialized moxa if DNS is valid" + boxline "-R | --force-restart - forces a restart of both the NPort driver and BDV services (assume -9)" + boxline "-p | --ping - Detect nPort online and write 'pingup' file if true or remove if false" + boxline "-P | --pid - returns the PID's of any ongoing NPort Driver processes" + boxline "-d | --daemon - daemonizes the service as a watchdog" + boxline "-F | --fix-dns - Checks system's DNS config and removes entries if invalid" + boxline "-u | --usage - Show this help text" + boxline "-h | --help - Show this help text" + boxline "-v | --version - returns the version of this script" + boxbottom +} + +removedns(){ + # Look for existing DNS entries if there are any + config_sh=$(grep DNS /.AVC-X/etc/networking/profiles/default/ifcfg-Public) + resolv_conf=$(grep nameserver /.AVC-X/etc/networking/profiles/default/resolv.conf) + resolv_search=$(grep search /.AVC-X/etc/networking/profiles/default/resolv.conf) + config_dev=$(grep DNS /.AVC-X/etc/networking/devices/ifcfg-Public) + sys_resolv_conf=$(grep nameserver /etc/resolv.conf) + sys_resolv_search=$(grep search /etc/resolv.conf) + etc_net_sh=$(grep DNS /etc/network-scripts/ifcfg-Public) + # Checks if the above variables are empty, and if all are, exits the function only + if [[ -z "$config_sh" && -z "$resolv_conf" && -z "$resolv_search" && -z "$config_dev" && -z "$sys_resolv_conf" && -z "$sys_resolv_search" && -z "$etc_net_sh" ]] ; then + logger echo -e "${grn}No DNS entries found${dfl}" + return 0 + else + # If either of the above variables are not empty, proceeds with checking + # and printing their contents + if [[ ! -z "$config_sh" ]] ; then + logger echo -e "${lbl}Current DNS is set to:${dfl}" + logger echo -e "$config_sh" + # sed uses regex to detect and delete lines out of the configuration script + # files that contain DNS entries + sudo sed -i '/DNS/d' /.AVC-X/etc/networking/profiles/default/ifcfg-Public && logger echo -e "${ylw}Successfully removed DNS entries from ifcfg-Public${dfl}\n" || logger fail "${red}Unable to remove DNS entries from /.AVC-X/etc/networking/profiles/default/ifcfg-Public${dfl}" + fi + if [[ ! -z "$resolv_conf" ]] ; then + logger echo -e "${lbl}Current resolv.conf entry is${dfl}" + logger echo -e "$resolv_conf" + sudo sed -i '/nameserver/d' /.AVC-X/etc/networking/profiles/default/resolv.conf && logger echo -e "${ylw}Successfully removed DNS entries from resolv.conf${dfl}\n" || logger warn "${red}Unable to remove DNS entries from /.AVC-X/etc/networking/profiles/default/resolv.conf${dfl}" + fi + if [[ ! -z "$resolv_conf" ]] ; then + logger echo -e "${lbl}Current resolv.conf is searching${dfl}" + logger echo -e "$resolv_search" + sudo sed -i '/search/d' /.AVC-X/etc/networking/profiles/default/resolv.conf && logger echo -e "${ylw}Successfully removed DNS entries from resolv.conf${dfl}\n" || logger warn "${red}Unable to remove DNS entries from /.AVC-X/etc/networking/profiles/default/resolv.conf${dfl}" + fi + if [[ ! -z "$config_dev" ]] ; then + logger echo -e "${lbl}Current DNS is set to:${dfl}" + logger echo -e "$config_dev" + # sed uses regex to detect and delete lines out of the configuration script + # files that contain DNS entries + sudo sed -i '/DNS/d' /.AVC-X/etc/networking/devices/ifcfg-Public && logger echo -e "${ylw}Successfully removed DNS entries from ifcfg-Public${dfl}\n" || logger fail "${red}Unable to remove DNS entries from /.AVC-X/etc/networking/devices/ifcfg-Public${dfl}" + fi + if [[ ! -z "$sys_resolv_conf" ]] ; then + logger echo -e "${lbl}Current resolv.conf entry is${dfl}" + logger echo -e "$sys_resolv_conf" + sudo sed -i '/nameserver/d' /.AVC-X/etc/networking/profiles/default/resolv.conf && logger echo -e "${ylw}Successfully removed DNS entries from /etc/resolv.conf${dfl}\n" || logger warn "${red}Unable to remove DNS entries from /etc/resolv.conf${dfl}" + fi + if [[ ! -z "$sys_resolv_search" ]] ; then + logger echo -e "${lbl}Current resolv.conf is searching${dfl}" + logger echo -e "$sys_resolv_search" + sudo sed -i '/search/d' /.AVC-X/etc/networking/profiles/default/resolv.conf && logger echo -e "${ylw}Successfully removed DNS entries from /etc/resolv.conf${dfl}\n" || logger warn "${red}Unable to remove DNS entries from /etc/resolv.conf${dfl}" + fi + if [[ ! -z "$etc_net_sh" ]] ; then + logger echo -e "${lbl}Current DNS is set to:${dfl}" + logger echo -e "$etc_net_sh" + # sed uses regex to detect and delete lines out of the configuration script + # files that contain DNS entries + sudo sed -i '/DNS/d' /etc/network-scripts/ifcfg-Public && logger echo -e "${ylw}Successfully removed DNS entries from ifcfg-Public${dfl}\n" || logger fail "${red}Unable to remove DNS entries from /.AVC-X/etc/networking/profiles/default/ifcfg-Public${dfl}" + fi + return 1 + fi +} + +dnscheck(){ + # The Moxa drivers are built in such a way that they will hang if no outside + # network is available, unless DNS entries are not present. Below checks for + # external network access, then removes dns entries if outside network is unavailable + + # Check if dns entries exist + if [[ ! -z "$config_sh" || ! -z "$resolv_conf" || ! -z "$config_dev" || ! -z "$sys_resolv_conf" || ! -z "$etc_net_sh" ]] ; then + # If DNS entries are found, check that the server can ping them + logger echo -e "DNS Entries found, checking for internet connection" + ping -c 1 $dns >/dev/null 2>&1 + if [[ $? != 0 ]] ; then + # If DNS entries are found, but not responding, + # remove DNS entries to prevent drivers from hanging on boot + logger echo -e "${ong}DNS $dns is not responding to ping${dfl}" + logger echo -e "Removing DNS entries from network configuration" + return 1 + else + # If DNS entries are found, but responding, leave the entries intact + logger echo -e "${grn}DNS is responding, entries valid.${dfl}" + logger echo -e "${lyl}Proceeding with initialization.${dfl}" + return 0 + fi + else + logger echo -e "DNS Entries not found, skipping DNS pingcheck" + return 0 + fi +} + +dnspurge(){ + # Run first dns check and removal + removedns + # Run network reset + sudo /sbin/service network restart + # Run second dns check for lingering entries + logger echo -e "${pur}Re-checking dns entries${dfl}" + removedns +} + +moxatty(){ + ls /dev/tty* | grep -c ttyr00 +} + +# Checks moxa's online status and stores state via external 'pingtest' file +online(){ + if [ ! -f $pingfile ] ; then + if ping -c 1 -W 1 $@ &> /dev/null ; then + echo 2 + else + echo 1 + fi + else + if ping -c 1 -W 1 $@ &> /dev/null ; then + echo 0 + else + echo 3 + fi + fi +} + +# Checks moxa's online status and stores state in a variable +pingtest(){ + if [[ $moxaonline == true ]] ; then + if ping -c 1 -W 1 $@ &> /dev/null ; then + echo 0 + else + echo 3 + fi + else + if ping -c 1 -W 1 $@ &> /dev/null ; then + echo 2 + else + echo 1 + fi + fi +} + +# Launch the Moxa NPort loader application +start(){ + /usr/lib/npreal2/driver/mxloadsvr +} + +# Force terminate any instances of the Moxa NPort application +stop(){ + sudo pkill -9 npreal2d +} + +# Force stop of any daemonized instances of this script +fullstop(){ + stop + sudo kill -9 $daemonpid +} + +# Restart of multiple services is required to re-establish device control after +# the NPort loses its connection. The order of restarting these services is +# significant, and should be maintained. +restart(){ + stop + start + god restart rs232 + sudo /sbin/service SimpleHttpDeviceServer restart_noscript + sudo /sbin/service BDVHardwareManager restart + god restart steris_act +} + +conditionalrestart(){ + if [[ $(dnscheck) == 0 ]] ; then + stop + start + fi + god restart rs232 + sudo /sbin/service SimpleHttpDeviceServer restart_noscript + sudo /sbin/service BDVHardwareManager restart + god restart steris_act +} + +# Clears loop flag variables +clearflags(){ + moxaonline= + pingfails= + state= +} + +#------------------------------------------------------# +# Options and Arguments Parser +#------------------------------------------------------# + +case $1 in + -s | start | --start) + start + exit 0 + ;; + -S | stop | --stop) + fullstop + exit $? + ;; + -R | --force-restart) + fullstop + restart + exit 0 + ;; + -r | restart | --restart) + conditionalrestart + exit 0 + ;; + -p | ping | --ping) + online moxa + exit $? + ;; + -P | pid | --pid) + echo -e "$daemonpid" + exit $? + ;; + -d | daemon | --daemon) + start + ;; + -u | --usage) + usage + exit -1 + ;; + -h | --help) + usage + exit -1 + ;; + -v | --version) + echo $VERSION + exit 0 + ;; + -F | --fix-dns) + purgedns + exit 0 + ;; + *) + warn "Invalid argument $@" + usage + exit -1 + ;; +esac + +#-----------------------------------------------------------------# +# Process substitution to fork log output +# Only uncomment if all script output should be logged! +#-----------------------------------------------------------------# + +# exec &> >(tee $logfile) + +#------------------------------------------------------# +# Script begins here +#------------------------------------------------------# + +logger boxtop +logger boxline "${lbl}Moxa NPort Driver Loader v$VERSION${dfl}" +logger boxbottom + +# Unset flags +clearflags + +while true ; do + state=$(online moxa) + case $state in + 0) # State 0 = Moxa is online, and was online in previous loop. No action needed + boxtop + boxline "${grn}Moxa NPort Online...${dfl}" + boxbottom + moxaonline=true + ;; + 1) # State 1 = Moxa is offline, and was offline in previous loop. No action needed + boxtop + boxline "${lrd}Moxa NPort Offline${dfl}" + boxbottom + moxaonline=false + failpings=$((failpings+1)) + ;; + 2) # State 2 = Moxa is online, but was offline in previous loop. Service Restart required to restore functionality and update moxaonline state + #if [[ $rst_threshold -gt $failpings ]] ; then + logger boxtop + logger boxline "${grn}Moxa NPort Came Online!${dfl}" + logger boxline "... ${lyl}Restarting Services${dfl} ..." + logger boxbottom + moxaonline=true + touch $pingfile + conditionalrestart + #fi + ;; + 3) # State 3 = Moxa is offline, but was online in previous loop. Start counting failed pings, and update moxaonline state + logger boxtop + logger boxline "${grn}Moxa NPort Has Gone Offline${dfl}" + logger boxline "${lyl}Awaiting Response before Restart${dfl} ..." + logger boxbottom + moxaonline=false + rm $pingfile + failpings=1 + ;; + esac + boxtop + boxline "Moxa Online = $moxaonline" + boxline "Checking again in $ratelimit seconds..." + boxbottom + sleep $ratelimit +done