From a278a8281b39502ce6c08d12014bd43764cd746a Mon Sep 17 00:00:00 2001 From: Nick Hepler Date: Sun, 20 Oct 2024 12:20:19 -0400 Subject: [PATCH] Initial commit. --- gotifyer.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 gotifyer.sh diff --git a/gotifyer.sh b/gotifyer.sh new file mode 100644 index 0000000..9d4abd6 --- /dev/null +++ b/gotifyer.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Configuration +GOTIFY_URL="/message" # Example: "http://gotify.example.com/message" +APP_TOKEN="" +MESSAGE_TITLE="" +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="gotify_notifications.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