diff --git a/monitor/monitor.conf b/monitor/monitor.conf new file mode 100644 index 0000000..3fed859 --- /dev/null +++ b/monitor/monitor.conf @@ -0,0 +1,32 @@ +# Configuration for SAP HANA Monitoring Script + +# --- Company Information --- +# Used to identify which company the alert is for. +COMPANY_NAME="Your Company Name" + +# --- Notification Settings --- +# Your ntfy.sh topic URL +NTFY_TOPIC_URL="https://ntfy.technopunk.space/sap" +# Your ntfy.sh bearer token (if required) +NTFY_TOKEN="your_ntfy_token_here" + +# --- HANA Connection Settings --- +# Full path to the hdbsql executable +HDBSQL_PATH="/usr/sap/hdbclient/hdbsql" +# HANA user key for authentication +HANA_USER_KEY="CRONKEY" +# HANA Instance Number for sapcontrol +HANA_INSTANCE_NR="00" + +# --- Monitoring Thresholds --- +# Disk usage percentage that triggers an alert +DISK_USAGE_THRESHOLD=80 +# Percentage of 'Truncated' log segments that triggers an alert +TRUNCATED_PERCENTAGE_THRESHOLD=50 +# Percentage of 'Free' log segments below which an alert is triggered +FREE_PERCENTAGE_THRESHOLD=25 + +# --- Monitored Directories --- +# List of directories to check for disk usage (space-separated) +DIRECTORIES_TO_MONITOR=("/hana/log" "/hana/shared" "/hana/data" "/usr/sap") + diff --git a/monitor/monitor.sh b/monitor/monitor.sh new file mode 100644 index 0000000..4fcd4a8 --- /dev/null +++ b/monitor/monitor.sh @@ -0,0 +1,121 @@ +#!/bin/bash +# Version: 1.0.0 +# ============================================================================= +# SAP HANA Monitoring Script +# +# Checks HANA processes, disk usage, and log segment state. +# Sends ntfy.sh notifications if thresholds are exceeded. +# ============================================================================= + +# --- Lock File Implementation --- +LOCK_FILE="/tmp/hana_monitor.lock" +if [ -e "$LOCK_FILE" ]; then + echo "▶️ Script is already running. Exiting." + exit 1 +fi +touch "$LOCK_FILE" +# Ensure lock file is removed on script exit +trap 'rm -f "$LOCK_FILE"' EXIT + +# --- Configuration and Setup --- +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +CONFIG_FILE="${SCRIPT_DIR}/monitor_hana.conf" + +if [ ! -f "$CONFIG_FILE" ]; then + echo "❌ Error: Configuration file not found at ${CONFIG_FILE}" + rm -f "$LOCK_FILE" + exit 1 +fi +source "$CONFIG_FILE" + +HOSTNAME=$(hostname) +SQL_QUERY="SELECT b.host, b.service_name, a.state, count(*) FROM PUBLIC.M_LOG_SEGMENTS a JOIN PUBLIC.M_SERVICES b ON (a.host = b.host AND a.port = b.port) GROUP BY b.host, b.service_name, a.state;" + +send_notification() { + local title="$1" + local message="$2" + local full_message="[${COMPANY_NAME} | ${HOSTNAME}] ${message}" + curl -H "Authorization: Bearer ${NTFY_TOKEN}" -H "Title: ${title}" -d "${full_message}" "${NTFY_TOPIC_URL}" +} + +# --- HANA Process Status --- +echo "⚙️ Checking HANA process status..." +non_green_processes=$(sapcontrol -nr "${HANA_INSTANCE_NR}" -function GetProcessList | tail -n +5 | grep -v 'GREEN') + +if [ -n "$non_green_processes" ]; then + echo "🚨 Alert: One or more HANA processes are not running!" + echo "$non_green_processes" + send_notification "HANA Process Alert" "🚨 Critical: One or more HANA processes are not GREEN. Problem processes: ${non_green_processes}" + exit 1 # Exit early as other checks might fail +fi +echo "✅ Success! All HANA processes are GREEN." + +# --- Disk Space Monitoring --- +echo "ℹ️ Checking disk usage..." +for dir in "${DIRECTORIES_TO_MONITOR[@]}"; do + if [ ! -d "$dir" ]; then + echo "⚠️ Warning: Directory '$dir' not found. Skipping." + continue + fi + usage=$(df -h "$dir" | awk 'NR==2 {print $5}' | sed 's/%//') + echo " - ${dir} is at ${usage}%" + if (( $(echo "$usage > $DISK_USAGE_THRESHOLD" | bc -l) )); then + echo "🚨 Alert: ${dir} usage is at ${usage}% which is above the ${DISK_USAGE_THRESHOLD}% threshold." + send_notification "HANA Disk Alert" "🚨 Critical: Disk usage for ${dir} is at ${usage}%." + fi +done + +# --- HANA Log Segment Monitoring --- +echo "⚙️ Executing HANA SQL query..." +if [ ! -x "$HDBSQL_PATH" ]; then + echo "❌ Error: hdbsql not found or not executable at ${HDBSQL_PATH}" + send_notification "HANA Monitor Error" "❌ Error: hdbsql not found or not executable at ${HDBSQL_PATH}" + exit 1 +fi +readarray -t sql_output < <("$HDBSQL_PATH" -U "$HANA_USER_KEY" -c ";" "$SQL_QUERY" 2>&1) +if [ $? -ne 0 ]; then + echo "❌ Failure! The hdbsql command failed. Please check logs." + error_message=$(printf '%s\n' "${sql_output[@]}") + send_notification "HANA Monitor Error" "❌ Failure! The hdbsql command failed. Details: ${error_message}" + exit 1 +fi + +total_segments=0 +truncated_segments=0 +free_segments=0 +for line in "${sql_output[@]}"; do + if [[ -z "$line" || "$line" == *"STATE"* ]]; then continue; fi + cleaned_line=$(echo "$line" | tr -d '"') + state=$(echo "$cleaned_line" | awk -F',' '{print $3}') + count=$(echo "$cleaned_line" | awk -F',' '{print $4}') + total_segments=$((total_segments + count)) + if [[ "$state" == "Truncated" ]]; then + truncated_segments=$((truncated_segments + count)) + elif [[ "$state" == "Free" ]]; then + free_segments=$((free_segments + count)) + fi +done + +echo "ℹ️ Total Segments: ${total_segments}" +echo "ℹ️ Truncated Segments: ${truncated_segments}" +echo "ℹ️ Free Segments: ${free_segments}" + +if [ $total_segments -eq 0 ]; then + echo "⚠️ Warning: No log segments found. Skipping percentage checks." + exit 0 +fi + +truncated_percentage=$((truncated_segments * 100 / total_segments)) +if (( $(echo "$truncated_percentage > $TRUNCATED_PERCENTAGE_THRESHOLD" | bc -l) )); then + echo "🚨 Alert: ${truncated_percentage}% of log segments are 'Truncated'." + send_notification "HANA Log Segment Alert" "🚨 Alert: ${truncated_percentage}% of HANA log segments are in 'Truncated' state." +fi + +free_percentage=$((free_segments * 100 / total_segments)) +if (( $(echo "$free_percentage < $FREE_PERCENTAGE_THRESHOLD" | bc -l) )); then + echo "🚨 Alert: Only ${free_percentage}% of log segments are 'Free'." + send_notification "HANA Log Segment Alert" "🚨 Alert: Only ${free_percentage}% of HANA log segments are in 'Free' state." +fi + +echo "✅ Success! HANA monitoring check complete." + diff --git a/packages.conf b/packages.conf index 6571365..fab68e3 100644 --- a/packages.conf +++ b/packages.conf @@ -9,6 +9,7 @@ declare -A SCRIPT_PACKAGES # The version should match the "# Version: x.x.x" line in the main script file. SCRIPT_PACKAGES["Aurora Suite"]="1.1.0|https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.conf" SCRIPT_PACKAGES["Backup Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.conf" +SCRIPT_PACKAGES["Monitor Suite"]="1.0.0|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.conf" SCRIPT_PACKAGES["Key Manager"]="1.2.1|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh" SCRIPT_PACKAGES["File Cleaner"]="1.1.0|https://git.technopunk.space/tomi/Scripts/raw/branch/main/clean.sh" SCRIPT_PACKAGES["HANA Tool"]="1.4.2|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanatool.sh"