112 lines
4.8 KiB
Bash
112 lines
4.8 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
|
|
LOCK_FILE=$(acquire_lock "$SCRIPT_NAME")
|
|
if [ $? -ne 0 ]; then
|
|
exit 1
|
|
fi
|
|
trap 'release_lock "$LOCK_FILE"' 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;"
|
|
|
|
# Check if hdbsql is available
|
|
if [ ! -x "$HDBSQL_PATH" ]; then
|
|
log_message "$SCRIPT_NAME" "ERROR: hdbsql not found or not executable at ${HDBSQL_PATH}"
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_hdbsql_path" "HANA Monitor Error" "hdbsql not found or not executable at ${HDBSQL_PATH}" "true" "HDBSQL_ERROR"
|
|
exit 1
|
|
fi
|
|
|
|
# Execute SQL query as HANA user with improved error handling
|
|
readarray -t sql_output < <(su - "$HANA_USER" -c "$HDBSQL_PATH -U $HANA_USER_KEY -c \";\" \"$SQL_QUERY\"" 2>&1)
|
|
sql_status=$?
|
|
|
|
if [ $sql_status -ne 0 ]; then
|
|
error_message=$(printf '%s\n' "${sql_output[@]}")
|
|
log_message "$SCRIPT_NAME" "ERROR: The hdbsql command failed. Details: ${error_message}"
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_hdbsql_command" "HANA Monitor Error" "The hdbsql command failed. Details: ${error_message}" "true" "HDBSQL_COMMAND_FAILED"
|
|
exit 1
|
|
fi
|
|
|
|
# Parse SQL output
|
|
total_segments=0
|
|
truncated_segments=0
|
|
free_segments=0
|
|
|
|
for line in "${sql_output[@]}"; 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) # Trim whitespace
|
|
count=$(echo "$cleaned_line" | awk -F',' '{print $4}' | xargs) # Trim whitespace
|
|
|
|
# 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
|
|
|
|
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. Skipping percentage checks."
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_segments_total" "HANA Log Segment Warning" "No log segments found. Skipping percentage checks." "true" "NO_LOG_SEGMENTS"
|
|
else
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_segments_total" "HANA Log Segment" "Log segments found." "false" "OK"
|
|
|
|
# Calculate truncated percentage with integer arithmetic
|
|
if [ $total_segments -gt 0 ]; then
|
|
truncated_percentage=$((truncated_segments * 100 / total_segments))
|
|
else
|
|
truncated_percentage=0
|
|
fi
|
|
|
|
if [ $truncated_percentage -gt $TRUNCATED_PERCENTAGE_THRESHOLD ]; then
|
|
log_message "$SCRIPT_NAME" "ALERT: ${truncated_percentage}% of log segments are 'Truncated'."
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_truncated" "HANA Log Segment" "${truncated_percentage}% of HANA log segments are in 'Truncated' state." "true" "${truncated_percentage}%"
|
|
else
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_truncated" "HANA Log Segment" "${truncated_percentage}% of HANA log segments are in 'Truncated' state (below threshold)." "false" "OK"
|
|
fi
|
|
|
|
# Calculate free percentage with integer arithmetic
|
|
if [ $total_segments -gt 0 ]; then
|
|
free_percentage=$((free_segments * 100 / total_segments))
|
|
else
|
|
free_percentage=0
|
|
fi
|
|
|
|
if [ $free_percentage -lt $FREE_PERCENTAGE_THRESHOLD ]; then
|
|
log_message "$SCRIPT_NAME" "ALERT: Only ${free_percentage}% of log segments are 'Free'."
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_free" "HANA Log Segment" "Only ${free_percentage}% of HANA log segments are in 'Free' state." "true" "${free_percentage}%"
|
|
else
|
|
send_notification_if_changed "$SCRIPT_NAME" "hana_log_free" "HANA Log Segment" "Only ${free_percentage}% of HANA log segments are in 'Free' state (above threshold)." "false" "OK"
|
|
fi
|
|
fi
|
|
|
|
log_message "$SCRIPT_NAME" "Log segment check complete."
|