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

TestBareosDaemon(){
    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
}
EchoGras "*** Sauvegarde"
TestBareosDaemon "Bareos Director" dir "$(CreoleGet activer_bareos_dir)"
TestBareosDaemon "Bareos Client" fd "$(CreoleGet activer_bareos_dir)"
TestBareosDaemon "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 [ "$(CreoleGet activer_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
echo
fi
exit 0
