Monitorizar uso filesystem

De jagfloriano.com
Ir a la navegaciónIr a la búsqueda

Código del script

Script para realizar un análisis nocturno de un Filesystem que se llena todos los días.

#monitor /var usage
#*/5 3-6 * * * /root/monitor_var.sh

Código del script

#!/bin/bash

# Server /var Monitoring Script
# Author: Monitoring Script
# Description: Monitors /var growth and files created/modified

# ============================================
# CONFIGURATION
# ============================================

MONITOR_DIR="/var"
LOG_OUTPUT="/var/log/monitoring_var.log"

# ============================================
# FUNCTIONS
# ============================================

log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_OUTPUT"
}

get_disk_usage() {
    du -sh "$MONITOR_DIR" 2>/dev/null | awk '{print $1}'
}

get_inode_usage() {
    df -i "$MONITOR_DIR" 2>/dev/null | tail -1 | awk '{printf "%.0f%%", ($3/$2)*100}'
}

# Captures files created/modified in the last X minutes
find_recent_files() {
    local minutes=$1
    log_message "=== FILES MODIFIED IN THE LAST $minutes MINUTES ==="
    find "$MONITOR_DIR" -type f -mmin -"$minutes" 2>/dev/null | \
    while read file; do
        size=$(du -h "$file" 2>/dev/null | awk '{print $1}')
        modified=$(stat -c %y "$file" 2>/dev/null | cut -d' ' -f1,2)
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] $size | $file | Modified: $modified"
    done | sort -rh | head -30 >> "$LOG_OUTPUT"
}

# Monitors the most active logs
monitor_logs() {
    log_message "=== MOST ACTIVE LOGS IN /var/log ==="
    find /var/log -type f \( -name "*.log*" -o -name "*.gz" \) 2>/dev/null | \
    xargs ls -lhS 2>/dev/null | head -20 | awk '{print "[" strftime("%Y-%m-%d %H:%M:%S") "] " $5, $9}' >> "$LOG_OUTPUT"
}

# Checks growth of main directories
check_subdirs() {
    log_message "=== ANALYSIS OF SUBDIRECTORIES IN /var ==="
    du -sh /var/* 2>/dev/null | sort -rh | head -15 | while read line; do
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] $line" >> "$LOG_OUTPUT"
    done
}

# ============================================
# MAIN EXECUTION
# ============================================

main() {
    log_message "=========================================="
    log_message "MONITORING REPORT FOR /var"
    log_message "=========================================="
    log_message "Starting /var monitoring"
    log_message ""

    # Captures initial state
    log_message "DISK SPACE:"
    log_message "  Total used: $(get_disk_usage)"
    log_message "  Inodes: $(get_inode_usage)"
    log_message ""

    # Collects detailed information
    log_message "SUBDIRECTORIES:"
    check_subdirs
    log_message ""

    log_message "FILES MODIFIED (last 2 hours):"
    find_recent_files 120
    log_message ""

    log_message "LARGEST LOGS:"
    monitor_logs
    log_message ""
    log_message "Report completed successfully"
    log_message "=========================================="
    log_message ""
}

# Execute main function
main