Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel Berteaud
a50f78ccd5 Update spec file 2012-11-05 11:44:16 +01:00
Daniel Berteaud
4bb3e2c8b0 Don't rotate tables, just remove old entries
+ Fix parsing lines when day < 10
2012-11-05 11:41:40 +01:00
4 changed files with 20 additions and 56 deletions

View File

@ -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

View File

@ -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

View File

@ -97,7 +97,7 @@ while (defined(my $line=$tail->read)){
$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
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;
$client_ip = $2;
$client_name = $3;

View File

@ -1,4 +1,4 @@
%define version 0.1.2
%define version 0.1.3
%define release 1
%define name smeserver-samba-db-logd
@ -23,6 +23,10 @@ Requires: perl(DBI)
Log samba events in a MySQL database
%changelog
* Mon Nov 5 2012 Daniel Berteaud <daniel@firewall-services.com> 0.1.3-1
- Don't rotate tables, just remove old entries
- Fix line parsing when day < 10
* Fri Oct 19 2012 Daniel Berteaud <daniel@firewall-services.com> 0.1.2-1
- Skip opendir lines as they are flooding logs
- Support more actions in log parser
@ -52,7 +56,7 @@ perl createlinks
--file /usr/bin/samba-db-logd 'attr(0755,root,root)' \
--file /var/service/samba-db-logd/run 'attr(0755,root,root)' \
--file /var/service/samba-db-logd/log/run 'attr(0755,root,root)' \
--file /etc/cron.monthly/samba-log-rotate 'attr(0755,root,root)' \
--file /etc/cron.daily/samba-db-cleanup 'attr(0755,root,root)' \
> %{name}-%{version}-filelist
echo "%doc CHANGELOG.git" >> %{name}-%{version}-filelist