Maintenance update for automake and PHP.
[gnucomo.git] / src / gcm_daemon / gcm_maintenance.php
1 #!/usr/bin/php
2 <?PHP
3 /**********************************************************************************
4 **  (c) Copyright 2002, Brenno J.S.A.A.F. de Winter, De Winter Information Solutions
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***********************************************************************************/
8
9
10 /*
11    NAME          : check_database
12    AUTHOR        : Arjen Baart
13                    Andromeda Technology & Automation
14
15  $Log: gcm_maintenance.php,v $
16  Revision 1.2  2011-03-24 09:49:20  arjen
17  Cleanup abuse records only for subnets smaller than /16.
18
19  Revision 1.1  2007/12/12 09:06:21  arjen
20  Added a new script gcm_maintenance.php to cleanup the database
21  and check referential integrity. Purging old log entries is
22  removed from the gcm_daemon script.
23
24
25 */
26
27 // $Id: gcm_maintenance.php,v 1.2 2011-03-24 09:49:20 arjen Exp $
28
29 ini_set('include_path', '.:./classes:../phpclasses');
30 ini_set('html_errors', 'false');
31
32 define("BATCHSIZE", 10000);
33
34 require_once "gnucomo_config.php";
35 require_once "db.class.php";
36
37 // Set the standard variables //
38
39 $purge_date     ="";            // Purge log entries until this date. Default: no purging
40 $project_name   = "gnucomo";    // name of the entire project
41 $app_name       = "gcm_maintenance"; // name of the application running
42
43 //Avoid time-limit issues
44 set_time_limit(0);
45
46 //  Scan the command arguments
47
48 for ($argi = 1; $argi < $argc; $argi++)
49 {
50    switch ($argv[$argi])
51    {
52    case "-c":
53       $argi++;
54       $project_name = $argv[$argi];
55       break;
56
57    case "-p":
58       $argi++;
59       $purge_date = $argv[$argi];
60       break;
61
62    default:
63       echo "Usage: gcm_daemon [-c configname] [-p purgedate]\n";
64       exit();
65       break;
66    }
67 }
68
69 // Read the database settings //
70 $class_settings = new gnucomo_config();
71 if (!$class_settings->read($project_name))
72 {
73    echo "Can not read Gnucomo configuration file for $project_name.\n";
74    exit();
75 }
76
77 openlog("gnucomo", LOG_PID, LOG_DAEMON);
78
79 //Open an connection to the database
80 $dbms_type = $class_settings->find_parameter("database", "type");
81 $dbms_host = $class_settings->find_parameter("database", "host");
82 $dbms_name = $class_settings->find_parameter("database", "name");
83 $dbms_user = $class_settings->find_parameter("gcm_daemon", "user");
84 $dbms_password = $class_settings->find_parameter("gcm_daemon", "password");
85
86 db_select($dbms_type);
87 $dbms = new db();
88 $dbms->db_host = $dbms_host;
89 $dbms->db_name = $dbms_name;
90 $dbms->db_user = $dbms_user;
91 $dbms->db_password = $dbms_password;
92 $dbms->db_connect($class_settings->database());
93
94 if ($dbms->have_db_connection() == "FALSE")
95 {
96    exit ("Database connection failed.");
97 }
98
99    if ($purge_date != "")
100    {
101       purge_old_logs($purge_date);
102    }
103    else
104    {
105
106       // Check the references from the abuse list to the logs
107
108
109       $abuse_result = $dbms->query("select source, nr_abuses, masklen(source) from object_abuse
110                                  where (status='dropped' or status='') and masklen(source)>16 and objectid=11 order by source");
111       for ($ab = 0; $ab < $dbms->num_rows($abuse_result); $ab++)
112       {
113          $logentries = 0;
114          $abuse = $dbms->fetch_object($abuse_result, $ab);
115          $nr_abuses = $abuse->nr_abuses;
116          $log_res = $dbms->query("select logid from log_abuse where source='" . $abuse->source . "' and objectid=11");
117
118          if ($dbms->num_rows($log_res) != $nr_abuses)
119          {
120             echo "Mismatch in nr of abuses. Corrected.\n";
121             $dbms->query("update object_abuse set nr_abuses=" . $dbms->num_rows($log_res) .
122                          " where source='" . $abuse->source . "' and objectid=11");
123          }
124          if ($dbms->num_rows($log_res) == 0)
125          {
126             echo "  Removing ", $abuse->source, "\n";
127             $dbms->query("delete from object_abuse where source='". $abuse->source . "' and objectid=11");
128          }
129          else
130          {
131             $logref_res = $dbms->query("select logid from log where logid in
132                                     (select logid from log_abuse where source='" . $abuse->source . "' and objectid=11)");
133             $logentries = $dbms->num_rows($logref_res);
134             if ($logentries == 0)
135             {
136                echo "All references to log entries are lost. Purging log_abuse table.\n";
137                $dbms->query("delete from log_abuse where source='" . $abuse->source . "' and objectid=11");
138             
139             }
140          }
141          echo $abuse->source . "\t$nr_abuses\t" . $dbms->num_rows($log_res) . "\t" . $logentries . "\n";
142       }
143    }
144
145    //  Gather the statistics for each object
146
147    $obj_result = $dbms->query("SELECT objectid FROM object");
148    for ($obj = 0; $obj < $dbms->num_rows($obj_result); $obj++)
149    {
150       $object = $dbms->fetch_object($obj_result, $obj);
151       echo "Gathering statistics for object " . $object->objectid . "\n";
152       GatherStatistics($object->objectid);
153    }
154
155
156 function purge_old_logs($purge_date)
157 {
158    global $dbms;
159
160    /*
161     *   Make a temporary table with the logids of the old log entries
162     *   We don't want to repeat a selection on the large log table itself.
163     */
164
165    echo "Purging log entries before $purge_date\n";
166
167    $dbms->query("CREATE TABLE gcm_deamon_old_log AS SELECT logid FROM log WHERE timestamp < '$purge_date'");
168    $dbms->query("SELECT logid FROM gcm_deamon_old_log");
169    echo $dbms->num_rows() . " log entries found.\n";
170
171    //  Clean up notifications that are left without reference to the log.
172
173    $r = $dbms->query("select notificationid from log_notification where logid in
174                          (select logid from gcm_deamon_old_log) group by notificationid");
175    echo "Notifications that may be affected:\n";
176    $notifications = array();
177    for ($i=0; $i < $dbms->num_rows(); $i++)
178    {
179       $notif = $dbms->fetch_object($r, $i);
180       $notifications[] = $notif->notificationid;
181       echo $notif->notificationid . "\n";
182    }
183    $dbms->query("delete from log_notification where logid in
184                          (select logid from gcm_deamon_old_log)");
185
186    //  Clean up any notifications that have no more logs left
187    foreach ($notifications as $notif)
188    {
189       $c = $dbms->fetch_object($dbms->query("select count(*) from log_notification where notificationid=$notif"), 0);
190       echo "Notification $notif has " . $c->count . " log entries left.\n";
191       if ($c->count == 0)
192       {
193          echo "Cleaning up notification $notif.\n";
194          $dbms->query("delete from action_user where notificationid=$notif");
195          $dbms->query("delete from notification where notificationid=$notif");
196       }
197    }
198
199    //  Clean up abuses that are left without reference to the log.
200
201    $r = $dbms->query("select source from log_abuse where masklen(source) > 16 and logid in
202                          (select logid from gcm_deamon_old_log) group by source");
203    echo "Abusing IP addresses that may be affected:\n";
204    $abusers = array();
205    for ($i = 0; $i < $dbms->num_rows(); $i++)
206    {
207       $ab = $dbms->fetch_object($r, $i);
208       $abusers[] = $ab->source;
209       echo $ab->source . "\n";
210    }
211    $dbms->query("delete from log_abuse where logid in
212                    (select logid from gcm_deamon_old_log)");
213
214    foreach ($abusers as $src)
215    {
216       $c = $dbms->fetch_object($dbms->query("select count(*) from log_abuse where source='$src'"), 0);
217       echo "IP address $src has " . $c->count . " log entries left.\n";
218       if ($c->count == 0)
219       {
220          echo "Cleaning up abusing address $src.\n";
221          $dbms->query("delete from object_abuse where source='$src' and (status='' or status='dropped')");
222       }
223    }
224
225    $dbms->query("delete from log where logid in
226                          (select logid from gcm_deamon_old_log)");
227
228    $dbms->query("drop table gcm_deamon_old_log");
229 }
230
231
232 /*
233  *   Update a single statistic for some object.
234  *   If it does not yet exist, it will be created.
235  */
236
237 function UpdateStatistic($objectid, $name, $value)
238 {
239    global $dbms;
240
241    $result = $dbms->query("SELECT objectid FROM object_statistics WHERE
242              objectid='$objectid' AND statname='$name'");
243    if ($dbms->num_rows() == 0)
244    {
245       $dbms->query("INSERT INTO object_statistics VALUES
246                     ('$objectid', '$name', '$value')");
247    }
248    else
249    {
250       $dbms->query("UPDATE object_statistics SET statvalue='$value' WHERE
251            statname='$name' AND objectid='$objectid'");
252    }
253 }
254
255 /*
256  *   Gather the statistics for a single object ($objectid).
257  *   We count the number of parameters, removed parameters, notifications
258  *   closed notifications and log entries. The totals of these are
259  *   maintained in a separate table: object_statistics.
260  */
261
262 function GatherStatistics($objectid)
263 {
264    global $dbms;
265
266    //  Gather statistics on parameters
267
268    $r = $dbms->query("SELECT paramid FROM parameter WHERE objectid=CAST('"
269                         . $objectid . "' AS BIGINT)");
270    $nr_parameters = $dbms->num_rows($r);
271
272    $removed_parameters = 0;
273    for ($p = 0; $p < $nr_parameters; $p++)
274    {
275       $param = pg_fetch_object($r, $p);
276       $qry ="select change_nature from history where paramid= CAST('";
277       $qry .= $param->paramid . "' AS BIGINT) order by modified desc";
278       $rhist = $dbms->query($qry);
279       if ($dbms->num_rows($rhist) == 0)
280       {
281          echo "ERROR: No history for parameter id " . $param->paramid . "\n";
282       }
283       else
284       {
285          $hist = $dbms->fetch_object($rhist, 0);
286          if ($hist->change_nature == "REMOVED")
287          {
288             $removed_parameters++;
289          }
290       }
291    }
292
293    UpdateStatistic($objectid, 'parameters', $nr_parameters);
294    UpdateStatistic($objectid, 'removed_parameters', $removed_parameters);
295
296    //  Gather statistics on notifications
297
298    $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE
299                        objectid = CAST('" . $objectid . "' AS BIGINT)");
300    $cnt = $dbms->fetch_object($r, 0);
301    UpdateStatistic($objectid, 'notifications', $cnt->count);
302
303    $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE
304                        objectid = CAST('" . $objectid . "' AS BIGINT) AND statuscode ='cls'");
305    $cnt = $dbms->fetch_object($r, 0);
306    UpdateStatistic($objectid, 'closed_notifications', $cnt->count);
307
308    //  Gather statistics on log entries
309
310    $r = $dbms->query("SELECT count(logid) FROM log WHERE
311                        objectid = CAST('" . $objectid . "' AS BIGINT)");
312    $cnt = $dbms->fetch_object($r, 0);
313    UpdateStatistic($objectid, 'logs', $cnt->count);
314 }