#!/usr/bin/env bash SINGLE_SERVER_SERVICE_CHECK(){ servicestocheck=${1^^}_SINGLESRVR_SERVICES[@] dockerstocheck=${1^^}_SINGLESRVR_DOCKERS[@] hosts=${1^^}_HOSTS[@] if [ "${!servicestocheck}" != "" ] || [ "${!dockerstocheck}" != "" ]; then local_ips=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | awk '/inet/ {print $2}' | cut -d'/' -f1); local_ip=$(/sbin/ip -o -4 addr list eth0 |grep 255 | awk '{print $4}' | cut -d/ -f1) if [[ ${local_ips} == *${NM_SINGLESRVR_IP[${1^^}]}* ]]; then if [ ! -f ${NM_TMPFOLDER}/.singleserver ]; then SENDNOTICE "Node-Balancing Notice" "Starting SingleServer Services on ${NM_NODETYPES[${1^^}]}-Node${local_ip: -1} (${local_ip}), stopping on other Nodes" touch ${NM_TMPFOLDER}/.singleserver fi gofor=false if [ "${1^^}" == "WEB" ] && [ ! -f /mnt/web-data/mounted ]; then /bin/mount -a sleep 5s [ -f ${NM_REPL_CHECK_LOC["${1}-data"]}/mounted ] && gofor=true else gofor=true fi if [ "${gofor}" == "true" ]; then for stc in "${!servicestocheck}"; do if [ "$(systemctl is-active ${stc}.service)" != "active" ]; then echo "$(date) - ${NM_NODETYPES[${1^^}]}-Node${local_ip: -1} - Starting ${stc} on ${local_ip}, stopping on the other Nodes ..." >> ${NM_LOGFILE} [ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.service pdnsadmin.socket" for nip in "${!hosts}"; do if [[ "${local_ip}" != *"${nip}"* ]] && [ "$(CHECK_HOST ${nip})" != "false" ]; then ssh -tq root@${nip} /bin/systemctl stop ${stc} ssh -tq root@${nip} rm -f ${NM_TMPFOLDER}/.singleserver fi done sleep 3s [ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.socket pdnsadmin.service" [ "${stc}" = "headscale" ] && sleep 90s /bin/systemctl start ${stc} & echo "$(date) - ${NM_NODETYPES[${1^^}]}-Node${local_ip: -1} - ... Done" >> ${NM_LOGFILE} fi done for docker in "${!dockerstocheck}"; do for nip in "${!hosts}"; do if [[ "${local_ip}" = *"${nip}"* ]]; then if [ "docker ps -a | grep ${docker})" ]; then [ "${docker}" == "authelia" ] && [ ! "$(/usr/bin/docker ps -q -f name=auth_redis)" ] && /usr/bin/docker start auth_redis && sleep 2s >/dev/null 2>&1 [ ! "$(/usr/bin/docker ps -q -f name=${docker})" ] && /usr/bin/docker start ${docker} & >/dev/null 2>&1 elif [ "${NM_DOCKER_COMPOSE_LOC[${docker}]}" != "" ]; then /usr/local/bin/docker compose -f ${NM_DOCKER_COMPOSE_LOC[${docker}]}/docker compose.yml up -d >/dev/null 2>&1 echo "$(date) - ${nip} - ${NM_HOSTNAMES[${nip}]}[${nip}] - ${NM_DOCKER_DESC[${docker}]} (docker) is not found, creating and starting now" >> ${NM_LOGFILE} fi else if [ "$(CHECK_HOST ${nip})" != "false" ]; then if [ "$(ssh root@${nip} docker ps -a | grep ${docker})" ]; then [ "$(ssh root@${nip} /usr/bin/docker ps -q -f name=${docker})" ] && ssh root@${nip} /usr/bin/docker stop ${docker} >/dev/null 2>&1 [ "${docker}" == "authelia" ] && [ "$(ssh root@${nip} /usr/bin/docker ps -q -f name=auth_redis)" ] && ssh root@${nip} /usr/bin/docker stop auth_redis >/dev/null 2>&1 elif [ "${NM_DOCKER_COMPOSE_LOC[${docker}]}" != "" ]; then ssh root@${nip} /usr/local/bin/docker compose -f ${NM_DOCKER_COMPOSE_LOC[${docker}]}/docker compose.yml up -d >/dev/null 2>&1 echo "$(date) - ${nip} - ${NM_HOSTNAMES[${nip}]}[${nip}] - ${NM_DOCKER_DESC[${docker}]} (docker) is not found, creating and starting now" >> ${NM_LOGFILE} fi fi fi done done fi else for stc in "${!servicestocheck}"; do [ "${stc}" = "pdnsadmin" ] && stc="pdnsadmin.socket pdnsadmin.service" /bin/systemctl stop ${stc} & >/dev/null 2>&1 done for docker in "${!dockerstocheck}"; do [ "$(/usr/bin/docker ps -q -f name=${docker})" ] && /usr/bin/docker stop ${docker} & >/dev/null 2>&1 done rm -f ${NM_TMPFOLDER}/.singleserver fi if [ "$(systemctl is-active nginx)" != "active" ]; then ${NM_SCRIPT} service web restart ${local_ip} fi fi } SERVICE(){ #var=NC_HOSTS[@] if [ "$1" = "glusterd" ]; then for nip in "${GL_HOSTS[@]}"; do if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then nip='localhost ' NCMD='' else NCMD="ssh root@${nip}" fi if [ "${3}" != "q" ]; then echo -en "${NM_SRVCOPTS[${2}]}ing on '$nip'... ${idsCL[Default]}" fi checkhost=$(CHECK_HOST ${nip}) if [ "${checkhost}" != "false" ]; then #$NCMD systemctl $2 $1 > /dev/null 2>&1 if [ "${3}" != "q" ]; then if [[ "enable,disable" = *"${2}"* ]] || [ "${1}" = "daemon-reload" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ "$(${NCMD} systemctl is-active ${1})" = "active" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ "$2" = "stop" ]; then echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}" else echo -e "${idsCL[Red]}ERROR${idsCL[Default]}" fi fi elif [ "${3}" != "q" ]; then echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}" fi done if [ "${2}" = "start" ] || [ "${2}" = "restart" ]; then echo sleep 5s for nip in "${GL_HOSTS[@]}"; do if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then nip='localhost ' NCMD='' else NCMD="ssh root@${nip}" fi if [ "${3}" != "q" ]; then echo -en "Mounting drive on '$nip'... ${idsCL[Default]}" fi checkhost=$(CHECK_HOST ${nip}) if [ "${checkhost}" != "false" ]; then #$NCMD mount -a > /dev/null 2>&1 if [ "${3}" != "q" ]; then # if [[ "${NCHOSTS}" == *"${nip}"* ]]; then if [[ " ${NC_HOSTS[*]} " =~ " ${nip} " ]]; then mntpath="/mnt/Nextcloud-Data" # elif [[ "${WEBHOSTS}" == *"${nip}"* ]]; then elif [[ " ${NM_HOSTS['WEB'][*]} " =~ " ${nip} " ]]; then mntpath="/mnt/web-data" fi ${NCMD} "test -e ${mntpath}/mounted" if [ $? -eq 0 ]; then echo -e "${idsCL[Green]}MOUNTED ($mntpath)${idsCL[Default]}" else echo -e "${idsCL[Red]}NOT MOUNTED ($mntpath)${idsCL[Default]}" fi fi if [ "$nip" = "10.10.1.43" ]; then echo -en "Mounting drive on '$nip'... ${idsCL[Default]}" mntpath="/mnt/Nextcloud-Data" ${NCMD} "test -e ${mntpath}/mounted" if [ $? -eq 0 ]; then echo -e "${idsCL[Green]}MOUNTED ($mntpath)${idsCL[Default]}" else echo -e "${idsCL[Red]}NOT MOUNTED ($mntpath)${idsCL[Default]}" fi fi elif [ "${3}" != "q" ]; then echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}" fi done fi else if [ "${4}" != "all" ]; then if [ "${RUN_NODE_TYPE}" != "" ]; then NT_HOSTS=${RUN_NODE_TYPE}_HOSTS[@] NT_HOST=${RUN_NODE_TYPE}_HOSTS[0] NTS=("${RUN_NODE_TYPE}"); else NTS=(${NM_NODE_TYPES[*]}) fi else NTS=(${NM_NODE_TYPES[*]}) fi # if [ "${RUN_NODE_TYPE}" == "" ]; then # # fi if [ "${3}" != "q" ]; then if [ "${NM_SERVICE_DESC[${1}]}" = "" ]; then echo -e "${idsCL[Red]}(${1}) is not an allowed service.${idsCL[Default]}" exit 1 fi if [ "${NM_SRVCOPTS[${2}]}" = "" ]; then echo -e "${idsCL[Red]}(${2}) is not an allowed service action.${idsCL[Default]}" exit 1 fi if [ "$2" = "start" ] && [ "$1" = "haproxy" ]; then TACT='restart' else TACT="${2}" fi echo echo -e "${idsCL[LightGreen]}[[ ${NM_SERVICE_DESC[${1}]} ${NM_SRVCOPTS[${2}]}ing ]]${idsCL[Default]}" echo -e "${idsCL[LightGreen]}-------------------------------------------${idsCL[Default]}" fi if [ "${1}" = "nginx" ] && ([ "${3}" == "" ] || [ "${3}" == "all" ]); then if [ "${RUN_NODE_TYPE}" != "" ] && [ "${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}" != "" ] && ([ "${2}" = "restart" ] || [ "${2}" = "reload" ]); then if [ "${3}" != "ns" ]; then if [ "${3}" != "q" ]; then echo -en "${idsCL[LightYellow]}Verifying replication across the nodes... ${idsCL[Default]}" fi if [[ "${RUN_NODE_IP}" == *"${!NT_HOST}"* ]]; then PH_CMD="" else PH_CMD="ssh root@${!NT_HOST}" fi ${PH_CMD} touch ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl # ${PH_CMD} "echo -e \"Service ${1} ${2}\" >> ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl" for nip in "${!NT_HOSTS}"; do checkhost=$(CHECK_HOST ${nip}) if [ "${checkhost}" != "false" ] && [ "${nip}" != "${!NT_HOST}" ]; then checked=false until [ "${checked}" = "true" ]; do checked="`${PH_CMD} ssh root@${nip} [ -f ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl ] && [ $(expr $(date +%s) - $(stat -L --format %Y ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl)) -le 90 ] && echo true`" done fi done ${PH_CMD} rm -f ${NM_REPL_NGINX_PATHS[${RUN_NODE_TYPE}]}/test.repl fi fi elif ([ "${1}" = "pdns" ] || [ "${1}" = "pdnsadmin" ] || [ "${1}" = "headscale" ]) && ([ "${3}" == "" ] || [ "${3}" == "all" ]); then if [ "${2}" = "start" ] || [ "${2}" = "restart" ] || [ "${2}" = "reload" ]; then if [ "${3}" != "ns" ]; then if [ "${3}" != "q" ]; then echo -en "${idsCL[LightYellow]}Verifying replication across the nodes... ${idsCL[Default]}" fi if [[ "${RUN_NODE_IP}" == *"${!NT_HOST}"* ]]; then PH_CMD="" else PH_CMD="ssh root@${!NT_HOST}" fi ${PH_CMD} touch ${NM_REPL_CHECK_LOC[${1}]}/test.repl for nip in "${!NT_HOSTS}"; do checkhost=$(CHECK_HOST ${nip}) if [ "${checkhost}" != "false" ] && [ "${nip}" != "${!NT_HOST}" ]; then checked=false until [ "${checked}" = "true" ]; do checked="`${PH_CMD} ssh root@${nip} [ -f ${NM_REPL_CHECK_LOC[${1}]}/test.repl ] && [ $(expr $(date +%s) - $(stat -L --format %Y ${NM_REPL_CHECK_LOC[${1}]}/test.repl)) -le 90 ] && echo true`" done fi done ${PH_CMD} rm -f ${NM_REPL_CHECK_LOC[${1}]}/test.repl fi fi fi if [ "${3}" != "q" ]; then echo -e "${idsCL[Green]}Completed${idsCL[Default]}" echo fi for NTYPE in "${NTS[@]}"; do var1=${NTYPE}_SERVICES_CHECK[@] # if [[ "${!var1}" = *"${1}"* ]]; then if [[ " ${!var1} " =~ " ${1} " ]]; then nid=1 if [ "${3}" != "" ]; then var2=(${3}) sethost=true; else var2=${NTYPE}_HOSTS[@] IFS=' ' var2=(${!var2}) unset IFS sethost=false fi for nip in "${var2[@]}"; do # if [[ "${RUN_NODE_IP}" == *"${3}"* ]]; then GO=true; # elif [ ! -z ${3+x} ] || [ "${3}" == "q" ]; then GO=true; # else GO=false; # fi # if [ "${GO}" == "true" ]; then if [ $sethost == true ]; then nodename=${NM_HOSTNAMES[${3}]} else nodename="${NM_HOSTNAMES[${nip}]}[${nip}]" fi if [[ "${RUN_NODE_IP}" == *"${nip}"* ]]; then nip='localhost ' NCMD='' else NCMD="ssh root@${nip}" fi NOGOCHK=true; # if [ "${1}" == "gitea" ] || [ "${1}" == "pdnsadmin" ] || [ "${1}" == "pdnsadmin.socket" ]; then if [ "${1}" == "gitea" ]; then if [[ $($NCMD ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1) != *${NM_SINGLESRVR_IP['WEB']}* ]]; then NOGOCHK=false; fi elif [ "${1}" == "headscale" ]; then if [[ $($NCMD ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1) != *${NM_SINGLESRVR_IP['HS']}* ]]; then NOGOCHK=false; fi fi if [ "${NOGOCHK}" == "true" ]; then if [ "${3}" != "q" ]; then echo -en "${NM_SRVCOPTS[${2}]}ing on ${nodename} ($nip)... ${idsCL[Default]}" fi checkhost=$(CHECK_HOST ${nip}) if [ "${checkhost}" != "false" ]; then if [ "${1}"= = "offsite-power-check" ]; then if [ "${3}" != "q" ] && [ "${2}" != "status" ]; then if [ "$(ssh root@${nip} ps -U root | grep "offsite-power-check.sh start" | grep -v "grep" | awk '{print $1}')" != "" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" else echo -e "${idsCL[Red]}Not Running${idsCL[Default]}" fi fi else [ "${1}" = "pdnsadmin" ] && srva="pdnsadmin.socket pdnsadmin.service" || srva=${1} if [ "${2}" != "status" ]; then $NCMD systemctl ${2} ${srva} >/dev/null 2>&1 else echo $NCMD systemctl ${2} ${srva} echo fi if [ "${3}" != "q" ] && [ "${2}" != "status" ]; then if [[ "enable,disable" = *"${2}"* ]] || [ "${1}" = "daemon-reload" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ "$(${NCMD} systemctl is-active ${1})" = "active" ]; then echo -e "${idsCL[Green]}Done${idsCL[Default]}" elif [ "$2" = "stop" ]; then echo -e "${idsCL[Red]}STOPPED${idsCL[Default]}" else echo -e "${idsCL[Red]}ERROR${idsCL[Default]}" fi fi fi elif [ "${3}" != "q" ]; then echo -e "${idsCL[Red]}Node is Down${idsCL[Default]}" fi fi # fi nid=`expr $nid + 1` # if [ "${1}" = "gitea" ]; then # break # fi done fi done fi if [ "${3}" != "q" ]; then echo echo -e "${idsCL[Green]}${NM_SERVICE_DESC[${1}]} has been ${NM_SRVCOPTS[${2}]}ed${idsCL[Default]}" echo fi } CHECK_DOCKER_SERVICES(){ for NTYPE in "${NM_NODE_TYPES[@]}"; do dockers=${NTYPE}_DOCKERS_CHECK[@] hosts=${NTYPE}_HOSTS[@] if [[ -v ${NTYPE}_DOCKERS_CHECK ]]; then if [ "${ST_ACTION}" != "check" ]; then echo -e "${idsST[Bold]}"; DIVIDER echo -e "${idsCL[Yellow]} ${NM_NODETYPES[$NTYPE]}-Node Docker Service(s) Status${idsCL[Default]}" DIVIDER; echo -e "${idsST[Reset]}" fi nid=1 for nip in "${!hosts}"; do if [[ ${RUN_NODE_IP} == *"${nip}"* ]]; then NCMD=''; LH='- localhost' else NCMD="ssh root@${nip}"; LH='' fi if [ "${NCMD}" != "" ]; then checkhost=$(CHECK_HOST ${nip}) fi if [ "${checkhost}" != "false" ]; then if [ "${ST_ACTION}" != "check" ]; then echo -en " ${idsST[Bold]}${idsCL[LightCyan]}${NM_HOSTNAMES[${nip}]}[${nip}]${idsST[Reset]}" uptime=`${NCMD} uptime -p` echo -e "${idsCL[LightCyan]} - ${uptime} ${idsCL[LightYello]}${LH}${idsCL[Default]}" DIVIDER false green fi if [ -f ${NM_TMPFOLDER}/${nip}.down ]; then if [ -f ${NM_TMPFOLDER}/${nip}.errtime ]; then toterrtime=`date +%s`-$(stat -c %Y ${NM_TMPFOLDER}/${nip}.errtime) else toterrtime=`date +%s`-$(stat -c %Y ${NM_TMPFOLDER}/${nip}.down) fi rm -f ${NM_TMPFOLDER}/${nip}.down rm -f ${NM_TMPFOLDER}/${nip}.errtime SENDNOTICE "${NM_HOSTNAMES[${nip}]}[${nip}]-UP" "${NM_HOSTNAMES[${nip}]}[${nip}] is back UP! It was down for $(SHOW_TIME ${toterrtime})" fi for docker in "${!dockers}"; do if [ "${ST_ACTION}" != "check" ]; then c=0; cw=30; spc='' spc1=`expr ${cw} - ${#NM_DOCKER_DESC[${docker}]}` until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en " ${NM_DOCKER_DESC[${docker}]}$spc: " fi if [ ! "$(${NCMD} docker ps -q -f name=${docker})" ]; then if [ "$(${NCMD} docker ps -aq -f status=exited -f name=${docker})" ]; then ${NCMD} docker rm ${docker} fi ${NCMD} /usr/local/bin/docker compose -f ${NM_DOCKER_COMPOSE_LOC[${docker}]}/docker compose.yml up -d sleep 10s if [ "$(${NCMD} docker ps -q -f name=${docker})" ]; then if [ "${ST_ACTION}" != "check" ]; then echo -e "${idsCL[Green]}Running - Fixed${idsCL[Default]}" fi else if [ "${ST_ACTION}" != "check" ]; then echo -e "${idsCL[Red]}Not Running - Could Not Fix!${idsCL[Default]}" fi fi else if [ "${ST_ACTION}" != "check" ]; then echo -e "${idsCL[Green]}Running${idsCL[Default]}" fi fi done else if [ ! -f ${NM_TMPFOLDER}/${nip}.down ]; then touch ${NM_TMPFOLDER}/${nip}.down if [ ! -f ${NM_TMPFOLDER}/${nip}.errtime ]; then touch ${NM_TMPFOLDER}/${nip}.errtime fi else errtime=`date +%s`-$(stat -c %Y ${NM_TMPFOLDER}/${nip}.down) fi if [ "${ST_ACTION}" != "check" ]; then toterrtime=`date +%s`-$(stat -c %Y ${NM_TMPFOLDER}/${nip}.errtime) echo -e " ${idsST[Bold]}${idsCL[LightCyan]}${NM_HOSTNAMES[${nip}]}[${nip}]${idsST[Reset]}${idsCL[Red]} - Node has been down for $(SHOW_TIME ${toterrtime}) ${idsCL[LightYello]}${LH}${idsCL[Default]}" fi fi if [ "${ST_ACTION}" != "check" ]; then echo; fi nid=`expr $nid + 1` done fi done }