add ssh connection check (#31472)
This commit is contained in:
parent
f833dac288
commit
ed4cc52250
|
@ -22,13 +22,32 @@ check_service() {
|
||||||
|
|
||||||
# Loop through the servers
|
# Loop through the servers
|
||||||
for server in "${servers[@]}"; do
|
for server in "${servers[@]}"; do
|
||||||
# Check if the service is running
|
ssh_success=false
|
||||||
if ssh -o StrictHostKeyChecking=no sol@"$server" sudo systemctl is-active "$service" >/dev/null; then
|
ssh_attempts=0
|
||||||
|
while ! $ssh_success && [ $ssh_attempts -lt 3 ]; do
|
||||||
|
# Check if the service is running
|
||||||
|
if ssh -o StrictHostKeyChecking=no sol@"$server" sudo systemctl is-active "$service" >/dev/null 2>&1; then
|
||||||
|
ssh_success=true
|
||||||
|
else
|
||||||
|
ssh_attempts=$((ssh_attempts + 1))
|
||||||
|
sleep 5
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if $ssh_success; then
|
||||||
# Service is running
|
# Service is running
|
||||||
message="The $service service is running on $server."
|
message="The $service service is running on $server."
|
||||||
echo "$message"
|
echo "$message"
|
||||||
else
|
else
|
||||||
# Service is not running, try to restart it
|
# SSH connection failed after retries
|
||||||
|
message="ERROR: Unable to establish SSH connection to $server after 3 retries."
|
||||||
|
echo "$message"
|
||||||
|
curl -H "Content-Type: application/json" -d '{"content":"'"$message"', manual intervention is required."}' "$DISCORD_WEBHOOK"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Service is not running, try to restart it
|
||||||
|
if ! $ssh_success; then
|
||||||
message="The $service service is not running on $server. Restarting..."
|
message="The $service service is not running on $server. Restarting..."
|
||||||
echo "$message"
|
echo "$message"
|
||||||
curl -H "Content-Type: application/json" -d '{"content":"'"$message"'"}' "$DISCORD_WEBHOOK"
|
curl -H "Content-Type: application/json" -d '{"content":"'"$message"'"}' "$DISCORD_WEBHOOK"
|
||||||
|
|
Loading…
Reference in New Issue