Created
February 2, 2025 04:36
-
-
Save joelibaceta/1c9a27ceb9895eb4e3b96a0dbc202f28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Función de ayuda | |
usage() { | |
echo "Uso: $0 -t <metric> -w <warning_threshold> -c <critical_threshold>" | |
echo "Métricas disponibles: cpu, memory, disk" | |
exit 1 | |
} | |
# Procesar argumentos | |
while getopts "t:w:c:" opt; do | |
case "$opt" in | |
t) METRIC="$OPTARG" ;; | |
w) WARNING="$OPTARG" ;; | |
c) CRITICAL="$OPTARG" ;; | |
*) usage ;; | |
esac | |
done | |
# Validar argumentos | |
if [[ -z "$METRIC" || -z "$WARNING" || -z "$CRITICAL" ]]; then | |
usage | |
fi | |
# Función para obtener la métrica | |
get_metric_value() { | |
case "$METRIC" in | |
cpu) echo $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d. -f1) ;; | |
memory) echo $(free | awk '/Mem/{printf("%.0f", $3/$2 * 100)}') ;; | |
disk) echo $(df -h / | awk 'NR==2 {print $5}' | sed 's/%//') ;; | |
*) echo "Métrica no válida"; exit 1 ;; | |
esac | |
} | |
# Obtener el valor de la métrica | |
VALUE=$(get_metric_value) | |
# Evaluar estado y enviar alerta | |
if [[ "$VALUE" -ge "$CRITICAL" ]]; then | |
echo "⚠ CRÍTICO: $METRIC en $VALUE%!" | mail -s "Alerta de $METRIC" "[email protected]" | |
echo "Notificación CRÍTICA enviada." | |
elif [[ "$VALUE" -ge "$WARNING" ]]; then | |
echo "⚠ Advertencia: $METRIC en $VALUE%." | |
else | |
echo "✅ Todo en orden: $METRIC en $VALUE%." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
¿Cómo usarlo?