Created
February 27, 2024 14:25
-
-
Save juffaz/d6a5d5ea3ca9eccaf127e7105b418e28 to your computer and use it in GitHub Desktop.
/root/ips-auth-sessionid-restart-telegram.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[root@nomad01test ~]# cat /root/ips-auth-sessionid-restart-telegram.sh | |
#!/bin/bash | |
TELEGRAM_BOT_TOKEN="571111150:AAG111cO7itQ7vfdsfdsfdsjFDFDpGl9FDFDSdDOz4" | |
TELEGRAM_CHAT_ID="-100111111971111" | |
job_name="ips-auth" | |
error_pattern="Connection state is null, that means there is not any active connection" | |
last_restart_file="/tmp/last_restart_timestamp.txt" | |
min_restart_interval=1 # Set to a low value for testing purposes | |
# Function to send a message to Telegram | |
send_message_to_telegram() { | |
local message="$1" | |
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \ | |
--data-urlencode "chat_id=$TELEGRAM_CHAT_ID" \ | |
--data-urlencode "text=$message" > /dev/null | |
} | |
# Check if the error exists in the logs | |
if nomad alloc logs -job "$job_name" | grep -E "$error_pattern" | grep -q "$error_pattern"; then | |
error_message="⚠️ Self-Healing Monitoring IPS-Auth ⚠️ $(date +%d-%m-%Y-%H:%M) Error(Connection state is null, that means there is not any active connection) found: $error_pattern. Restarting $job_name." | |
# Send error message to Telegram | |
send_message_to_telegram "$error_message" | |
# Restart the job | |
nomad job status "$job_name" | awk '{if (/run(.*)running/) {system("nomad alloc restart " $1)}}' | |
# Update the last restart timestamp | |
date +%s > "$last_restart_file" | |
else | |
success_message="⚠️ Self-Healing Monitoring IPS-Auth ⚠️ $(date +%d-%m-%Y-%H:%M) No error(Connection state is null, that means there is not any active connection) found in the IPS-Auth logs. " | |
# Send success message to Telegram | |
send_message_to_telegram "$success_message" | |
echo "$success_message" | |
fi | |
[root@nomad01test ~]# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment