Files
hana-scripts/hana_lib.sh
Tomi Eckert 7495ebcd78 refactor(monitoring): remove hardcoded tool paths
Remove SAPCONTROL_PATH and HDBSQL_PATH variables from configuration.
Update scripts to rely on the system PATH environment variable when
executing as the <sid>adm user. Remove redundant existence checks for
these commands.
2026-03-12 22:24:02 +01:00

135 lines
3.8 KiB
Bash

#!/bin/bash
# =============================================================================
# SAP HANA Monitoring Library - Shared Functions
# =============================================================================
# Initialize script with common setup
# Usage: init_script "SCRIPT_NAME"
# Sets up: SCRIPT_DIR, SCRIPT_NAME, LOG_FILE, LOCK_DIR
init_script() {
SCRIPT_NAME="$1"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# Load configuration
source "${SCRIPT_DIR}/hana.conf"
# Setup logging
LOG_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
LOG_FILE="${LOG_DIR}/hana_monitor.log"
# Setup lock directory
LOCK_DIR="/tmp"
}
# Acquire lock for script execution
# Usage: acquire_lock "SCRIPT_NAME"
# Returns: 0 on success, 1 on failure (already running)
acquire_lock() {
local script_name="$1"
local lock_file="${LOCK_DIR}/hana_${script_name}.lock"
if [ -e "$lock_file" ]; then
log_message "$script_name" "Script is already running. Exiting."
return 1
fi
touch "$lock_file"
return 0
}
# Release lock
# Usage: release_lock "SCRIPT_NAME"
release_lock() {
local script_name="$1"
local lock_file="${LOCK_DIR}/hana_${script_name}.lock"
if [ -f "$lock_file" ]; then
rm -f "$lock_file"
fi
}
# Logging function with script name prefix
# Usage: log_message "SCRIPT_NAME" "message"
log_message() {
local script_name="$1"
local message="$2"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
echo "[${timestamp}] [${script_name}] ${message}" | tee -a "${LOG_FILE}"
}
# Send notification via ntfy
# Usage: send_notification "TITLE" "MESSAGE"
send_notification() {
local title="$1"
local message="$2"
local hostname=$(hostname)
local full_message="[${COMPANY_NAME} | ${hostname}] ${message}"
if [ -n "$NTFY_TOKEN" ] && [ -n "$NTFY_TOPIC_URL" ]; then
curl -H "Authorization: Bearer ${NTFY_TOKEN}" -H "Title: ${title}" -d "${full_message}" "${NTFY_TOPIC_URL}" > /dev/null 2>&1
log_message "NOTIFY" "Notification sent: ${title}"
else
log_message "NOTIFY" "Ntfy not configured, skipping notification"
fi
}
# Send alert notification
# Usage: send_alert "SCRIPT_NAME" "TITLE_PREFIX" "MESSAGE"
send_alert() {
local script_name="$1"
local title_prefix="$2"
local message="$3"
send_notification "${title_prefix} Alert" "🚨 Critical: ${message}"
log_message "$script_name" "ALERT: ${message}"
}
# Execute SQL query as HANA user
# Usage: execute_hana_sql "SQL_QUERY"
# Returns: SQL output on stdout, returns 0 on success, 1 on failure
execute_hana_sql() {
local sql_query="$1"
local output
output=$(su - "$HANA_USER" -c "hdbsql -U $HANA_USER_KEY -j -a -x \"$sql_query\"" 2>&1)
local sql_status=$?
if [ $sql_status -ne 0 ]; then
log_message "SQL" "ERROR: Failed to execute SQL query. Exit code: ${sql_status}"
echo "$output" >&2
return 1
fi
echo "$output"
return 0
}
# Execute SQL query and return result (for single-value queries)
# Usage: execute_hana_sql_query "SQL_QUERY"
# Returns: Query result on stdout, returns 0 on success, 1 on failure
execute_hana_sql_query() {
local sql_query="$1"
local output
output=$(execute_hana_sql "$sql_query")
local sql_status=$?
if [ $sql_status -ne 0 ]; then
return 1
fi
# Clean output: remove quotes and whitespace
echo "$output" | tr -d '"' | xargs
return 0
}
# Get disk usage percentage for a directory
# Usage: get_disk_usage_percentage "/path/to/dir"
# Returns: Usage percentage as integer (without % sign)
get_disk_usage_percentage() {
local dir="$1"
if [ ! -d "$dir" ]; then
echo "0"
return
fi
df "$dir" 2>/dev/null | awk 'NR==2 {gsub(/%/,"",$5); print $5}'
}