Don't rotate tables, just remove old entries

+ Fix parsing lines when day < 10
master
Daniel Berteaud 12 years ago
parent ed3813ad51
commit 4bb3e2c8b0
  1. 13
      root/etc/cron.daily/samba-db-cleanup
  2. 53
      root/etc/cron.monthly/samba-log-rotate
  3. 2
      root/usr/bin/samba-db-logd

@ -0,0 +1,13 @@
#!/bin/bash
LOG=$(/sbin/e-smith/db configuration getprop samba-db-logd status || echo 'disabled')
if [ "$LOG" != "enabled" ]; then
exit 0
fi
RETENTION=$(/sbin/e-smith/db configuration getprop samba-db-logd Retention || echo 370)
DB=$(/sbin/e-smith/db configuration getprop samba-db-logd DbName || echo samba_log)
echo "delete from audit where date_day<DATE_SUB(NOW(), INTERVAL $RETENTION DAY);" | /usr/bin/mysql $DB

@ -1,53 +0,0 @@
#!/bin/bash
DB_HOST=$(/sbin/e-smith/db configuration getprop samba-db-logd DbHost || echo localhost)
RETENTION=$(/sbin/e-smith/db configuration getprop samba-db-logd Retention || echo 365)
SQL_DB=$(/sbin/e-smith/db configuration getprop samba-db-logd DbName || echo 'samba_log')
TABNAME="audit"
SQLCMD="mysql ${SQL_DB} --batch";
MONTH=$(date +%m)
YEAR=$(date +%Y)
# We rotate on the first day of a new month
if [ "$MONTH" == "1" ]; then
MONTH=12
else
MONTH=$(($MONTH-1))
fi
# Pad with 0
MONTH=$(printf "%02d" $MONTH)
DATE=$MONTH"_"$YEAR
for T in ${TABNAME}; do
# create table 0
echo "CREATE TABLE IF NOT EXISTS ${T}_0 LIKE ${T};" | $SQLCMD;
# Rotate table
echo "FLUSH TABLES ${T}; RENAME TABLE ${T} TO ${T}_$DATE; RENAME TABLE ${T}_0 TO ${T}" | ${SQLCMD} >/dev/null 2>&1
# Drop _0 table if we rotate more than two times a month
if echo "DESCRIBE ${T}_0;" | ${SQLCMD} >/dev/null 2>&1; then
echo "DROP TABLE ${T}_0;" | $SQLCMD
fi
#compress 2
cd /var/lib/mysql/${SQL_DB}/
echo "FLUSH TABLE ${T}_${DATE};" | $SQLCMD
myisampack -s "${T}_${DATE}.MYI"
myisamchk -s -rq --sort-index --analyze "${T}_${DATE}.MYI"
echo "FLUSH TABLE ${T}_${DATE}" | $SQLCMD
done
# Now check existing table to drop olds ones
for T in $(echo "show tables" | $SQLCMD | grep -v -P "^Tables_in_"$SQL_DB | grep -v -P "^audit$"); do
TMONTH=$(echo $T | perl -pe 'm/^audit_(\d+)_(\d+)/; print $1;exit')
TYEAR=$(echo $T | perl -pe 'm/^audit_(\d+)_(\d+)/; print $2;exit')
# Drop table if older than configured retention
if [ "$(($(date -d "01/$MONTH/$YEAR" +%s)-$(date -d "01/$TMONTH/$TYEAR" +%s)))" -gt "$((24*3600*$RETENTION))" ]; then
echo "DROP TABLE $T;" | $SQLCMD
fi
done

@ -97,7 +97,7 @@ while (defined(my $line=$tail->read)){
$action, $status, $access_mode, $file_src, $file_dst) = undef; $action, $status, $access_mode, $file_src, $file_dst) = undef;
# Oct 12 17:20:24 sme8 smbd[11176]: admin|192.168.7.50|pc10-45|intranet|mkdir|Nouveau dossier # Oct 12 17:20:24 sme8 smbd[11176]: admin|192.168.7.50|pc10-45|intranet|mkdir|Nouveau dossier
if ($line =~ m/^\w+\s\d+\s\d+:\d+:\d+\s\w+\ssmbd\[\d+\]:\s+(\w+)\|(\d+\.\d+\.\d+\.\d+)\|([\w\.\-]+)\|(\w+)\|(\w+)/){ if ($line =~ m/^\w+\s+\d+\s\d+:\d+:\d+\s\w+\ssmbd\[\d+\]:\s+(\w+)\|(\d+\.\d+\.\d+\.\d+)\|([\w\.\-]+)\|(\w+)\|(\w+)/){
$username = $1; $username = $1;
$client_ip = $2; $client_ip = $2;
$client_name = $3; $client_name = $3;

Loading…
Cancel
Save