Debian Codegraph @

src/hpct-nodes-list

raw
1 #!/usr/bin/env bash
2 # ____ ____ ____ _ _ _
3 # / ___| ___ | _ \ / ___|___ | | | ___ ___| |_ ___ _ __
4 # \___ \ / _ \| |_) | | | / _ \| | |/ _ \/ __| __/ _ \| '__|
5 # ___) | (_) | __/ | |__| (_) | | | __/ (__| || (_) | |
6 # |____/ \___/|_| \____\___/|_|_|\___|\___|\__\___/|_|
7 #
8 # HiPerConTracer Collector
9 # Copyright (C) 2022-2026 by Thomas Dreibholz
10 #
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #
24 # Contact: [email protected]
25
26 # Bash options:
27 set -eu
28
29 HIPERCONTRACER_DIRECTORY="/var/hipercontracer"
30 UNAME="$(uname)"
31
32
33 # ###### Compute time difference between time stamps ########################
34 calculate_time_difference ()
35 {
36 local t1
37 local t2
38 if [ "${UNAME}" != "FreeBSD" ] ; then
39 t1="$(date --utc --date "$1" +"%s" 2>/dev/null || true)"
40 t2="$(date --utc --date "$2" +"%s" 2>/dev/null || true)"
41 else
42 t1="$(date -j -f "%Y-%m-%d %H:%M:%S" "$1" +"%s" 2>/dev/null || true)"
43 t2="$(date -j -f "%Y-%m-%d %H:%M:%S" "$2" +"%s" 2>/dev/null || true)"
44 fi
45 if [ "$1" == "" ] || [ "$2" == "" ] || [ "${t1}" == "" ] || [ "${t2}" == "" ] ; then
46 echo "Invalid time!"
47 return
48 fi
49 difference=$((t1-t2)) || true
50
51 days=$((difference/86400)) || true
52 difference=$((difference-days*86400)) || true
53 if [ ${difference} -lt 0 ] ; then
54 difference=$((-difference)) || true
55 fi
56 hours=$((difference/3600)) || true
57 difference=$((difference-hours*3600)) || true
58 minutes=$((difference/60)) || true
59 difference=$((difference-minutes*60)) || true
60 seconds=${difference}
61
62 printf "%03dd %02d:%02d:%02d" "${days}" "${hours}" "${minutes}" "${seconds}"
63 }
64
65
66 # ###### Main program #######################################################
67
68 # Get configured nodes and RTunnel ports ====================================
69 # shellcheck disable=SC2207
70 CONFIGURED_NODES="( $(find /home -follow -mindepth 1 -maxdepth 1 -type d -name "node*" -print0 | xargs -0 -r -n1 basename | sed -e "s/^node//" | sort -n) )"
71 # Get open ports bound to localhost (127.0.0.1 or ::1).
72 # shellcheck disable=SC2207
73 if [ "${UNAME}" != "FreeBSD" ] ; then
74 LISTENING_TCP_PORTS="( $(ss -tlwn | awk '/^tcp/ { if(($2 == "LISTEN") && ($5 ~ /^127.0.0.1|^\[::1\]/ )) { print $5 }}' | sed -e "s/.*://" | sort -un) )"
75 else
76 LISTENING_TCP_PORTS="( $(sockstat -46ln -P tcp | awk '/^[0-9]+/ { if($6 ~ /^127.0.0.1|^\[::1\]/) { print $6 } }' | sed -e 's/^.*://' | sort -u) )"
77 fi
78
79 # for port in ${LISTENING_TCP_PORTS[@]} ; do
80 # echo "Port: ${port}"
81 # done
82 # echo "${LISTENING_TCP_PORTS[*]}"
83
84
85 # ====== Show nodes and status ==============================================
86 echo -e "\x1b[34mNode Port Tunnel Last Seen Last Sync Last Boot System Version\x1b[0m"
87 now=$(date -u +"%Y-%m-%d %H:%M:%S")
88 for nodeID in "${CONFIGURED_NODES[@]}" ; do
89 nodeID=$((nodeID)) # Convert string to number. Necessary!
90 nodePort=$((10000+nodeID))
91
92 # ====== RTunnel online? =================================================
93 if [[ ${LISTENING_TCP_PORTS[*]} =~ (^| )"${nodePort}"($| ) ]] ; then
94 nodeConnected="online "
95 echo -en "\x1b[32m"
96
97 nodeLastSeen="${now}"
98 else
99 nodeConnected="OFFLINE"
100 echo -en "\x1b[31m"
101
102 nodeLastSeen=""
103 fi
104
105 # ====== Last Seen ====================================================
106 # NOTE: last-seen.txt must be accessible by the user!
107 if [ -e "${HIPERCONTRACER_DIRECTORY}/data/node${nodeID}/last-seen.txt" ] ; then
108 nodeInfo="$(cat "${HIPERCONTRACER_DIRECTORY}/data/node${nodeID}/last-seen.txt")"
109 else
110 nodeInfo="Unknown! (no access to last-seen.txt?)"
111 fi
112 if [ "${nodeLastSeen}" == "" ] ; then
113 nodeLastSeen="$(echo "${nodeInfo}" | head -n1)"
114 fi
115 nodeSystem=$(echo "${nodeInfo}" | grep "^lsb_release: " | sed -e "s/[^:]*: //")
116 nodePackageVersion=$(echo "${nodeInfo}" | grep "^node_package_version: " | sed -e "s/[^:]*: //")
117 nodeUptime=$(echo "${nodeInfo}" | grep "^node_uptime: " | sed -e "s/[^:]*: //")
118 # tunnelUptime=$(echo "${nodeInfo}" | grep "^tunnel_uptime: " | sed -e "s/[^:]*: //")
119
120 # ====== Last Sync =======================================================
121 # NOTE: last-seen.txt must be accessible by the user!
122 if [ -e "${HIPERCONTRACER_DIRECTORY}/data/node${nodeID}/last-sync.txt" ] ; then
123 nodeLastSync="$(cat "${HIPERCONTRACER_DIRECTORY}/data/node${nodeID}/last-sync.txt")"
124 else
125 nodeLastSync="Unknown! (no access to last-sync.txt?)"
126 fi
127
128 printf "Node %-4d %5d %s %-13s %-13s %-13s %-30s %-8s\x1b[0m\n" \
129 "${nodeID}" "${nodePort}" "${nodeConnected}" \
130 "$(calculate_time_difference "${now}" "${nodeLastSeen}")" \
131 "$(calculate_time_difference "${now}" "${nodeLastSync}")" \
132 "$(calculate_time_difference "${now}" "${nodeUptime}")" \
133 "${nodeSystem}" \
134 "${nodePackageVersion}"
135
136 done