clear_sasl_log.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. require_once "/web/inc/vars.inc.php";
  3. if (file_exists('/web/inc/vars.local.inc.php')) {
  4. include_once('/web/inc/vars.local.inc.php');
  5. }
  6. ini_set('error_reporting', 0);
  7. // Init database
  8. //$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  9. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  10. $opt = [
  11. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  12. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  13. PDO::ATTR_EMULATE_PREPARES => false,
  14. ];
  15. try {
  16. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  17. }
  18. catch (PDOException $e) {
  19. echo($e->getMessage() . PHP_EOL);
  20. exit;
  21. }
  22. try {
  23. $dateThreshold = new DateTime();
  24. $dateThreshold->modify('-31 days');
  25. $dateThresholdFormatted = $dateThreshold->format('Y-m-d H:i:s');
  26. $batchSize = 1000;
  27. $lastProcessedDatetime = null;
  28. $lastFetchedRows = 0;
  29. $loopCounter = 0;
  30. $rowCounter = 0;
  31. $clearedRowCounter = 0;
  32. do {
  33. $loopCounter++;
  34. echo("Processing batch $loopCounter\n");
  35. $stmt = $pdo->prepare("
  36. SELECT service, real_rip, username, datetime
  37. FROM sasl_log
  38. WHERE datetime < :dateThreshold
  39. AND (:lastProcessedDatetime IS NULL OR datetime >= :lastProcessedDatetime2)
  40. ORDER BY datetime ASC
  41. LIMIT :limit
  42. ");
  43. $stmt->execute(array(
  44. ':dateThreshold' => $dateThresholdFormatted,
  45. ':lastProcessedDatetime' => $lastProcessedDatetime,
  46. ':lastProcessedDatetime2' => $lastProcessedDatetime,
  47. ':limit' => $batchSize
  48. ));
  49. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  50. $rowCount = count($rows);
  51. $rowCounter += $rowCount;
  52. echo("Fetched $rowCount rows (total of $rowCounter)\n");
  53. foreach ($rows as $row) {
  54. $stmt = $pdo->prepare("
  55. SELECT MAX(datetime) as max_date
  56. FROM sasl_log
  57. WHERE datetime < :dateThreshold AND service = :service AND username = :username
  58. ");
  59. $stmt->execute(array(
  60. ':dateThreshold' => $dateThresholdFormatted,
  61. ':service' => $row['service'],
  62. ':username' => $row['username']
  63. ));
  64. $subrow = $stmt->fetch(PDO::FETCH_ASSOC);
  65. if ($row['datetime'] < $subrow['max_date']) {
  66. $stmt = $pdo->prepare("
  67. DELETE FROM sasl_log
  68. WHERE username = :username AND service = :service AND datetime = :datetime
  69. ");
  70. $stmt->execute(array(
  71. ':username' => $row['username'],
  72. ':service' => $row['service'],
  73. ':datetime' => $row['datetime']
  74. ));
  75. $clearedRowCounter++;
  76. }
  77. }
  78. if ($lastFetchedRows == $rowCount && $rowCount != $batchSize) {
  79. $rowCount = 0;
  80. }
  81. // Update last processed datetime
  82. if ($rowCount > 0) {
  83. $lastProcessedDatetime = $rows[$rowCount - 1]['datetime'];
  84. $lastFetchedRows = $rowCount;
  85. }
  86. } while ($rowCount > 0);
  87. }
  88. catch (PDOException $e) {
  89. echo($e->getMessage() . PHP_EOL);
  90. exit;
  91. }
  92. echo("Succesfully cleared $clearedRowCounter rows of $rowCounter rows");