public function OnStartForking() { $db = \Scalr::getDb(); // Clear old instances log $oldlogtime = mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 10, date("Y")); $db->Execute("DELETE FROM logentries WHERE `time` < {$oldlogtime}"); print "DELETE FROM logentries WHERE `time` < {$oldlogtime}\n"; sleep(60); $oldlogtime = date("Y-m-d H:i:s", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 7, date("Y"))); $db->Execute("DELETE FROM scripting_log WHERE `dtadded` < '{$oldlogtime}'"); print "DELETE FROM scripting_log WHERE `dtadded` < '{$oldlogtime}'\n"; sleep(60); $oldlogtime = date("Y-m-d H:i:s", mktime(date("H"), date("i"), date("s"), date("m") - 2, date("d"), date("Y"))); $db->Execute("DELETE FROM events WHERE `dtadded` < '{$oldlogtime}'"); print "DELETE FROM events WHERE `dtadded` < '{$oldlogtime}'\n"; sleep(60); $oldlogtime = date("Y-m-d H:i:s", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 10, date("Y"))); $db->Execute("DELETE FROM messages WHERE type='out' AND status='1' AND `dtlasthandleattempt` < '{$oldlogtime}'"); print "m1\n"; sleep(60); $db->Execute("DELETE FROM messages WHERE type='out' AND status='3' AND `dtlasthandleattempt` < '{$oldlogtime}'"); print "m2\n"; sleep(60); $oldlogtime = date("Y-m-d H:i:s", mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 20, date("Y"))); $db->Execute("DELETE FROM messages WHERE type='in' AND status='1' AND `dtlasthandleattempt` < '{$oldlogtime}'"); print "m3\n"; sleep(60); print "SYSLOG\n"; //Clear old scripting events $year = date("Y"); $month = date("m", mktime(date("H"), date("i"), date("s"), date("m") - 1, date("d"), date("Y"))); $db->Execute("DELETE FROM `farm_role_scripts` WHERE ismenuitem='0' AND event_name LIKE 'CustomEvent-{$year}{$month}%'"); $db->Execute("DELETE FROM `farm_role_scripts` WHERE ismenuitem='0' AND event_name LIKE 'APIEvent-{$year}{$month}%'"); // Rotate syslog if ($db->GetOne("SELECT COUNT(*) FROM syslog") > 1000000) { $dtstamp = date("dmY"); $db->Execute("\n CREATE TABLE syslog_{$dtstamp} (\n id INT NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (id)\n ) ENGINE=MyISAM\n SELECT dtadded, message, severity, transactionid\n FROM syslog\n "); $db->Execute("TRUNCATE TABLE syslog"); $db->Execute("OPTIMIZE TABLE syslog"); $db->Execute("TRUNCATE TABLE syslog_metadata"); $db->Execute("OPTIMIZE TABLE syslog_metadata"); $this->Logger->debug("Log rotated. New table 'syslog_{$dtstamp}' created."); } //Rotate aws_statistics printf("%s - AWS Statistics rotate\n", date('H:i:s')); StatisticsPlugin::rotate(); printf("%s - OK\n", date('H:i:s')); }
/** * {@inheritdoc} * @see \Scalr\System\Pcntl\ProcessInterface::OnStartForking() */ public function OnStartForking() { $db = \Scalr::getDb(); $sevenDaysAgo = date('Y-m-d H:i:s', strtotime('-7 days')); $tenDaysAgo = date('Y-m-d H:i:s', strtotime('-10 days')); $twentyDaysAgo = date('Y-m-d H:i:s', strtotime('-20 days')); $monthAgo = date('Y-m-d H:i:s', strtotime('-1 months')); $twoMonthAgo = date('Y-m-d H:i:s', strtotime('-2 months')); $twoWeeksAgo = date('Y-m-d H:i:s', strtotime('-14 days')); $this->console->out("%s (UTC) Start RotateLogsProcess", gmdate('Y-m-d')); $this->console->out("Rotating logentries table"); $this->rotateTable("DELETE FROM `logentries` WHERE `time` < ?", [strtotime('-10 days')]); $this->console->out("Rotating scripting_log table"); $this->rotateTable("DELETE FROM `scripting_log` WHERE `dtadded` < ?", array($sevenDaysAgo)); $this->console->out("Rotating events table"); $this->rotateTable("DELETE FROM `events` WHERE `dtadded` < ?", array($twoMonthAgo)); $this->console->out("Rotating messages table"); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='1' AND `dtlasthandleattempt` < ?", array($tenDaysAgo)); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='3' AND `dtlasthandleattempt` < ?", array($tenDaysAgo)); $this->rotateTable("DELETE FROM messages WHERE type='in' AND status='1' AND `dtlasthandleattempt` < ?", array($tenDaysAgo)); $this->console->out('Rotating webhook_history table'); $this->rotateTable("DELETE FROM webhook_history WHERE `created` < ?", array($twoWeeksAgo)); $this->console->out("Rotating farm_role_scripts table"); $year = date('Y'); $month = date('m', strtotime('-1 months')); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'CustomEvent-{$year}{$month}%'\n "); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'APIEvent-{$year}{$month}%'\n "); $this->console->out('Calculating number of the records in the syslog table'); if ($db->GetOne("SELECT COUNT(*) FROM `syslog`") > 1000000) { $this->console->out("Rotating syslog table"); $dtstamp = date("HdmY"); try { if ($db->GetOne("SHOW TABLES LIKE ?", ['syslog_tmp'])) { $db->Execute("DROP TABLE `syslog_tmp`"); } $db->Execute("CREATE TABLE `syslog_tmp` LIKE `syslog`"); $db->Execute("RENAME TABLE `syslog` TO `syslog_" . $dtstamp . "`, `syslog_tmp` TO `syslog`"); $db->Execute("TRUNCATE TABLE syslog_metadata"); $db->Execute("OPTIMIZE TABLE syslog"); $db->Execute("OPTIMIZE TABLE syslog_metadata"); } catch (Exception $e) { $this->console->error($e->getMessage()); } $this->Logger->debug("Log rotated. New table 'syslog_{$dtstamp}' created."); $this->rotateBackup('^syslog_[0-9]{8,10}$'); } //Rotate aws_statistics $this->console->out("Rotating AWS Statistics"); StatisticsPlugin::rotate(); //Rotate cost analytics data if (Scalr::getContainer()->analytics->enabled) { $this->console->out("Rotating analytics.poller_sessions table"); $before = (new DateTime('-7 days', new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `poller_sessions` WHERE `dtime` < ?", [$before], 'cadb'); $this->console->out("Rotating analytics.usage_h table"); $before = (new DateTime('-14 days', new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `usage_h` WHERE `dtime` < ?", [$before], 'cadb'); $this->console->out("Rotating analytics.nm_usage_h table"); $this->rotateTable("DELETE FROM `nm_usage_h` WHERE `dtime` < ?", [$before], 'cadb'); } $this->console->out('Done'); }
/** * {@inheritdoc} * @see \Scalr\System\Zmq\Cron\TaskInterface::enqueue() */ public function enqueue() { $db = \Scalr::getDb(); $keep = $this->config()->keep; if ($db->GetOne("SHOW TABLES LIKE 'api_counters'")) { $this->getLogger()->info("Rotating api rate limit counters"); $db->Execute("TRUNCATE TABLE `api_counters`"); } $this->getLogger()->info("Rotating logentries table. Keep:'%s'", $keep['scalr']['logentries']); $this->rotateTable("DELETE FROM `logentries` WHERE `time` < ?", [strtotime($keep['scalr']['logentries'])]); $this->getLogger()->info("Rotating scripting_log table. Keep:'%s'", $keep['scalr']['scripting_log']); $this->rotateTable("DELETE FROM `scripting_log` WHERE `dtadded` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['scripting_log']))]); $this->getLogger()->info("Rotating api_log table. Keep:'%s'", $keep['scalr']['api_log']); $this->rotateTable("DELETE FROM `api_log` WHERE `dtadded` < ?", [strtotime($keep['scalr']['api_log'])]); $this->getLogger()->info("Rotating events table. Keep:'%s'", $keep['scalr']['events']); $this->rotateTable("DELETE FROM `events` WHERE `dtadded` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['events']))]); $this->getLogger()->info("Rotating messages table. Keep:'%s'", $keep['scalr']['messages']); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='1' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='3' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->rotateTable("DELETE FROM messages WHERE type='in' AND status='1' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->getLogger()->info("Rotating webhook_history table. Keep:'%s'", $keep['scalr']['webhook_history']); $this->rotateTable("DELETE FROM webhook_history WHERE `created` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['webhook_history']))]); $this->getLogger()->info("Rotating ui_errors table"); $this->rotateTable("DELETE FROM ui_errors WHERE `tm` < ?", [date('Y-m-d H:i:s', strtotime('-1 day'))]); $this->getLogger()->info("Rotating farm_role_scripts table"); $year = date('Y'); $month = date('m', strtotime('-1 months')); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'CustomEvent-{$year}{$month}%'\n "); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'APIEvent-{$year}{$month}%'\n "); $this->getLogger()->info('Calculating number of the records in the syslog table'); if ($db->GetOne("SELECT COUNT(*) FROM `syslog`") > $keep['scalr']['syslog']) { $this->getLogger()->info("Rotating syslog table. Keep:'%d'", $keep['scalr']['syslog']); $dtstamp = date("HdmY"); try { if ($db->GetOne("SHOW TABLES LIKE ?", ['syslog_tmp'])) { $db->Execute("DROP TABLE `syslog_tmp`"); } $db->Execute("CREATE TABLE `syslog_tmp` LIKE `syslog`"); $db->Execute("RENAME TABLE `syslog` TO `syslog_" . $dtstamp . "`, `syslog_tmp` TO `syslog`"); $db->Execute("TRUNCATE TABLE syslog_metadata"); $db->Execute("OPTIMIZE TABLE syslog"); $db->Execute("OPTIMIZE TABLE syslog_metadata"); } catch (Exception $e) { $this->console->error($e->getMessage()); } $this->getLogger()->debug("Log rotated. New table 'syslog_{$dtstamp}' created."); $this->rotateBackup('^syslog_[0-9]{8,10}$'); } //Rotate aws_statistics $this->getLogger()->info("Rotating AWS Statistics"); StatisticsPlugin::rotate(); //Rotate cost analytics data if (\Scalr::getContainer()->analytics->enabled) { $this->getLogger()->info("Rotating analytics.poller_sessions table. Keep:'%s'", $keep['analytics']['poller_sessions']); $before = (new DateTime($keep['analytics']['poller_sessions'], new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `poller_sessions` WHERE `dtime` < ?", [$before], 'cadb'); $this->getLogger()->info("Rotating analytics.usage_h table. Keep:'%s'", $keep['analytics']['usage_h']); $before = (new DateTime($keep['analytics']['usage_h'], new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `usage_h` WHERE `dtime` < ?", [$before], 'cadb'); $this->getLogger()->info("Rotating analytics.nm_usage_h table"); $this->rotateTable("DELETE FROM `nm_usage_h` WHERE `dtime` < ?", [$before], 'cadb'); $this->getLogger()->info("Rotating analytics.aws_billing_records table. Keep:'%s'", $keep['analytics']['aws_billing_records']); $before = (new DateTime($keep['analytics']['aws_billing_records'], new DateTimeZone('UTC')))->format('Y-m-d'); $this->rotateTable("DELETE FROM `aws_billing_records` WHERE `date` < ?", [$before], 'cadb'); } $this->getLogger()->info("Update bundle_tasks table. Fail for 3 days expired tasks."); $affected = BundleTask::failObsoleteTasks(); $this->getLogger()->info("%d task%s %s failed by timeout", $affected, $affected != 1 ? 's' : '', $affected > 1 ? 'were' : 'was'); if (\Scalr::config('scalr.auth_mode') == 'scalr') { // suspend user based on config settings $days = (int) \Scalr::config('scalr.security.user.suspension.inactivity_days'); if ($days > 0) { $dt = date('Y-m-d H:i:s', strtotime("-{$days} day")); $db->Execute("\n UPDATE `account_users`\n SET `status` = ?\n WHERE `email` != 'admin' AND (\n `dtlastlogin` IS NOT NULL AND `dtlastlogin` < ? OR `dtlastlogin` IS NULL AND `dtcreated` < ?\n )\n ", [User::STATUS_INACTIVE, $dt, $dt]); $affected = $db->Affected_Rows(); if ($affected > 0) { $this->getLogger()->info("%d %s suspended due to inactivity", $affected, $affected > 1 ? 'users were' : 'user was'); } } } $this->getLogger()->info('Done'); //It does not need to handle a work because all stuff is handled in the client. return new ArrayObject([]); }
/** * {@inheritdoc} * @see \Scalr\System\Zmq\Cron\TaskInterface::enqueue() */ public function enqueue() { $db = \Scalr::getDb(); $keep = $this->config()->keep; $this->getLogger()->info("Rotating logentries table. Keep:'%s'", $keep['scalr']['logentries']); $this->rotateTable("DELETE FROM `logentries` WHERE `time` < ?", [strtotime($keep['scalr']['logentries'])]); $this->getLogger()->info("Rotating scripting_log table. Keep:'%s'", $keep['scalr']['scripting_log']); $this->rotateTable("DELETE FROM `scripting_log` WHERE `dtadded` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['scripting_log']))]); $this->getLogger()->info("Rotating events table. Keep:'%s'", $keep['scalr']['events']); $this->rotateTable("DELETE FROM `events` WHERE `dtadded` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['events']))]); $this->getLogger()->info("Rotating messages table. Keep:'%s'", $keep['scalr']['messages']); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='1' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->rotateTable("DELETE FROM messages WHERE type='out' AND status='3' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->rotateTable("DELETE FROM messages WHERE type='in' AND status='1' AND `dtlasthandleattempt` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['messages']))]); $this->getLogger()->info("Rotating webhook_history table. Keep:'%s'", $keep['scalr']['webhook_history']); $this->rotateTable("DELETE FROM webhook_history WHERE `created` < ?", [date('Y-m-d H:i:s', strtotime($keep['scalr']['webhook_history']))]); $this->getLogger()->info("Rotating farm_role_scripts table"); $year = date('Y'); $month = date('m', strtotime('-1 months')); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'CustomEvent-{$year}{$month}%'\n "); $this->rotateTable("\n DELETE FROM `farm_role_scripts`\n WHERE ismenuitem='0' AND event_name LIKE 'APIEvent-{$year}{$month}%'\n "); $this->getLogger()->info('Calculating number of the records in the syslog table'); if ($db->GetOne("SELECT COUNT(*) FROM `syslog`") > $keep['scalr']['syslog']) { $this->getLogger()->info("Rotating syslog table. Keep:'%d'", $keep['scalr']['syslog']); $dtstamp = date("HdmY"); try { if ($db->GetOne("SHOW TABLES LIKE ?", ['syslog_tmp'])) { $db->Execute("DROP TABLE `syslog_tmp`"); } $db->Execute("CREATE TABLE `syslog_tmp` LIKE `syslog`"); $db->Execute("RENAME TABLE `syslog` TO `syslog_" . $dtstamp . "`, `syslog_tmp` TO `syslog`"); $db->Execute("TRUNCATE TABLE syslog_metadata"); $db->Execute("OPTIMIZE TABLE syslog"); $db->Execute("OPTIMIZE TABLE syslog_metadata"); } catch (Exception $e) { $this->console->error($e->getMessage()); } $this->getLogger()->debug("Log rotated. New table 'syslog_{$dtstamp}' created."); $this->rotateBackup('^syslog_[0-9]{8,10}$'); } //Rotate aws_statistics $this->getLogger()->info("Rotating AWS Statistics"); StatisticsPlugin::rotate(); //Rotate cost analytics data if (\Scalr::getContainer()->analytics->enabled) { $this->getLogger()->info("Rotating analytics.poller_sessions table. Keep:'%s'", $keep['analytics']['poller_sessions']); $before = (new DateTime($keep['analytics']['poller_sessions'], new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `poller_sessions` WHERE `dtime` < ?", [$before], 'cadb'); $this->getLogger()->info("Rotating analytics.usage_h table. Keep:'%s'", $keep['analytics']['usage_h']); $before = (new DateTime($keep['analytics']['usage_h'], new DateTimeZone('UTC')))->format('Y-m-d H:i:s'); $this->rotateTable("DELETE FROM `usage_h` WHERE `dtime` < ?", [$before], 'cadb'); $this->getLogger()->info("Rotating analytics.nm_usage_h table"); $this->rotateTable("DELETE FROM `nm_usage_h` WHERE `dtime` < ?", [$before], 'cadb'); } $this->getLogger()->info("Update bundle_tasks table. Fail for 3 days expired tasks."); $affected = BundleTask::failObsoleteTasks(); $this->getLogger()->info("%d task%s %s failed by timeout", $affected, $affected != 1 ? 's' : '', $affected > 1 ? 'were' : 'was'); $this->getLogger()->info('Done'); //It does not need to handle a work because all stuff is handled in the client. return new ArrayObject([]); }