#!/bin/sh
errors=0

#
# NOTE: We don't rely on system to run logrotate for us on purpose. By
# default system invokes logrotate on daily basis, but that is not good
# enough for us. VDSM can generate large amount of logs during peek periods. 
# Therefore we prefer to run logrotate manually on a *hourly* basis.
#
# See: https://bugzilla.redhat.com/1113264
#
/usr/sbin/logrotate /etc/vdsm/logrotate/vdsm

EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t vdsm-logrotate "ALERT logrotate exited abnormally with [$EXITVALUE]"
    errors=$((errors+1))
fi

if [ -d /var/log/vdsm/import ] ; then
    /usr/bin/find /var/log/vdsm/import -type f -mtime +30 -exec /bin/rm -f '{}' \;
    EXITVALUE=$?
    if [ $EXITVALUE != 0 ]; then
        /usr/bin/logger -t vdsm-logrotate "ALERT clean of old import log files exited abnormally with [$EXITVALUE]"
        errors=$((errors+1))
    fi
fi

if [ $errors -ne 0 ]; then
    exit 1
fi

exit 0
