Files
hana-scripts/hana_log_segments.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

87 lines
2.9 KiB
Bash

#!/bin/bash
# =============================================================================
# SAP HANA Log Segment Monitoring Script
# Checks log segment states (Truncated, Free)
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
SCRIPT_NAME="hana_log_segments"
# Load configuration
source "${SCRIPT_DIR}/hana.conf"
source "${SCRIPT_DIR}/hana_lib.sh"
# Acquire lock
if ! acquire_lock "$SCRIPT_NAME"; then
exit 1
fi
trap 'release_lock "$SCRIPT_NAME"' EXIT
log_message "$SCRIPT_NAME" "Starting log segment check..."
# SQL Query for log segments
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;"
# Execute SQL query
sql_output=$(execute_hana_sql "$SQL_QUERY")
sql_status=$?
if [ $sql_status -ne 0 ]; then
log_message "$SCRIPT_NAME" "ERROR: The hdbsql command failed."
send_alert "$SCRIPT_NAME" "HANA Monitor Error" "The hdbsql command failed."
exit 1
fi
# Parse SQL output
total_segments=0
truncated_segments=0
free_segments=0
while IFS= read -r line; do
# Skip empty lines and header
if [[ -z "$line" || "$line" == *"STATE"* || "$line" == *"host"* ]]; then
continue
fi
cleaned_line=$(echo "$line" | tr -d '"')
state=$(echo "$cleaned_line" | awk -F',' '{print $3}' | xargs)
count=$(echo "$cleaned_line" | awk -F',' '{print $4}' | xargs)
# Validate count is a number
if ! [[ "$count" =~ ^[0-9]+$ ]]; then
continue
fi
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 <<< "$sql_output"
log_message "$SCRIPT_NAME" "Total Segments: ${total_segments}"
log_message "$SCRIPT_NAME" "Truncated Segments: ${truncated_segments}"
log_message "$SCRIPT_NAME" "Free Segments: ${free_segments}"
if [ $total_segments -eq 0 ]; then
log_message "$SCRIPT_NAME" "WARNING: No log segments found."
send_alert "$SCRIPT_NAME" "HANA Log Segment Warning" "No log segments found."
exit 1
fi
# Calculate percentages
truncated_percentage=$((truncated_segments * 100 / total_segments))
free_percentage=$((free_segments * 100 / total_segments))
# Check thresholds and alert
if [ $truncated_percentage -gt $TRUNCATED_PERCENTAGE_THRESHOLD ]; then
send_alert "$SCRIPT_NAME" "HANA Log Segment" "${truncated_percentage}% of log segments are 'Truncated' (threshold: ${TRUNCATED_PERCENTAGE_THRESHOLD}%)."
fi
if [ $free_percentage -lt $FREE_PERCENTAGE_THRESHOLD ]; then
send_alert "$SCRIPT_NAME" "HANA Log Segment" "Only ${free_percentage}% of log segments are 'Free' (threshold: ${FREE_PERCENTAGE_THRESHOLD}%)."
fi
log_message "$SCRIPT_NAME" "Log segment check complete."