61 lines
2.0 KiB
Bash
61 lines
2.0 KiB
Bash
#!/bin/bash
|
|
|
|
# Configuration
|
|
GOTIFY_URL="<YOUR_SERVER_URL>/message" # Example: "http://gotify.example.com/message"
|
|
APP_TOKEN="<YOUR_APP_TOKEN>"
|
|
MESSAGE_TITLE="<YOUR_MESSAGE_TITLE>"
|
|
MESSAGE="<YOUR_MESSAGE>"
|
|
HOSTNAME=$(hostname) # Get the hostname of the machine
|
|
TITLE="$HOSTNAME - $MESSAGE_TITLE" # Title with hostname prefixing the message
|
|
PRIORITY=5 # Notification priority (1-5)
|
|
LOG_FILE="gotifyer.log"
|
|
|
|
# Function to log messages
|
|
log_message() {
|
|
local message="$1"
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
|
|
}
|
|
|
|
# Function to check if the Gotify server is healthy
|
|
check_gotify_health() {
|
|
local base_url="${GOTIFY_URL%/*}" # Extract base URL (removes /message)
|
|
local health_url="$base_url/health" # Construct the health check URL
|
|
local response
|
|
|
|
# Make a request to the health endpoint and capture the response
|
|
response=$(curl --silent --write-out "%{http_code}" --output /dev/null "$health_url")
|
|
|
|
# Check if the response code is 200 (OK)
|
|
if [ "$response" -eq 200 ]; then
|
|
# Get the actual health status
|
|
health_status=$(curl --silent "$health_url")
|
|
echo "$health_status"
|
|
else
|
|
echo "Health check failed. HTTP status code: $response"
|
|
fi
|
|
}
|
|
|
|
# Check if the Gotify server is healthy
|
|
health_check_result=$(check_gotify_health)
|
|
if [[ $health_check_result != *"\"health\":\"green\""* ]]; then
|
|
log_message "Gotify server is down or not healthy: $health_check_result. Exiting."
|
|
exit 1
|
|
fi
|
|
|
|
# Send notification
|
|
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$GOTIFY_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-H "X-Gotify-Key: $APP_TOKEN" \
|
|
-d "{
|
|
\"title\": \"$TITLE\",
|
|
\"message\": \"$MESSAGE\",
|
|
\"priority\": $PRIORITY
|
|
}")
|
|
|
|
# Check response and log result
|
|
if [ "$response" -eq 200 ]; then
|
|
log_message "Notification sent successfully: $MESSAGE"
|
|
else
|
|
log_message "Failed to send notification. HTTP status code: $response. Title: $TITLE, Message: $MESSAGE, Priority: $PRIORITY"
|
|
fi
|