diff --git a/defaults.inc b/defaults.inc index f9c1150..62ad107 100755 --- a/defaults.inc +++ b/defaults.inc @@ -1,6 +1,6 @@ #!/usr/bin/env bash -VERS='3.9.28-07102023' +VERS='3.9.29-07102023' MMFOLDER="/opt/idssys/mediamanager" TMPFOLDER="${MMFOLDER}/.tmp" @@ -17,7 +17,7 @@ MM_LOGFILE=${LOGFOLDER}/logfile MM_OUTPUT_LOGFILE=${LOGFOLDER}/outputs.check.log MM_BACKUP_FOLDER='/opt/!MM-Service-Backups' -[ -z ${MM_VPN_RESTART+x} ] && MM_VPN_RESTART=86400 +([ -z ${MM_VPN_RESTART+x} ] || [ ${MM_VPN_RESTART+x} -gt 86400 ]) && MM_VPN_RESTART=86400 [ ! -f ${MM_LOGFILE} ] && touch ${MM_LOGFILE} [ ! -f ${MM_OUTPUT_LOGFILE} ] && touch ${MM_OUTPUT_LOGFILE} diff --git a/mm-scripts.sh b/mm-scripts.sh index 0039fb1..cd4a6ff 100755 --- a/mm-scripts.sh +++ b/mm-scripts.sh @@ -33,7 +33,6 @@ if [[ "${noheader}" != *" ${1} "* ]] && [[ "${noheader}" != *" ${2} "* ]] || ([ DIVIDER . lightGreen fi - cw_spc=40 MMCRONCHECK(){ @@ -45,14 +44,14 @@ MMCRONCHECK(){ if [ $(date +%u) -eq 6 ] && [ $(date +%H)$(date +%M) -ge 0400 ] && [ $(date +%H)$(date +%M) -le 0405 ] ; then if [ $(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vers.info)) -gt 600 ]; then UPDATESERVICE service all q >/dev/null 2>&1 - echo "$(date) - Updated All Services" >> $logfile + echo "$(date) - Updated All Services" >> ${MM_LOGFILE} fi elif [ $(date +%H)$(date +%u) -eq 6 ] && [ $(date +%H)$(date +%M) -ge 0300 ] && [ $(date +%H)$(date +%M) -lt 0305 ] ; then if [ $(expr `date +%s` - $(stat -c %Y ${MMFOLDER}/.git/ORIG_HEAD)) -gt 600 ] && [ "`git -C /opt/idssys/nodemgmt log --pretty=%H ...refs/heads/${GBRANCH}^ | head -n 1`" != "`git -C /opt/idssys/nodemgmt ls-remote origin -h refs/heads/${GBRANCH} |cut -f1`" ]; then echo "updating mm executing" /usr/local/bin/mm update q >/dev/null 2>&1 source /opt/idssys/mediamanager/defaults.inc - echo "$(date) - Updated MM Scripts to v${VERS}" >> $logfile + echo "$(date) - Updated MM Scripts to v${VERS}" >> ${MM_LOGFILE} fi fi @@ -60,9 +59,9 @@ MMCRONCHECK(){ if [ -f ${TMPFOLDER}/mm.stop ]; then /usr/local/bin/mm check q >/dev/null 2>&1 else - echo -e "$(date) - Starting log #############################" >> ${outputlogfile} - /usr/local/bin/mm check q >> ${outputlogfile} - echo -e "$(date) - Finished log #############################" >> ${outputlogfile} + echo -e "$(date) - Starting log #############################" >> ${MM_OUTPUT_LOGFILE} + /usr/local/bin/mm check q >> ${MM_OUTPUT_LOGFILE} + echo -e "$(date) - Finished log #############################" >> ${MM_OUTPUT_LOGFILE} fi exit 0 @@ -78,12 +77,12 @@ MMSTART(){ elif [ "${1}" == "check" ] && [ "$(/bin/ip link | grep "${VPN_INTERFACE}")" == "" ] && [ ! -f ${TMPFOLDER}/mm.vpn* ] && [ ! -f ${TMPFOLDER}/vpninfo.* ]; then GOFORSTART=true; SENDNOTICE "Starting Media Management Systems" "Fresh Startup" - echo "$(date) - Starting Media Management system - (fresh start)" >> $logfile + echo "$(date) - Starting Media Management system - (fresh start)" >> ${MM_LOGFILE} echo -e "${idsCL[LightCyan]}Fresh Startup${idsCL[Default]}" elif [ "${1}" = "check" ] && [ "$(/bin/ip link | grep "${VPN_INTERFACE}")" = "" ] && [ -f ${TMPFOLDER}/mm.vpn* ]; then GOFORSTART=true; - echo "$(date) - Attempting to restart systems after VPN failure..." >> $logfile + echo "$(date) - Attempting to restart systems after VPN failure..." >> ${MM_LOGFILE} echo -e "${idsCL[LightCyan]}Attempting to restart systems after VPN failure...${idsCL[Default]}" SENDNOTICE "Attempting to start systems after VPN failure..." @@ -130,13 +129,13 @@ MMSTART(){ mmsv='Verifying' fi - [ $VERBOSE = true ] && echo "$(date) - ${mmsv} VPN..." >> $logfile + [ $VERBOSE = true ] && echo "$(date) - ${mmsv} VPN..." >> ${MM_LOGFILE} OUT=$(/bin/ip link | grep "$VPN_INTERFACE") if [ ${#OUT} = 0 ]; then if [ -f ${TMPFOLDER}/mm.vpn.fail ]; then echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}" - echo "$(date) - VPN Failure #2 - restarting system" >> $logfile + echo "$(date) - VPN Failure #2 - restarting system" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "2nd Failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail2 rm -f ${TMPFOLDER}/mm.vpn.fail @@ -149,14 +148,14 @@ MMSTART(){ exit 1 elif [ -f ${TMPFOLDER}/mm.vpn.fail2 ]; then echo -e "${idsCL[Red]} failed, shutting down system${idsCL[Default]}" - echo "$(date) - VPN Failure #3 - Shutting down Media Management systems" >> $logfile + echo "$(date) - VPN Failure #3 - Shutting down Media Management systems" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "Final Failure, shutting down system" 2 MMSTOP [ $VERBOSE = true ] && DEBUGLOGSTOP exit 1 else echo -e "${idsCL[Red]} failed, restarting system${idsCL[Default]}" - echo "$(date) - VPN Failure #1 - restarting system" >> $logfile + echo "$(date) - VPN Failure #1 - restarting system" >> ${MM_LOGFILE} SENDNOTICE "VPN Failure" "1st Failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP @@ -179,8 +178,14 @@ MMSTART(){ vpnuptime=$(expr `date +%s` - $(stat -c %Y ${TMPFOLDER}/vpninfo)) if [ ${vpnuptime} -ge ${MM_VPN_RESTART} ]; then - echo "$(date) - 24hr session limit reached, restarting vpn & services" >> $logfile - echo -en "${idsCL[LightCyan]}24hr session limit reached, restarting vpn & services ." + if [ ${MM_VPN_RESTART} -eq 86400 ]; then + echo "$(date) - 24hr session limit reached, restarting vpn & services" >> ${MM_LOGFILE} + echo -en "${idsCL[LightCyan]}24hr session limit reached, restarting vpn & services" + else + $(date -d@${MM_VPN_RESTART} -u "+%H hr(s), %M min(s) & %S sec(s)")' + echo "$(date) - Your '$(date -d@${MM_VPN_RESTART} -u "+%H hr(s), %M min(s) & %S sec(s)")' session limit reached, restarting vpn & services" >> ${MM_LOGFILE} + echo -en "${idsCL[LightCyan]}24hr session limit reached, restarting vpn & services" + fi [ -f ${TMPFOLDER}/mm.noservices ] && rv="vpn" || rv="" MMSTOP >/dev/null 2>&1 echo -en "." @@ -201,7 +206,7 @@ MMSTART(){ # if [ "${zerogw}" != "${defaultgw}" ]; then # if [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then if [ "${zerogw}" != "${defaultgw}" ] || [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then - echo "$(date) - Adding VPN default routes" >> $logfile + echo "$(date) - Adding VPN default routes" >> ${MM_LOGFILE} msg="Adding VPN default routes" c=0; spc=''; spc1=$((${cw_spc}-${#msg})) @@ -248,7 +253,7 @@ MMSTART(){ elif [ ${cc} -eq 10 ]; then echo -e "${idsCL[Red]}Could not confirm default routes, restarting system!${idsCL[Default]}" - echo "$(date) - Could not confirm default routes, restarting system" >> $logfile + echo "$(date) - Could not confirm default routes, restarting system" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Default route failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail @@ -283,8 +288,8 @@ MMSTART(){ if [ ${cc} -eq 10 ]; then echo echo -e "${idsCL[Red]}Cannot get an WAN address, restarting system!${idsCL[Default]}" - echo "$(date) - Cannot get an WAN address, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> $logfile - [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile + echo "$(date) - Cannot get an WAN address, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> ${MM_LOGFILE} + [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Cannot get an WAN address, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP >/dev/null 2>&1 @@ -300,12 +305,12 @@ MMSTART(){ done if [ "${REALWANIP}" != "${mmip}" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" - [ $VERBOSE = true ] && echo "$(date) - WAN Address: ${mmip} - GW Address: ${defaultgw}" >> $logfile + [ $VERBOSE = true ] && echo "$(date) - WAN Address: ${mmip} - GW Address: ${defaultgw}" >> ${MM_LOGFILE} else echo echo -e "${idsCL[Red]}Traffic is not routing through VPN, restarting system!${idsCL[Default]}" - echo "$(date) - Traffic is not routing through VPN, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> $logfile - [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile + echo "$(date) - Traffic is not routing through VPN, restarting system! wanip(${REALWANIP}) vpnip(${mmip})" >> ${MM_LOGFILE} + [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> ${MM_LOGFILE} SENDNOTICE "Network Failure" "Traffic route failure, will try and restart the VPN" 1 touch ${TMPFOLDER}/mm.vpn.fail MMSTOP @@ -326,7 +331,7 @@ MMSTART(){ ### CHECK FOR PLEX MOUNT if [ ! -f ${PLEX_MOUNT}/mounted ] && [ "${2}" != "vpn" ] && [ ! -f ${TMPFOLDER}/mm.noservices ]; then - echo "$(date) - Plex Media Not Mounted, will try and mount" >> $logfile + echo "$(date) - Plex Media Not Mounted, will try and mount" >> ${MM_LOGFILE} umount ${PLEX_MOUNT} >/dev/null 2>&1 sleep 2s mount -a >/dev/null 2>&1 @@ -346,7 +351,7 @@ MMSTART(){ if [ ${PMFS} = true ]; then [ -f "${TMPFOLDER}/mm.plexmount.fail2" ] && touch ${TMPFOLDER}/mm.plexmount.fail2 || touch ${TMPFOLDER}/mm.plexmount.fail SENDNOTICE "Plex Media Not Mounted" "Could not mount the Plex Media Folder" 1 - echo "$(date) - Plex Media could not be mounted" >> $logfile + echo "$(date) - Plex Media could not be mounted" >> ${MM_LOGFILE} fi if [ ! -f ${TMPFOLDER}/mm.plexmount.fail2 ]; then echo -e "${idsCL[Red]}Not Mounted${idsCL[Default]}" @@ -360,7 +365,7 @@ MMSTART(){ else echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}" SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!" - echo "$(date) - Plex Media now mounted, restarting services" >> $logfile + echo "$(date) - Plex Media now mounted, restarting services" >> ${MM_LOGFILE} for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl restart ${srvc} >/dev/null 2>&1 & done @@ -373,7 +378,7 @@ MMSTART(){ if [ -f ${TMPFOLDER}/mm.plexmount.fail ]; then echo -e "${idsCL[Green]}Mounted, fixed${idsCL[Default]}" SENDNOTICE "Plex Media Mounted" "The Plex Media Folder was successfully re-mounted again!" - echo "$(date) - Plex Media now mounted, restarting services" >> $logfile + echo "$(date) - Plex Media now mounted, restarting services" >> ${MM_LOGFILE} for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl restart ${srvc} >/dev/null 2>&1 & done @@ -420,7 +425,7 @@ MMSTART(){ if [ -f ${TMPFOLDER}/mm.${srvc}.starting ]; then msg="${srvc^} Service is not starting properly, will try and update" echo -e "${idsCL[Red]}${msg}${idsCL[Default]}" - echo "$(date) - ${msg}" >> $logfile + echo "$(date) - ${msg}" >> ${MM_LOGFILE} SENDNOTICE "${srvc^} Service Issue" "${msg}" 1 tryupdate="true" @@ -472,14 +477,14 @@ MMSTART(){ MMSTATUS fi if [ -f ${TMPFOLDER}/mm.vpn.fail* ]; then - echo "$(date) - VPN issue has been fixed" >> $logfile + echo "$(date) - VPN issue has been fixed" >> ${MM_LOGFILE} SENDNOTICE "Services Restored" "VPN issue has been fixed" rm -f ${TMPFOLDER}/mm.vpn.fail* fi rm -f ${TMPFOLDER}/mm.stop* echo -e "${idsCL[Green]}All systems are go!${idsCL[Default]}" if [ "${1}" = "start" ] || [ "${1}" = "" ]; then - echo "$(date) - Media Management systems started" >> $logfile + echo "$(date) - Media Management systems started" >> ${MM_LOGFILE} fi fi @@ -560,7 +565,7 @@ MMSTOP(){ [ "$(/bin/ip route show | grep default | cut -d' ' -f 3)" != "${DEFAULT_GW}" ] && /bin/ip route add default via ${DEFAULT_GW} [ "${vpn_server_address}" != "" ] && /bin/ip route del ${vpn_server_address} - [ $VERBOSE = true ] && echo "$(date) - Added default route back in" >> $logfile + [ $VERBOSE = true ] && echo "$(date) - Added default route back in" >> ${MM_LOGFILE} echo -e "${idsCL[Green]}Done${idsCL[Default]}" @@ -570,7 +575,7 @@ MMSTOP(){ rm -f ${TMPFOLDER}/mm.plexmount.fail* rm -f ${TMPFOLDER}/mm.stop.checking - echo "$(date) - Media Management systems stopped" >> $logfile + echo "$(date) - Media Management systems stopped" >> ${MM_LOGFILE} echo echo -e "${idsCL[Red]}All services have been stopped${idsCL[Default]}" @@ -883,7 +888,7 @@ UPDATESERVICE(){ elif [ "${2}" != "" ]; then usrvc=${2} echo "${usrvc} Update" > ${TMPFOLDER}/mm.update.stop - echo "$(date) - Updating Service (${usrvc}) : Starting" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Starting" >> ${MM_LOGFILE} echo echo -e "${idsCL[Green]}Updating ${usrvc^} Service${idsCL[Default]}" DIVIDER . lightGreen @@ -905,10 +910,10 @@ UPDATESERVICE(){ echo -en "${idsCL[White]}${msg}${spc} : ${idsCL[Default]}" /bin/systemctl restart nginx >/dev/null 2>&1 echo -e "${idsCL[Green]}Done${idsCL[Default]}" - echo "$(date) - Updating Service (${usrvc}) : Completed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" - echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> ${MM_LOGFILE} fi else @@ -920,11 +925,11 @@ UPDATESERVICE(){ if [[ $BETA_SERVICES == *${usrvc}* ]]; then latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases | jq -r '.[0].assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` - echo "$(date) - Updating Service (${usrvc}) : BETA UPDATEURL = ${latest_release_url}" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : BETA UPDATEURL = ${latest_release_url}" >> ${MM_LOGFILE} beta="BETA " else latest_release_url=`curl -s https://api.github.com/repos/${MM_UPDATE_REPO[${usrvc}]}/${usrvc}/releases/latest | jq -r '.assets[].browser_download_url' | grep "${MM_UPDATE_REPO_GREP[${usrvc}]}" | cut -d '"' -f 4 | grep -i 'https://'` - echo "$(date) - Updating Service (${usrvc}) : UPDATEURL = ${latest_release_url}" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : UPDATEURL = ${latest_release_url}" >> ${MM_LOGFILE} beta="" fi latest_ver=`echo ${latest_release_url} | cut -d/ -f8` @@ -976,7 +981,7 @@ UPDATESERVICE(){ echo "${usrvc}_VER=\"${latest_ver}\"" >> ${TMPFOLDER}/vers.info if [ "${INSTALLSRVC}" != "true" ]; then - echo "$(date) - Updating Service (${usrvc}) : Completed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} # echo -e "${idsCL[Green]}${usrvc^} Service has been updated${idsCL[Default]}" else sudo -iu user mkdir -p /home/user/.config/${usrvc^} >> ${LOGFOLDER}/update.${usrvc} @@ -986,7 +991,7 @@ UPDATESERVICE(){ CREATE_SYSTEMD_SERVICE ${usrvc} >> ${LOGFOLDER}/update.${usrvc} systemctl daemon-reload >> ${LOGFOLDER}/update.${usrvc} fi - echo "$(date) - Installing Service (${usrvc}) : Completed" >> $logfile + echo "$(date) - Installing Service (${usrvc}) : Completed" >> ${MM_LOGFILE} echo -e "${idsCL[Green]}${usrvc^} Service has been Installed${idsCL[Default]}" fi @@ -994,7 +999,7 @@ UPDATESERVICE(){ else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" - echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Update Not Needed" >> ${MM_LOGFILE} fi @@ -1019,13 +1024,13 @@ UPDATESERVICE(){ fi echo -e "${idsCL[Green]}Done${idsCL[Default]}" - echo "$(date) - Updating Service (${usrvc}) : Completed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Completed" >> ${MM_LOGFILE} SERVICEBACKUPSTART ${usrvc} else echo -e "${idsCL[LightCyan]}Update Not Available${idsCL[Default]}" - echo "$(date) - Updating Service (${usrvc}) : Update not needed" >> $logfile + echo "$(date) - Updating Service (${usrvc}) : Update not needed" >> ${MM_LOGFILE} fi @@ -1625,12 +1630,12 @@ if [ ! -f ${TMPFOLDER}/mm.update.stop ]; then if [ -f ${TMPFOLDER}/mm.stop ] && [ "${action}" == "check" ]; then if [ "${2}" != "q" ]; then echo -e "${stopmsg}" - # [ $VERBOSE = true ] && echo "$(date) - Not checking, system has been stopped" >> $logfile + # [ $VERBOSE = true ] && echo "$(date) - Not checking, system has been stopped" >> ${MM_LOGFILE} echo fi exit 1 elif [ "${VPN_USER}" == "" ] || [ "${VPN_PASS}" == "" ] || [ "${VPN_SERVER}" == "" ]; then - echo "$(date) - VPN User Info Missing" >> $logfile + echo "$(date) - VPN User Info Missing" >> ${MM_LOGFILE} echo -e "${idsCL[Red]}VPN User Info Missing${idsCL[Default]}" echo exit 1 @@ -1638,7 +1643,7 @@ if [ ! -f ${TMPFOLDER}/mm.update.stop ]; then checkstopped=`expr $(date +%s) - $(stat -c %Y ${TMPFOLDER}/mm.stop.checking)` if [ $checkstopped -gt 900 ]; then rm -f ${TMPFOLDER}/mm.stop* - echo "$(date) - Bash script stuck in stop mode after doing a check, will restart" >> $logfile + echo "$(date) - Bash script stuck in stop mode after doing a check, will restart" >> ${MM_LOGFILE} SENDNOTICE "MM Script Error" "Bash script stuck in check mode, restarting system" MMSTOP rm -f ${TMPFOLDER}/mm.stop* diff --git a/run.sh b/run.sh index 800ca66..ff2a610 100755 --- a/run.sh +++ b/run.sh @@ -13,7 +13,7 @@ if [ "${2}" == "q" ]; then idsCL=('') idsBG=('') idsST=('') - [ "${1}" == "update" ] && echo -e "\n\n$(date) - Starting log #############################\n" >> ${outputlogfile} + [ "${1}" == "update" ] && echo -e "\n\n$(date) - Starting log #############################\n" >> ${MM_OUTPUT_LOGFILE} fi if ([ "${1}" == "check" ] && [ "$(date +%M)" == "15" ]) || [ "${1}" == "" ]; then