#!/bin/bash

. /usr/lib/eole/diagnose.sh

# Éviter les affichages des messages d'erreur de CreoleGet
activer_log_distant=$(CreoleGet activer_log_distant 2>/dev/null)
rsyslog_envoi_tls=$(CreoleGet rsyslog_envoi_tls 2>/dev/null)
rsyslog_reception_tls=$(CreoleGet rsyslog_reception_tls 2>/dev/null)
activer_reception_logs=$(CreoleGet activer_reception_logs 2>/dev/null)
activer_envoi_logs=$(CreoleGet activer_envoi_logs 2>/dev/null)

# Test des connexions
if [ "$activer_log_distant" = "oui" ]; then

    EchoGras "*** Journalisation à distance"
    if [ "$rsyslog_reception_tls" = "oui" ]; then

        rsyslog_ca_file=$(CreoleGet rsyslog_ca_file)
        rsyslog_cert_file=$(CreoleGet rsyslog_cert_file)
        purpose="sslserver"

        # Test de la validité de la chaîne de certification
        printf ". %${len_pf}s => " "Statut de TLS"
        EchoVert "Activé"

        printf ". %${len_pf}s => " "Test de validité de la chaîne de certification"
        openssl verify -purpose $purpose -CAfile $rsyslog_ca_file $rsyslog_cert_file > /dev/null
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    elif [ ! "$rsyslog_envoi_tls" = "oui" ]; then
        Inactif "Statut de TLS"
    fi

# Test des ports
printf ". %${len_pf}s => " "Test de la configuration de réception"
if [ "$activer_reception_logs" = "oui" ]; then

    activer_reception_logs_relp=$(CreoleGet activer_reception_logs_relp 2>/dev/null)
    activer_reception_logs_tcp=$(CreoleGet activer_reception_logs_tcp 2>/dev/null)
    activer_reception_logs_udp=$(CreoleGet activer_reception_logs_udp 2>/dev/null)

    EchoVert "Activée"
    printf ". %${len_pf}s => " "Test de RELP"
    if [ "$activer_reception_logs_relp" = "oui" ]; then
        TestConnexion rsyslog tcp 20514 reception
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    else
        EchoOrange "Désactivé"
    fi
    printf ". %${len_pf}s => " "Test de TCP"
    if [ "$activer_reception_logs_tcp" = "oui" ]; then
        TestConnexion rsyslog tcp 10514 reception
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    else
        EchoOrange "Désactivé"
    fi
    printf ". %${len_pf}s => " "Test de UDP"
    if [ "$activer_reception_logs_udp" = "oui" ]; then
        TestConnexion rsyslog udp 514
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    else
        EchoOrange "Désactivé"
    fi
else
    EchoOrange "Désactivé"
fi

printf ". %${len_pf}s => " "Test de la configuration d'envoi"
if [ "$activer_envoi_logs" = "oui" ]; then
    EchoVert "Activée"
    if [ "$rsyslog_envoi_tls" = "oui" ]; then
        printf ". %${len_pf}s => " "Test de TCP"
        TestConnexion rsyslog tcp 10514 envoi
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    else
        printf ". %${len_pf}s => " "Test de RELP"
        TestConnexion rsyslog tcp 20514 envoi
        if [ $? -ne 0 ]; then
            EchoRouge "Erreur"
        else
            EchoVert "OK"
        fi
    fi
else
    EchoOrange "Désactivé"
fi
echo
fi

EchoGras "*** Logrotate (fichiers pris en charge par rsyslog)"
LOGROTATED_DIR=""
for DIR in `cat /etc/logrotate.d/* | sed -e '/^\/.*/ !d' | sed -e 's/{//'`; do
    LOGROTATED_DIR="$LOGROTATED_DIR `ls $DIR 2>/dev/null`"

done
NB_LOGS=0
NOT_ROTATED=""
for RSYSLOG_DIR in `find /var/log/rsyslog -type f -name "*.log"`; do
	if [[ ! "$LOGROTATED_DIR" =~ $RSYSLOG_DIR ]]; then
		NB_LOGS=$(( $NB_LOGS + 1 ))
		NOT_ROTATED="$NOT_ROTATED\n$RSYSLOG_DIR"
	fi
done
echo "Fichiers non pris en charge par logrotate : $NB_LOGS"
if [ "$Verbose" != "yes" ]
    then
        echo
else
	echo -e $NOT_ROTATED
	echo
fi
if [[ -e /etc/logrotate.d/generated_remote_rules  ]] || [[ -e /etc/logrotate.d/generated_local_rules ]]; then
	echo "Filtres automatiques (/etc/logrotate.d/generated_{remote,local}_rules) : " `cat /etc/logrotate.d/generated_*_rules 2>/dev/null | sed -e '/^\/var\/log\/rsyslog/ !d' |wc -l`
	echo
fi
exit 0
