|
| 1 | +#!/bin/bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +help() { |
| 20 | + printf "Usage: $0 |
| 21 | + -i identifier (ignored for local-only heartbeat) |
| 22 | + -p path (ignored for local-only heartbeat) |
| 23 | + -m mount point (local path where heartbeat will be written) |
| 24 | + -h host (host IP/name to include in heartbeat filename) |
| 25 | + -r write/read hb log (read-check mode) |
| 26 | + -c cleanup (trigger emergency reboot) |
| 27 | + -t interval between read hb log\n" |
| 28 | + exit 1 |
| 29 | +} |
| 30 | + |
| 31 | +#set -x |
| 32 | +NfsSvrIP= |
| 33 | +NfsSvrPath= |
| 34 | +MountPoint= |
| 35 | +HostIP= |
| 36 | +interval= |
| 37 | +rflag=0 |
| 38 | +cflag=0 |
| 39 | + |
| 40 | +while getopts 'i:p:m:h:t:rc' OPTION |
| 41 | +do |
| 42 | + case $OPTION in |
| 43 | + i) |
| 44 | + NfsSvrIP="$OPTARG" |
| 45 | + ;; # retained for CLI compatibility but unused for local-only script |
| 46 | + p) |
| 47 | + NfsSvrPath="$OPTARG" |
| 48 | + ;; # retained for CLI compatibility but unused for local-only script |
| 49 | + m) |
| 50 | + MountPoint="$OPTARG" |
| 51 | + ;; |
| 52 | + h) |
| 53 | + HostIP="$OPTARG" |
| 54 | + ;; |
| 55 | + r) |
| 56 | + rflag=1 |
| 57 | + ;; |
| 58 | + t) |
| 59 | + interval="$OPTARG" |
| 60 | + ;; |
| 61 | + c) |
| 62 | + cflag=1 |
| 63 | + ;; |
| 64 | + *) |
| 65 | + help |
| 66 | + ;; |
| 67 | + esac |
| 68 | +done |
| 69 | + |
| 70 | +# Match original kvmheartbeat.sh: require NfsSvrIP parameter for CLI compatibility |
| 71 | +if [ -z "$NfsSvrIP" ] |
| 72 | +then |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +# For local-only heartbeat we require a mountpoint |
| 77 | +if [ -z "$MountPoint" ] |
| 78 | +then |
| 79 | + echo "Mount point (-m) is required" |
| 80 | + help |
| 81 | +fi |
| 82 | + |
| 83 | +# Ensure mount point exists and is writable |
| 84 | +if [ ! -d "$MountPoint" ]; then |
| 85 | + mkdir -p "$MountPoint" 2>/dev/null |
| 86 | + if [ $? -ne 0 ]; then |
| 87 | + echo "Failed to create mount point directory: $MountPoint" >&2 |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +fi |
| 91 | + |
| 92 | +# Determine a sensible HostIP if not provided |
| 93 | +if [ -z "$HostIP" ]; then |
| 94 | + # try to get a non-loopback IPv4 address, fallback to hostname |
| 95 | + ipaddr=$(hostname -I 2>/dev/null | awk '{print $1}') |
| 96 | + if [ -n "$ipaddr" ]; then |
| 97 | + HostIP="$ipaddr" |
| 98 | + else |
| 99 | + HostIP=$(hostname) |
| 100 | + fi |
| 101 | +fi |
| 102 | + |
| 103 | +#delete VMs on this mountpoint (best-effort) |
| 104 | +deleteVMs() { |
| 105 | + local mountPoint=$1 |
| 106 | + vmPids=$(ps aux | grep qemu | grep "$mountPoint" | awk '{print $2}' 2> /dev/null) |
| 107 | + if [ $? -gt 0 ] |
| 108 | + then |
| 109 | + return |
| 110 | + fi |
| 111 | + |
| 112 | + if [ -z "$vmPids" ] |
| 113 | + then |
| 114 | + return |
| 115 | + fi |
| 116 | + |
| 117 | + for pid in $vmPids |
| 118 | + do |
| 119 | + kill -9 $pid &> /dev/null |
| 120 | + done |
| 121 | +} |
| 122 | + |
| 123 | +#checking is there the mount point present under $MountPoint? |
| 124 | +mounts=$(cat /proc/mounts | grep "$MountPoint") |
| 125 | +if [ $? -gt 0 ] |
| 126 | +then |
| 127 | + # mount point not present — we don't remount in local-only script |
| 128 | + # nothing to do here; keep for compatibility with original flow |
| 129 | + : |
| 130 | +else |
| 131 | + # mount exists; if not in read-check mode, consider deleting VMs similar to original behavior |
| 132 | + if [ "$rflag" == "0" ] |
| 133 | + then |
| 134 | + deleteVMs $MountPoint |
| 135 | + fi |
| 136 | +fi |
| 137 | + |
| 138 | +hbFolder="$MountPoint/KVMHA/" |
| 139 | +hbFile="$hbFolder/hb-$HostIP" |
| 140 | + |
| 141 | +write_hbLog() { |
| 142 | +#write the heart beat log |
| 143 | + stat "$hbFile" &> /dev/null |
| 144 | + if [ $? -gt 0 ] |
| 145 | + then |
| 146 | + # create a new one |
| 147 | + mkdir -p "$hbFolder" &> /dev/null |
| 148 | + # touch will be done by atomic write below; ensure folder is writable |
| 149 | + if [ ! -w "$hbFolder" ]; then |
| 150 | + printf "Folder not writable: $hbFolder" >&2 |
| 151 | + return 2 |
| 152 | + fi |
| 153 | + fi |
| 154 | + |
| 155 | + timestamp=$(date +%s) |
| 156 | + # Write atomically to avoid partial writes (write to tmp then mv) |
| 157 | + tmpfile="${hbFile}.$$" |
| 158 | + printf "%s\n" "$timestamp" > "$tmpfile" 2>/dev/null |
| 159 | + if [ $? -ne 0 ]; then |
| 160 | + printf "Failed to write heartbeat to $tmpfile" >&2 |
| 161 | + return 2 |
| 162 | + fi |
| 163 | + mv -f "$tmpfile" "$hbFile" 2>/dev/null |
| 164 | + return $? |
| 165 | +} |
| 166 | + |
| 167 | +check_hbLog() { |
| 168 | + if [ ! -f "$hbFile" ]; then |
| 169 | + # signal large difference if file missing |
| 170 | + return 999999 |
| 171 | + fi |
| 172 | + now=$(date +%s) |
| 173 | + hb=$(cat "$hbFile" 2>/dev/null) |
| 174 | + if [ -z "$hb" ]; then |
| 175 | + return 999998 |
| 176 | + fi |
| 177 | + diff=`expr $now - $hb 2>/dev/null` |
| 178 | + if [ $? -ne 0 ] |
| 179 | + then |
| 180 | + return 999997 |
| 181 | + fi |
| 182 | + if [ -z "$interval" ]; then |
| 183 | + # if no interval provided, consider 0 as success |
| 184 | + if [ $diff -gt 0 ]; then |
| 185 | + return $diff |
| 186 | + else |
| 187 | + return 0 |
| 188 | + fi |
| 189 | + fi |
| 190 | + if [ $diff -gt $interval ] |
| 191 | + then |
| 192 | + return $diff |
| 193 | + fi |
| 194 | + return 0 |
| 195 | +} |
| 196 | + |
| 197 | +if [ "$rflag" == "1" ] |
| 198 | +then |
| 199 | + check_hbLog |
| 200 | + diff=$? |
| 201 | + if [ $diff == 0 ] |
| 202 | + then |
| 203 | + echo "=====> ALIVE <=====" |
| 204 | + else |
| 205 | + echo "=====> Considering host as DEAD because last write on [$hbFile] was [$diff] seconds ago, but the max interval is [$interval] <======" |
| 206 | + fi |
| 207 | + exit 0 |
| 208 | +elif [ "$cflag" == "1" ] |
| 209 | +then |
| 210 | + /usr/bin/logger -t heartbeat "kvmsmpheartbeat.sh will reboot system because it was unable to write the heartbeat to the storage." |
| 211 | + sync & |
| 212 | + sleep 5 |
| 213 | + echo b > /proc/sysrq-trigger |
| 214 | + exit $? |
| 215 | +else |
| 216 | + write_hbLog |
| 217 | + exit $? |
| 218 | +fi |
| 219 | + |
0 commit comments