#!/bin/bash # ============================================================================= # SAP HANA Disk Space Monitoring Script # Checks disk usage for configured directories with auto-cleanup capability # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" SCRIPT_NAME="hana_disk" # Load configuration source "${SCRIPT_DIR}/hana.conf" source "${SCRIPT_DIR}/hana_lib.sh" # Acquire lock LOCK_FILE=$(acquire_lock "$SCRIPT_NAME") if [ $? -ne 0 ]; then exit 1 fi trap 'release_lock "$LOCK_FILE"' EXIT log_message "$SCRIPT_NAME" "Starting disk usage check..." # Track overall status ALERT_COUNT=0 TOTAL_DIRS=0 CLEANUP_PERFORMED=0 for dir in "${DIRECTORIES_TO_MONITOR[@]}"; do TOTAL_DIRS=$((TOTAL_DIRS + 1)) # Check if directory exists if [ ! -d "$dir" ]; then log_message "$SCRIPT_NAME" "WARNING: Directory '$dir' not found. Skipping." send_notification_if_changed "$SCRIPT_NAME" "disk_dir_not_found_${dir//\//_}" "HANA Disk Warning" "Directory '$dir' not found." "true" "DIR_NOT_FOUND" ALERT_COUNT=$((ALERT_COUNT + 1)) continue fi # Get disk usage percentage usage=$(get_disk_usage_percentage "$dir") if [ -z "$usage" ] || [ "$usage" -eq 0 ]; then log_message "$SCRIPT_NAME" "WARNING: Could not determine disk usage for '$dir'. Skipping." continue fi log_message "$SCRIPT_NAME" "Directory ${dir} is at ${usage}%" # Check if usage exceeds threshold if [ "$usage" -gt "$DISK_USAGE_THRESHOLD" ]; then log_message "$SCRIPT_NAME" "ALERT: ${dir} usage is at ${usage}% which is above the ${DISK_USAGE_THRESHOLD}% threshold." # Attempt auto-cleanup if enabled if [ "$AUTO_CLEANUP_ENABLED" == "true" ]; then log_message "$SCRIPT_NAME" "Attempting auto-cleanup for '${dir}'..." mount_point=$(get_mount_point "$dir") if auto_cleanup "$mount_point" "$MIN_FREE_SPACE_AFTER_CLEANUP"; then CLEANUP_PERFORMED=$((CLEANUP_PERFORMED + 1)) new_usage=$(get_disk_usage_percentage "$dir") log_message "$SCRIPT_NAME" "After cleanup, ${dir} usage is at ${new_usage}%" usage=$new_usage else log_message "$SCRIPT_NAME" "Auto-cleanup failed or no files to clean for '${dir}'" fi fi # Send notification with final usage after cleanup attempt send_notification_if_changed "$SCRIPT_NAME" "disk_usage_${dir//\//_}" "HANA Disk" "Disk usage for ${dir} is at ${usage}%." "true" "${usage}%" ALERT_COUNT=$((ALERT_COUNT + 1)) else # Send OK notification only if state changed from alert send_notification_if_changed "$SCRIPT_NAME" "disk_usage_${dir//\//_}" "HANA Disk" "Disk usage for ${dir} is at ${usage}% (below threshold)." "false" "OK" fi done # Summary logging log_message "$SCRIPT_NAME" "Disk check complete. Total: ${TOTAL_DIRS} dirs, ${ALERT_COUNT} alerts, ${CLEANUP_PERFORMED} cleanups performed." # Exit with status based on alerts if [ "$ALERT_COUNT" -gt 0 ]; then exit 1 fi exit 0