- Replace state-based notifications with direct alert functions - Remove auto-cleanup functionality from disk monitoring and configuration - Simplify lock acquisition/release across all monitoring scripts - Add execute_hana_sql helper functions for consistent SQL execution - Remove state file tracking in favor of direct file operations - Standardize error handling with exit codes on critical failures - Clean up hana.conf by removing unused auto-delete directory settings
150 lines
4.2 KiB
Bash
150 lines
4.2 KiB
Bash
#!/bin/bash
|
|
# =============================================================================
|
|
# SAP HANA Monitoring Library - Shared Functions
|
|
# =============================================================================
|
|
|
|
# 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}"
|
|
}
|
|
|
|
# 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
|
|
}
|
|
|
|
# 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}"
|
|
}
|
|
|
|
# Send OK notification (state change from alert to normal)
|
|
# Usage: send_ok "SCRIPT_NAME" "TITLE_PREFIX" "MESSAGE"
|
|
send_ok() {
|
|
local script_name="$1"
|
|
local title_prefix="$2"
|
|
local message="$3"
|
|
send_notification "${title_prefix} Resolved" "✅ Resolved: ${message}"
|
|
log_message "$script_name" "RESOLVED: ${message}"
|
|
}
|
|
|
|
# Run command as HANA user using su
|
|
# Usage: run_as_hana_user "COMMAND"
|
|
run_as_hana_user() {
|
|
local command="$1"
|
|
su - "$HANA_USER" -c "$command"
|
|
}
|
|
|
|
# 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_PATH -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}'
|
|
}
|
|
|
|
# Get mount point for a directory
|
|
# Usage: get_mount_point "/path/to/dir"
|
|
# Returns: Mount point path
|
|
get_mount_point() {
|
|
local dir="$1"
|
|
df "$dir" 2>/dev/null | awk 'NR==2 {print $NF}'
|
|
}
|
|
|
|
# Get available disk space in KB for a directory
|
|
# Usage: get_available_space_kb "/path/to/dir"
|
|
# Returns: Available space in KB
|
|
get_available_space_kb() {
|
|
local dir="$1"
|
|
df -k "$dir" 2>/dev/null | awk 'NR==2 {print $4}'
|
|
}
|