diff options
Diffstat (limited to 'mod/logrotate')
-rw-r--r-- | mod/logrotate/languages/en.php | 28 | ||||
-rw-r--r-- | mod/logrotate/manifest.xml | 17 | ||||
-rw-r--r-- | mod/logrotate/start.php | 121 | ||||
-rw-r--r-- | mod/logrotate/views/default/plugins/logrotate/settings.php | 47 |
4 files changed, 213 insertions, 0 deletions
diff --git a/mod/logrotate/languages/en.php b/mod/logrotate/languages/en.php new file mode 100644 index 000000000..27731d732 --- /dev/null +++ b/mod/logrotate/languages/en.php @@ -0,0 +1,28 @@ +<?php +/** + * Elgg log rotator language pack. + * + * @package ElggLogRotate + */ + +$english = array( + 'logrotate:period' => 'How often should the system log be archived?', + + 'logrotate:weekly' => 'Once a week', + 'logrotate:monthly' => 'Once a month', + 'logrotate:yearly' => 'Once a year', + + 'logrotate:logrotated' => "Log rotated\n", + 'logrotate:lognotrotated' => "Error rotating log\n", + + 'logrotate:delete' => 'Delete archived logs older than a', + + 'logrotate:week' => 'week', + 'logrotate:month' => 'month', + 'logrotate:year' => 'year', + + 'logrotate:logdeleted' => "Log deleted\n", + 'logrotate:lognotdeleted' => "Error deleting log\n", +); + +add_translation("en", $english); diff --git a/mod/logrotate/manifest.xml b/mod/logrotate/manifest.xml new file mode 100644 index 000000000..1aa62e2e7 --- /dev/null +++ b/mod/logrotate/manifest.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8"> + <name>Log Rotate</name> + <author>Core developers</author> + <version>1.5</version> + <category>bundled</category> + <category>admin</category> + <description>Rotate the system log at specific intervals</description> + <website>http://www.elgg.org/</website> + <copyright>See COPYRIGHT.txt</copyright> + <license>GNU General Public License version 2</license> + <requires> + <type>elgg_release</type> + <version>1.8</version> + </requires> + <activate_on_install>true</activate_on_install> +</plugin_manifest> diff --git a/mod/logrotate/start.php b/mod/logrotate/start.php new file mode 100644 index 000000000..28f14ad14 --- /dev/null +++ b/mod/logrotate/start.php @@ -0,0 +1,121 @@ +<?php +/** + * Elgg log rotator. + * + * @package ElggLogRotate + */ + +elgg_register_event_handler('init', 'system', 'logrotate_init'); + +function logrotate_init() { + $period = elgg_get_plugin_setting('period', 'logrotate'); + $delete = elgg_get_plugin_setting('delete', 'logrotate'); + switch ($period) { + case 'weekly': + case 'monthly' : + case 'yearly' : + break; + default: + $period = 'monthly'; + } + + // Register cron hook for archival of logs + elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron'); + // Register cron hook for deletion of selected archived logs + elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron'); +} + +/** + * Trigger the log rotation. + */ +function logrotate_archive_cron($hook, $entity_type, $returnvalue, $params) { + $resulttext = elgg_echo("logrotate:logrotated"); + + $day = 86400; + + $offset = 0; + $period = elgg_get_plugin_setting('period', 'logrotate'); + switch ($period) { + case 'weekly': + $offset = $day * 7; + break; + case 'yearly': + $offset = $day * 365; + break; + case 'monthly': + default: + // assume 28 days even if a month is longer. Won't cause data loss. + $offset = $day * 28; + } + + if (!archive_log($offset)) { + $resulttext = elgg_echo("logrotate:lognotrotated"); + } + + return $returnvalue . $resulttext; +} + +/** + * Trigger the log deletion. + */ +function logrotate_delete_cron($hook, $entity_type, $returnvalue, $params) { + $resulttext = elgg_echo("logrotate:logdeleted"); + + $day = 86400; + + $offset = 0; + $period = elgg_get_plugin_setting('delete', 'logrotate'); + switch ($period) { + case 'weekly': + $offset = $day * 7; + break; + case 'yearly': + $offset = $day * 365; + break; + case 'monthly': + default: + // assume 28 days even if a month is longer. Won't cause data loss. + $offset = $day * 28; + } + + if (!log_browser_delete_log($offset)) { + $resulttext = elgg_echo("logrotate:lognotdeleted"); + } + + return $returnvalue . $resulttext; +} + +/** + * This function deletes archived copies of the system logs that are older than specified. + * + * @param int $time_of_delete An offset in seconds from now to delete (useful for log deletion) + */ +function log_browser_delete_log($time_of_delete) { + global $CONFIG; + + $offset = (int)$time_of_delete; + $now = time(); + + $ts = $now - $offset; + + $FLAG = 1; + $result = mysql_query("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'"); + while ($showtablerow = mysql_fetch_array($result)) { + //To obtain time of archival + $log_time = explode("{$CONFIG->dbprefix}system_log_", $showtablerow[0]); + if ($log_time < $ts) { + //If the time of archival is before the required offset then delete + if (!mysql_query("DROP TABLE $showtablerow[0]")) { + $FLAG = 0; + } + } + } + + //Check if the appropriate tables have been deleted and return true if yes + if ($FLAG) { + return true; + } else { + return false; + } + +} diff --git a/mod/logrotate/views/default/plugins/logrotate/settings.php b/mod/logrotate/views/default/plugins/logrotate/settings.php new file mode 100644 index 000000000..bef8b308d --- /dev/null +++ b/mod/logrotate/views/default/plugins/logrotate/settings.php @@ -0,0 +1,47 @@ +<?php +/** + * Elgg log rotator plugin settings. + * + * @package ElggLogRotate + */ + +$period = $vars['entity']->period; +$delete = $vars['entity']->delete; +if (!$period) { + $period = 'monthly'; +} + +if (!$delete) { + $delete = 'monthly'; +} +?> +<div> + <?php + + echo elgg_echo('logrotate:period') . ' '; + echo elgg_view('input/dropdown', array( + 'name' => 'params[period]', + 'options_values' => array( + 'weekly' => elgg_echo('logrotate:weekly'), + 'monthly' => elgg_echo('logrotate:monthly'), + 'yearly' => elgg_echo('logrotate:yearly'), + ), + 'value' => $period, + )); + ?> +</div> +<div> + <?php + + echo elgg_echo('logrotate:delete') . ' '; + echo elgg_view('input/dropdown', array( + 'name' => 'params[delete]', + 'options_values' => array( + 'weekly' => elgg_echo('logrotate:week'), + 'monthly' => elgg_echo('logrotate:month'), + 'yearly' => elgg_echo('logrotate:year'), + ), + 'value' => $delete, + )); + ?> +</div> |