From 3eecf0af78053b349c716279ae0ba00a2af5fdb0 Mon Sep 17 00:00:00 2001 From: Vincent Zanotti Date: Sun, 27 Sep 2009 19:13:08 +0200 Subject: [PATCH] Adds a monthly cron to remove unwanted logger table entries. In short: we won't keep more than 12 months of application logs for regular users. Signed-off-by: Vincent Zanotti --- bin/cron/compliance.php | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ configs/platal.cron.in | 3 ++ 2 files changed, 103 insertions(+) create mode 100755 bin/cron/compliance.php diff --git a/bin/cron/compliance.php b/bin/cron/compliance.php new file mode 100755 index 0000000..0a07a15 --- /dev/null +++ b/bin/cron/compliance.php @@ -0,0 +1,100 @@ +#!/usr/bin/php5 -q +fetchOneCell())) { + echo "Orphaned sessions: found $count orphaned sessions. Please fix that.\n"; + } +} + +/** + * Purges session events without a valid session. + */ +function purgeOrphanedEvents() { + XDB::execute( + "DELETE logger.e + FROM logger.events AS e + LEFT JOIN logger.sessions AS s ON (s.id = e.session) + WHERE s.id IS NULL"); + $affectedRows = XDB::affectedRows(); + echo "Orphaned events: removed $affectedRows events.\n"; +} + +// Remove expired sessions. +// For normal user, we only keep 12 months of data (and at least the last two sessions). +// For administrator, we also keep data for 12 months, but with a backlog of at least 20 sessions. +// For disabled users, we keep data for 5 years, and with a backlog of at least 2 sessions. +// For other users, no data are discarded. +discardExpiredSessions('user', 12, 2); +discardExpiredSessions('admin', 12, 20); +discardExpiredSessions('disabled', 60, 2); + +// Purge orphaned entries; events are purged automatically, sessions require explicit +// action from the administrator. +checkOrphanedSessions(); +purgeOrphanedEvents(); + +// Many rows have been removed from the two logger tables. Let's optimize them. +XDB::execute("OPTIMIZE TABLE logger.events"); +XDB::execute("OPTIMIZE TABLE logger.sessions"); + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/configs/platal.cron.in b/configs/platal.cron.in index a931d67..6923650 100644 --- a/configs/platal.cron.in +++ b/configs/platal.cron.in @@ -34,4 +34,7 @@ WD=/home/web/prod/platal/bin/cron # synchronisation google apps - plat/al */5 * * * * web cd $WD; ./google_apps.php +# Clean-up of our logging tables +45 0 1 * * web cd $WD; ./compliance.php | mail -e -s "Logging compliance" br@staff.m4x.org + # vim:set noet syntax=crontab ts=8 sw=8 sts=8 enc=utf-8: -- 2.1.4