Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adzhurinskij/0da98b388c6793487659b65c4cd2216e to your computer and use it in GitHub Desktop.
Save adzhurinskij/0da98b388c6793487659b65c4cd2216e to your computer and use it in GitHub Desktop.
Rotation script for freeradius accounting detail files. Shall be run as a cron script.
#! /bin/sh
########################################################################################
# Compresses old radacct detail files and removes very old compressed radacct files.
########################################################################################
# Author: P. Tomulik
########################################################################################
# Path to the programs used (for environments without $PATH set)
FIND=/usr/bin/find
RM=/bin/rm
GZIP=/bin/gzip
# The directory where radacct detail files are created by freeradius.
RADACCTDIR="/var/log/freeradius/radacct"
# The following regular expression matches only the detail-* files created by radius.
# In particular, it doesn't match files with an extension (compressed files, or other products).
COMPRESSREG="^${RADACCTDIR}/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/detail-[0-9]+\$"
# Required age of the file in DAYS. Example: RADACCTAGE=29 -> only files modified 30 days ago or
# older are compressed by the script. Others are ignored.
COMPRESSAGE=29
for FILE in $($FIND "${RADACCTDIR}" -type f -regex "${COMPRESSREG}" -mtime "+${COMPRESSAGE}"); do
echo $GZIP "${FILE}";
done
# The following regular expression matches only the detail-*.gz files created by us.
# In particular, it doesn't match files without an extension.
DELETEREG="^${RADACCTDIR}/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/detail-[0-9]+\.gz\$"
# Required age of the file in DAYS. Example: DELETEAGE=59 -> only files created 60 days ago or
# older are processed by the script. Others are ignored.
DELETEAGE=59
# NOTE: Once it's tested we can simply do:
# $FIND "${RADACCTDIR}" -type f -regex "${DELETEREG}" -mtime "+${DELETEAGE}" -delete
# instead of the whole for loop below
for FILE in $($FIND "${RADACCTDIR}" -type f -regex "${DELETEREG}" -mtime "+${DELETEAGE}"); do
echo $RM -f "${FILE}";
done
#! /bin/sh
###############################################################################
# Compresses old radacct detail files and removes very old compressed radacct
# files. Instead of using mtime we use the creation date encoded in file name
# by radius daemon. Old files are not used by daemon, so we should be safe
# to manipulate them without harm.
###############################################################################
# Author: P. Tomulik
###############################################################################
###############################################################################
# is_detail_file_older(): tests whether a file age determined from its name is
# greater than the specified age. Example of an acceptable file name:
# detail-20150113
# This file is assumed to be created on 2015-01-13. The file name may contain
# optional non-numeric suffix (extension, e.g. detail-20150113.gz).
###############################################################################
# Usage example:
# if is_detail_file_older $FILE $DAYS 2> /dev/null; then
# # older;
# fi
#
# Arguments:
#
# $FILE: file name
# $DAYS: minimum age in days for which the function returns true
###############################################################################
is_detail_file_older() {
FILE=$1 # file name with encoded
DAYS=$2 # required age in days
TODAYDAT=$(date '+%Y%m%d'); # todays date
TODAYSEC=$(date --date="$TODAYDAT" "+%s"); # number of seconds since epoch
FILEDAT=$(basename ${FILE} | sed -e 's:^detail-\([0-9]\+\)[^0-9]*$:\1:');
FILESEC=$(date --date="$FILEDAT" "+%s")
SECDIFF=$(($TODAYSEC-$FILESEC));
SECSREQ=$(($DAYS*86400));
test $SECDIFF -gt $SECSREQ
}
# The directory where radacct detail files are created by freeradius.
RADACCTDIR="/var/log/freeradius/radacct"
# The following regular expression matches only the detail-* files created by
# radius. In particular, it doesn't match files with an extension (compressed
# files, or other products).
COMPRESSREG="${RADACCTDIR}/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/detail-[0-9]+"
# Required age of the file in DAYS. Example: COMPRESSAGE=30 -> only files that
# have 31 days or more are compressed by the script. Others are skipped.
COMPRESSAGE=30
for FILE in $(find "${RADACCTDIR}" -type f -regex "^${COMPRESSREG}\$"); do
# Date encoded in a file name
if is_detail_file_older ${FILE} ${COMPRESSAGE} 2> /dev/null; then
echo gzip ${FILE};
fi
done
# The following regular expression matches only the detail-*.gz files within
# radacct tree.
DELETEREG="${COMPRESSREG}\.gz"
# Required age of the file in DAYS. Example: DELETEAGE=120 -> only files that
# have 121 days or more are removed by the script. Others are skipped.
DELETEAGE=120
for FILE in $(find "${RADACCTDIR}" -type f -regex "^$DELETEREG\$"); do
if is_detail_file_older ${FILE} ${DELETEAGE} 2> /dev/null; then
echo rm -f ${FILE};
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment