#!/usr/bin/env bash ################################################################################ # BSD 3-Clause License # # Copyright (c) 2020-2021, Alban Vidal # Copyright (c) 2020-2021, Quentin Lejard # All rights reserved. # # Please see license file on root of this directory ################################################################################ # !! GIT FILE !! # https://framagit.org/zorval/scripts/check-nrpe ################################################################################ # Pour un retour en debug # Pour activer, lancer le script avec la variable d'environnement DEBUG=true DEBUG=${DEBUG:=false} $DEBUG && echo -e "\n\t\t\t~~~~ DEBUG MODE ~~~~" # Option possible : --print pour afficher sur l'écran d'accueil # Si pas d'option : directement utilisable par NRPE OPTION_1=$1 PRINT_MODE=false if [ ! -z "$OPTION_1" ]; then if [ "$OPTION_1" = "--print" ]; then PRINT_MODE=true else echo "[CRITICAL] Option '$OPTION_1' inconnue" exit 2 fi fi FIC_COLORS=/etc/profile.d/00_couleurs.sh # Récupération des couleurs (NORMAL GRAS SSLIGNE ROUGE VERT BLEU GRIS) if [ "$PRINT_MODE" = true ]; then if [ -f $FIC_COLORS ]; then source $FIC_COLORS else echo "[CRITICAL] Impossible de lire le fichier $FIC_COLORS" exit 2 fi fi # Répertoire des logs perso REP_LOG="/var/log/check_log" # Répertoire ulogd REP_ULOGD="/var/log/ulog" # Fichier iptables ou nftables [ -f '/var/log/iptables.log' ] && FIC_TABLES_LOG='/var/log/iptables.log' [ -f '/var/log/nftables.log' ] && FIC_TABLES_LOG='/var/log/nftables.log' # Pour définir le code retour ERROR=false # Pour la concaténation des logs LOG_OK="[OK]" LOG_ERR="[CRITICAL]" ################################################################################ # Si le répertoire des logs perso n'existe pas, on met un petit WARNING if [ ! -d "$REP_LOG" ] ; then echo "[WARNING] Le répertoire $REP_LOG n'existe pas" exit 1 # Sortie en Warning fi ################################################################################ # Fonction permettant de trouver les .log non vides d'un répertoire function find_log() { FIND_DIR=$1 # Cherche tous les fichiers .log présents dans le répertoire LISTE_LOG=$(find $FIND_DIR ! -empty -type f -name '*.log') if [ "$LISTE_LOG" ] ; then # Pour chaque fichier présent for LOG in $LISTE_LOG ; do $DEBUG && echo -e "DEBUG\t\t\tERROR\t$LOG" # Concaténation du log d'erreur FIC_ERR="$FIC_ERR\n$LOG" # Et on dit qu'on a au moins une erreur ERROR=true done else $DEBUG && echo -e "DEBUG\t\t\t$LISTE_LOG est vide\t" fi } find_log $REP_LOG if [ -d $REP_ULOGD ] ; then find_log $REP_ULOGD fi ################################################################################ # Test si le fichier # /var/log/iptables.log ou /var/log/nftables.log # n'est pas vide if [ "$FIC_TABLES_LOG" ];then if [ -s $FIC_TABLES_LOG ];then $DEBUG && echo -e "DEBUG\t\t\tERROR\tFichier $FIC_TABLES_LOG non vide" FIC_ERR="$FIC_ERR\n$FIC_TABLES_LOG" ERROR=true else $DEBUG && echo -e "DEBUG\t\t\t$FIC_TABLES_LOG est vide\t" fi fi ################################################################################ # AFFICHAGE ${PRINT_MODE} && echo -e "${GRAS}${SSLIGNE}${LBLEU}Vérification des logs${NORMAL}\n" # Si on a au moins un erreur, retour nagios critique if $ERROR ; then ${PRINT_MODE} && echo -e "${ROUGE}${LOG_ERR}\033[38;5;208m$FIC_ERR${NORMAL}" || echo -e "${LOG_ERR}\n${FIC_ERR}" exit 2 else ${PRINT_MODE} && echo -e "${VERT}$LOG_OK${NORMAL}" || echo -e "$LOG_OK" exit 0 fi