| 1 |
#!/usr/bin/env bash |
| 2 |
# ========================================================================== |
| 3 |
# _ _ _ ____ ____ _____ |
| 4 |
# | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 |
# | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 |
# | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 |
# |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 |
# |
| 9 |
# --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 |
# https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 |
# ========================================================================== |
| 12 |
# |
| 13 |
# High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 |
# Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 |
# |
| 16 |
# This program is free software: you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation, either version 3 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
# |
| 29 |
# Contact: [email protected] |
| 30 |
|
| 31 |
# Bash options: |
| 32 |
set -eu |
| 33 |
|
| 34 |
|
| 35 |
# ###### Usage ############################################################## |
| 36 |
usage () { |
| 37 |
echo >&2 "Usage: $0 [-h|--help] [--verbose|-v] [--quiet|-q] [--local|-L local_directory] [--remote|-R remote_location] [--collector|-C collector] [--key|-K ssh_private_key_file]" |
| 38 |
exit 1 |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
# ###### Main program ####################################################### |
| 43 |
|
| 44 |
# ====== Handle arguments =================================================== |
| 45 |
GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)" |
| 46 |
options="$(${GETOPT} -o hN:L:R:C:K:H:vq --long help,nodeid:,local:,remote:,collector:,key:,known-hosts:,verbose,quiet -a -- "$@")" |
| 47 |
# shellcheck disable=SC2181 |
| 48 |
if [[ $? -ne 0 ]]; then |
| 49 |
usage |
| 50 |
fi |
| 51 |
|
| 52 |
NODE_ID= |
| 53 |
LOCAL="/var/hipercontracer/data" |
| 54 |
REMOTE="/var/hipercontracer/data" |
| 55 |
COLLECTOR= |
| 56 |
KEY="/var/hipercontracer/ssh/id_ed25519" |
| 57 |
KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts" |
| 58 |
VERBOSE=1 |
| 59 |
|
| 60 |
eval set -- "${options}" |
| 61 |
while [ $# -gt 0 ] ; do |
| 62 |
case "$1" in |
| 63 |
-N | --nodeid) |
| 64 |
NODE_ID="$2" |
| 65 |
shift 2 |
| 66 |
;; |
| 67 |
-L | --local) |
| 68 |
LOCAL="$2" |
| 69 |
shift 2 |
| 70 |
;; |
| 71 |
-R | --remote) |
| 72 |
REMOTE="$2" |
| 73 |
shift 2 |
| 74 |
;; |
| 75 |
-C | --collector) |
| 76 |
COLLECTOR="$2" |
| 77 |
shift 2 |
| 78 |
;; |
| 79 |
-K | --key) |
| 80 |
KEY="$2" |
| 81 |
shift 2 |
| 82 |
;; |
| 83 |
-H | --known-hosts) |
| 84 |
KNOWN_HOSTS="$2" |
| 85 |
shift 2 |
| 86 |
;; |
| 87 |
-h | --help) |
| 88 |
usage |
| 89 |
;; |
| 90 |
-v | --verbose) |
| 91 |
VERBOSE=1 |
| 92 |
shift |
| 93 |
;; |
| 94 |
-q | --quiet) |
| 95 |
VERBOSE=0 |
| 96 |
shift |
| 97 |
;; |
| 98 |
--) |
| 99 |
shift |
| 100 |
break |
| 101 |
;; |
| 102 |
esac |
| 103 |
done |
| 104 |
|
| 105 |
if [ "${LOCAL}" == "" ] || [ "${REMOTE}" == "" ] || [ "${COLLECTOR}" == "" ] ; then |
| 106 |
usage |
| 107 |
fi |
| 108 |
if [ ! -d "${LOCAL}" ] ; then |
| 109 |
echo >&2 "ERROR: Local directory ${LOCAL} does not exist!" |
| 110 |
exit 1 |
| 111 |
fi |
| 112 |
if [ ! -e "${KEY}" ] ; then |
| 113 |
echo >&2 "ERROR: SSH private key file ${KEY} does not exist!" |
| 114 |
exit 1 |
| 115 |
fi |
| 116 |
|
| 117 |
if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then |
| 118 |
echo >&2 "ERROR: No or invalid Node ID specified!" |
| 119 |
exit 1 |
| 120 |
fi |
| 121 |
if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 999999999 ] ; then |
| 122 |
echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" |
| 123 |
exit 1 |
| 124 |
fi |
| 125 |
|
| 126 |
|
| 127 |
# ====== Run RSync ========================================================== |
| 128 |
rsyncOptions="-rt" # --remove-source-files |
| 129 |
if [ ${VERBOSE} -ne 0 ] ; then |
| 130 |
rsyncOptions="${rsyncOptions} --info=progress2" |
| 131 |
else |
| 132 |
rsyncOptions="${rsyncOptions} -q" |
| 133 |
fi |
| 134 |
rsyncOptions="${rsyncOptions} --exclude '*.tmp'" |
| 135 |
rsyncOptions="${rsyncOptions} -e 'ssh -i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}'" |
| 136 |
|
| 137 |
user="node${NODE_ID}" |
| 138 |
command="rsync ${rsyncOptions} ${LOCAL}/ ${user}@${COLLECTOR}:${REMOTE}/${user}/" |
| 139 |
if [ ${VERBOSE} -ne 0 ] ; then |
| 140 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running RSync: ${command}\x1b[0m" |
| 141 |
fi |
| 142 |
sh -c "${command}" && status=0 || status=$? |
| 143 |
if [ "${status}" -ne 0 ] ; then |
| 144 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") RSync failed: ${command}\x1b[0m" |
| 145 |
exit 1 |
| 146 |
else |
| 147 |
ssh -tt -i "${KEY}" -oUserKnownHostsFile="${KNOWN_HOSTS}" "${user}"@"${COLLECTOR}" "date -u +\"%Y-%m-%d %H:%M:%S\" >${REMOTE}/${user}/last-sync.txt" || true |
| 148 |
if [ ${VERBOSE} -ne 0 ] ; then |
| 149 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" |
| 150 |
fi |
| 151 |
fi |