Diferencia entre revisiones de «Monitorizar uso filesystem»
De jagfloriano.com
Ir a la navegaciónIr a la búsqueda
(Página creada con «== Código del script == <syntaxhighlight lang="bash"> #!/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 # ============================================…») |
|||
| Línea 1: | Línea 1: | ||
= Código del script = | |||
Script para realizar un análisis nocturno de un Filesystem que se llena todos los días. | |||
<syntaxhighlight lang="bash"> | |||
#monitor /var usage | |||
#*/5 3-6 * * * /root/monitor_var.sh | |||
</syntaxhighlight> | |||
== Código del script == | == Código del script == | ||
Revisión actual - 15:35 12 may 2026
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