conf-10-check-nrpe/conf/usr/local/sbin/check_git
2020-10-23 19:15:21 +02:00

127 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
################################################################################
# Script permettant de vérifier la cohérence entre des depôts Git clonés
# et les fichiers présents sur le système
################################################################################
# Copyright © 2019-2020 Alban Vidal - zordhak@debian.org
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
################################################################################
# Vérifie sur le fichier listant les repo existe, sinon échec
# TODO : Mettre à jour le README et expliquer un peu comment fonctionne ce check
if [ ! -f /etc/zorval/liste_repo_check_git ] ; then
echo "ERREUR, le fichier /etc/zorval/liste_repo_check_git n'existe pas"
echo "Merci de faire un tour sur le dépôt Git suivant :"
echo "https://framagit.org/zorval/scripts/check-nrpe"
exit 2
fi
################################################################################
RETURN_CODE=0
EXIT_MESSAGE=""
################################################################################
# Pour chaque repo :
# - Partie n°1 : git status
# - Partie n°2 : boucle pour checker chaque fichier
# - Partie n°3 : boucle pour checker les liens symboliques
while read REPO ; do
# Test si le répertoire existe ou non
if [ ! -d $REPO ] ; then
RETURN_CODE=1
EXIT_MESSAGE="${EXIT_MESSAGE}\n- HS ${REPO} -> n'existe pas\n"
continue # Passage à l'occurence suivante (le check de ce repo est ignoré)
fi
# On retire l'éventuel '/' à la fin de $REPO, sinon le check ne fonctionne pas
REPO=${REPO%/}
#------------------------------------------------------------#
# Partie n° 1 - git pull et status
cd $REPO
# pull désactivé car l'utilisateur 'nagios' n'a pas les droits pour puller
#git pull > /dev/null 2>&1
OUTPUT=$(git status --porcelain)
# Si 'git status' renvoie quelque chose -> Warning
if [ ! -z "$OUTPUT" ]; then
RETURN_CODE=1
EXIT_MESSAGE="${EXIT_MESSAGE}\n- HS ${REPO} -> 'git status' incorrect."
EXIT_MESSAGE="${EXIT_MESSAGE}\n${OUTPUT}"
else
EXIT_MESSAGE="${EXIT_MESSAGE}\n- OK ${REPO}"
fi
#------------------------------------------------------------#
# Partie n°2 : boucle pour checker chaque fichier
FILES=$(find $REPO -type f) # Liste des fichiers dans le dépot
# Pour chaque fichier
IFS=$'\n'
for FILE in $FILES; do
# On fait un sha1sum dans le repo git
REPO_SHA1SUM=$(sha1sum $FILE | awk '{print $1}')
FILE=$(echo $FILE | sed "s,$REPO,,")
# Si le fichier existe dans /
if [ -f $FILE ]; then
# On fait un sha1sum du fichier dans /
LOCAL_SHA1SUM=$(sha1sum $FILE | awk '{print $1}')
# Si le sha1sum est différent, les fichiers sont différents
if [ "$REPO_SHA1SUM" != "$LOCAL_SHA1SUM" ]; then
RETURN_CODE=1
EXIT_MESSAGE="${EXIT_MESSAGE}\n diff ${FILE#/} $FILE"
fi
# Sinon s'il n'existe pas
else
RETURN_CODE=1
EXIT_MESSAGE="${EXIT_MESSAGE}\n Absent : $FILE"
fi
done
#------------------------------------------------------------#
# Partie n°3 : Vérification des liens symboliques présents dans le dépôt
LN_REPO=$(find $REPO -type l) # Liste des liens symboliques dans le dépôt
for LN in $LN_REPO ; do
# On vérifie si il existe en local
LN=$(echo $LN | sed "s,$REPO,,")
# Test si il existe ou non en local
if [ ! -L $LN ] ; then
RETURN_CODE=1
EXIT_MESSAGE="${EXIT_MESSAGE}\n Lien absent : $LN"
fi
done
EXIT_MESSAGE="${EXIT_MESSAGE}\n" # Ajoute à retour à la ligne à la fin du check d'un dépôt
done < /etc/zorval/liste_repo_check_git
################################################################################
if [ $RETURN_CODE -eq 0 ] ; then
echo -e "Git OK\n${EXIT_MESSAGE}"
else
echo -e "Git ERROR\n${EXIT_MESSAGE}"
fi
exit $RETURN_CODE