Maintenance update for automake and PHP.
[gnucomo.git] / src / gcm_daemon / gcm_maintenance.php
index 0bbb56a..a672266 100755 (executable)
                    Andromeda Technology & Automation
 
  $Log: gcm_maintenance.php,v $
- Revision 1.1  2007-12-12 09:06:21  arjen
+ Revision 1.2  2011-03-24 09:49:20  arjen
+ Cleanup abuse records only for subnets smaller than /16.
+
+ Revision 1.1  2007/12/12 09:06:21  arjen
  Added a new script gcm_maintenance.php to cleanup the database
  and check referential integrity. Purging old log entries is
  removed from the gcm_daemon script.
 
 */
 
-// $Id: gcm_maintenance.php,v 1.1 2007-12-12 09:06:21 arjen Exp $
+// $Id: gcm_maintenance.php,v 1.2 2011-03-24 09:49:20 arjen Exp $
 
 ini_set('include_path', '.:./classes:../phpclasses');
 ini_set('html_errors', 'false');
 
 define("BATCHSIZE", 10000);
 
-//Tell the log that we're up.
-define_syslog_variables();
-
 require_once "gnucomo_config.php";
 require_once "db.class.php";
 
@@ -100,43 +100,46 @@ if ($dbms->have_db_connection() == "FALSE")
    {
       purge_old_logs($purge_date);
    }
+   else
+   {
 
-   // Check the references from the abuse list to the logs
-
+      // Check the references from the abuse list to the logs
 
-   $abuse_result = $dbms->query("select source, nr_abuses, masklen(source) from object_abuse
-                                 where status='dropped' and masklen(source)=32 and objectid=1 order by source");
-   for ($ab = 0; $ab < $dbms->num_rows($abuse_result); $ab++)
-   {
-      $logentries = 0;
-      $abuse = $dbms->fetch_object($abuse_result, $ab);
-      $nr_abuses = $abuse->nr_abuses;
-      $log_res = $dbms->query("select logid from log_abuse where source='" . $abuse->source . "' and objectid=1");
 
-      if ($dbms->num_rows($log_res) != $nr_abuses)
-      {
-         echo "Mismatch in nr of abuses. Corrected.\n";
-         $dbms->query("update object_abuse set nr_abuses=" . $dbms->num_rows($log_res) .
-                      " where source='" . $abuse->source . "' and objectid=1");
-      }
-      if ($dbms->num_rows($log_res) == 0)
+      $abuse_result = $dbms->query("select source, nr_abuses, masklen(source) from object_abuse
+                                 where (status='dropped' or status='') and masklen(source)>16 and objectid=11 order by source");
+      for ($ab = 0; $ab < $dbms->num_rows($abuse_result); $ab++)
       {
-         echo "  Removing ", $abuse->source, "\n";
-         $dbms->query("delete from object_abuse where source='". $abuse->source . "' and objectid=1");
-      }
-      else
-      {
-         $logref_res = $dbms->query("select logid from log where logid in
-                                    (select logid from log_abuse where source='" . $abuse->source . "' and objectid=1)");
-         $logentries = $dbms->num_rows($logref_res);
-         if ($logentries == 0)
+         $logentries = 0;
+         $abuse = $dbms->fetch_object($abuse_result, $ab);
+         $nr_abuses = $abuse->nr_abuses;
+         $log_res = $dbms->query("select logid from log_abuse where source='" . $abuse->source . "' and objectid=11");
+
+         if ($dbms->num_rows($log_res) != $nr_abuses)
+         {
+            echo "Mismatch in nr of abuses. Corrected.\n";
+            $dbms->query("update object_abuse set nr_abuses=" . $dbms->num_rows($log_res) .
+                         " where source='" . $abuse->source . "' and objectid=11");
+         }
+         if ($dbms->num_rows($log_res) == 0)
+         {
+            echo "  Removing ", $abuse->source, "\n";
+            $dbms->query("delete from object_abuse where source='". $abuse->source . "' and objectid=11");
+         }
+         else
          {
-            echo "All references to log entries are lost. Purging log_abuse table.\n";
-            $dbms->query("delete from log_abuse where source='" . $abuse->source . "' and objectid=1");
+            $logref_res = $dbms->query("select logid from log where logid in
+                                    (select logid from log_abuse where source='" . $abuse->source . "' and objectid=11)");
+            $logentries = $dbms->num_rows($logref_res);
+            if ($logentries == 0)
+            {
+               echo "All references to log entries are lost. Purging log_abuse table.\n";
+               $dbms->query("delete from log_abuse where source='" . $abuse->source . "' and objectid=11");
             
+            }
          }
+         echo $abuse->source . "\t$nr_abuses\t" . $dbms->num_rows($log_res) . "\t" . $logentries . "\n";
       }
-      echo $abuse->source . "\t$nr_abuses\t" . $dbms->num_rows($log_res) . "\t" . $logentries . "\n";
    }
 
    //  Gather the statistics for each object
@@ -195,7 +198,7 @@ function purge_old_logs($purge_date)
 
    //  Clean up abuses that are left without reference to the log.
 
-   $r = $dbms->query("select source from log_abuse where logid in
+   $r = $dbms->query("select source from log_abuse where masklen(source) > 16 and logid in
                          (select logid from gcm_deamon_old_log) group by source");
    echo "Abusing IP addresses that may be affected:\n";
    $abusers = array();
@@ -215,7 +218,7 @@ function purge_old_logs($purge_date)
       if ($c->count == 0)
       {
          echo "Cleaning up abusing address $src.\n";
-         $dbms->query("delete from object_abuse where source='$src'");
+         $dbms->query("delete from object_abuse where source='$src' and (status='' or status='dropped')");
       }
    }