#!/bin/bash
. /usr/lib/eole/diagnose.sh
len_pf_accent=$((len_pf+1))

TestBareosMonitorLocal(){
     printf "Test de $1 :\n"
        if [ ! "$3" = "oui" ]; then
            Inactif "$1"
        else
            printf ".  %${len_pf}s => " "$1"
            pass=$(CreoleGet bareos.monitor.bareos_mon_password)
            /usr/lib/nagios/plugins/check_bareos -H localhost -D $2 -K "$pass" -M bareos-$(CreoleGet nom_machine)-mon > /dev/null 2>&1
            if [ $? -eq 0 ]; then          
                EchoVert "Ok"
                printf ".  %${len_pf}s => " "fichier de configuration"
                bareos-$2 -t -c /etc/bareos/bareos-$2.conf > /dev/null 2>&1
                ret=$?
                    if [ $ret -eq 0 ]
                    then
                        EchoVert "Ok"
                    else
                        EchoRouge "Erreur"
                        return 1
                    fi
                return 0
            else
            EchoRouge "Erreur"
            return 1
            fi
        fi
}
TestClientDistant(){
    if [ "$(CreoleGet activer_bareos_dir)" = 'oui' ]; then
        RemoteFD="$(CreoleGet bareos_dir_set_remote_fds)" 
        if [ $RemoteFD = "oui" ]; then
            declare -a FILER_NAME
            declare -a FILER_IP
            declare -a FILER_PWD
            FILER_NAME=($(CreoleGet bareos_dir_remote_fd_display_name))
            FILER_IP=($(CreoleGet bareos_dir_remote_fd_address))
            FILER_PWD=($(CreoleGet bareos_dir_remote_fd_password))
            DIR_NAME=$(CreoleGet bareos_dir_name)
            NB_FILER=${#FILER_NAME[*]}
            printf ".  %${len_pf}s" "sauvegardes distantes ($NB_FILER clients)"
            printf "\n"
            for ((id=0; id < $NB_FILER; id+=1)) do
                printf ".  %${len_pf}s =>" "Client ${FILER_NAME[id]}"
                /usr/lib/nagios/plugins/check_bareos -H ${FILER_IP[id]} -D fd -K "${FILER_PWD[id]}" -M $DIR_NAME > /dev/null 2>&1
                if [ $? -eq 0 ]; then          
                    EchoVert " Ok"
                else 
                    EchoRouge " Erreur"
                fi
            done
        fi
    fi
}
EchoGras "*** Sauvegarde"
bareos_dir=$(CreoleGet activer_bareos_dir)
TestBareosMonitorLocal "Bareos Director" dir "$bareos_dir"
#Test du client a faire si le directeur est activé
if [ "$bareos_dir" = "oui" ]; then
    TestBareosMonitorLocal "Bareos Client" fd "$bareos_dir"
else
    TestBareosMonitorLocal "Bareos Client" fd "$(CreoleGet activer_bareos_fd)"   
fi
TestClientDistant
TestBareosMonitorLocal "Bareos Storage" sd "$(CreoleGet activer_bareos_sd)"

 if [ "$(CreoleGet activer_bareos_sd)" = 'oui' ];then
        if [ "$(CreoleGet bareos.support.support_type)" = 'none' ];then
            NoConfig "Montage du support"
        else
            printf ".  %${len_pf}s => " "Montage du support"
            /usr/share/eole/sbin/bareosmount.py -t -f > /dev/null 2>&1
            if [ $? = 0 ]; then
                EchoVert "Ok"
            else
                EchoRouge "Erreur"
            fi
        fi
    fi
if [ "$bareos_dir" = "oui" ]; then
        # utilisation des fonctions de pyeole/bareos.py
        BAREOS_RAPPORT_OK="1"
        BAREOS_RAPPORT_ERR="-1"
        #BAREOS_RAPPORT_UNKNOWN="0"
        printf "Statut des sauvegardes :\n"
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load; rap = bareos_rapport_load("cronpre"); print "RAP[0]=\"%s\";RAP[1]=\"%s\";" %(rap[0], rap[1])'`
        printf ".  %${len_pf_accent}s => " "préparation sauvegarde"
        if [ "${RAP[0]}" = $BAREOS_RAPPORT_OK ]; then
            EchoVert "Ok : ${RAP[1]}"
        elif [ "${RAP[0]}" = $BAREOS_RAPPORT_ERR ]; then
            EchoRouge "Erreur : ${RAP[1]}"
        else
            EchoOrange "Inconnu : ${RAP[1]}"
        fi
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load; rap = bareos_rapport_load("sauvegarde"); print "RAP[0]=\"%s\";RAP[1]=\"%s\";" %(rap[0], rap[1])'`
        printf ".  %${len_pf}s => " "sauvegarde principale"
        if [ "${RAP[0]}" = $BAREOS_RAPPORT_OK ]; then
            EchoVert "Ok : ${RAP[1]}"
        elif [ "${RAP[0]}" = $BAREOS_RAPPORT_ERR ]; then
            EchoRouge "Erreur : ${RAP[1]}"
        else
            EchoOrange "Inconnu : ${RAP[1]}"
        fi
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load; rap = bareos_rapport_load("catalogue"); print "RAP[0]=\"%s\";RAP[1]=\"%s\";" %(rap[0], rap[1])'`
        printf ".  %${len_pf}s => " "sauvegarde catalogue"
        if [ "${RAP[0]}" = $BAREOS_RAPPORT_OK ]; then
            EchoVert "Ok : ${RAP[1]}"
        elif [ "${RAP[0]}" = $BAREOS_RAPPORT_ERR ]; then
            EchoRouge "Erreur : ${RAP[1]}"
        else
            EchoOrange "Inconnu : ${RAP[1]}"
        fi
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load; rap = bareos_rapport_load("cronpost"); print "RAP[0]=\"%s\";RAP[1]=\"%s\";" %(rap[0], rap[1])'`
        if [ "${RAP[0]}" = $BAREOS_RAPPORT_OK ]; then
            printf ".  %${len_pf_accent}s => " "exécution cron par bareos"
            EchoVert "Ok : ${RAP[1]}"
        elif [ "${RAP[0]}" = $BAREOS_RAPPORT_ERR ]; then
            printf ".  %${len_pf_accent}s => " "exécution cron par bareos"
            EchoRouge "Erreur : ${RAP[1]}"
        fi
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load_pcent_usage; rap = bareos_rapport_load_pcent_usage(); print "PCENT=\"%s\";IPCENT=\"%s\";" %(rap[0], rap[1])'`
        if [ ! $PCENT = -1 ] &&  [ ! $IPCENT = -1 ]; then
            printf "Espace sur le support de sauvegarde :\n"
            if [ ! $PCENT = -1 ]; then
                printf ".  %${len_pf_accent}s => " "espace utilisé sur le support"
                if [ "$PCENT" -lt 80 ]; then
                    EchoVert "$PCENT%"
                elif [ "$PCENT" -lt 90 ]; then
                    EchoOrange "$PCENT%"
                else
                    EchoRouge "$PCENT%"
                fi
            fi
            if [ ! $IPCENT = -1 ]; then
                printf ".  %${len_pf_accent}s => " "inodes utilisés sur le support"
                if [ "$IPCENT" -lt 80 ]; then
                    EchoVert "$IPCENT%"
                elif [ "$IPCENT" -lt 90 ]; then
                    EchoOrange "$IPCENT%"
                else
                    EchoRouge "$IPCENT%"
                fi
            fi
        fi
        eval `/usr/bin/env python -c 'from pyeole.bareos import bareos_rapport_load_free_space; rap = bareos_rapport_load_free_space(); print "STATUS=\"%s\";COMMENT=\"%s\";" %(rap[0], rap[1])'`
        printf "Espace disponible sur le support :\n"
        printf ".  %${len_pf_accent}s => " "estimation de l'espace disponible"
            if [ $STATUS = -1 ]; then
                EchoOrange "$COMMENT"
            elif [ $STATUS = 1 ]; then
                EchoVert "$COMMENT"
            fi

    echo
    fi
exit 0

