El siguiente plugin NAGIOS en Scripts Bash, lo realice para monitorear un servidor LINUX, el cual se creo solo con un File System principal (/). Y se requiere monitorear el espacio disponible en los siguiente Directorios: /usr /usr01 /usr02 A estos directorios les asignaremos un máximo de uso de 15 20 y 30 Gigas de uso en disco. El plugin debe alertar en estado WARNING cuando sobrepasen el 70 % de uso y alertar en estado CRITICAL, cuando sobrepase el 90% de uso.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/bin/bash crit="" ; warn="" ; ok="" global_status=0 # Procesar cada parametro for param in "$@"; do IFS='-' read -r fs limit warning critical <<< "$param" # Obtiene los valores de DIRECTORIOS USED=$(du -sBG "$fs" 2>/dev/null | awk '{print $1}' | sed 's/G//') if [ -z "$USED" ]; then echo "UNKNOWN - uso de $fs" exit 3 fi # Calcular porcentaje de uso used_percent=$(( USED * 100 / limit )) if [ "$used_percent" -ge "$critical" ]; then crit="${crit}${fs}-${USED}G(${used_percent}%) " global_status=2 elif [ "$used_percent" -ge "$warning" ]; then warn="${warn}${fs}-${USED}G(${used_percent}%) " [ "$global_status" -lt 2 ] && global_status=1 else ok="${ok}${fs}-${USED}G(${used_percent}%) " fi done if [ "$global_status" -eq 0 ]; then echo "OK=$ok" exit 0 elif [ "$global_status" -eq 1 ]; then echo "WARN=$warn OK=$ok" exit 1 elif [ "$global_status" -eq 2 ]; then output="CRIT=$crit" [ -n "$warn" ] && output="$output WARN=$warn" [ -n "$ok" ] && output="$output OK=$ok" echo "$output" exit 2 fi # Si no se cumple ninguna condicion "Error: $global_status No definido" exit 3 |
Al ejecutarlo directamente en un servidor. Entregaría el siguiente resultado :
|
1 2 3 |
./checkdir.sh "/usr01-42-95-98" "/usr-5-95-98" "/usr02-1-95-98" CRIT=/usr01-43G(102%) /usr02-1G(100%) OK=/usr-3G(60%) |