When removing an object, clean up logs, parameters and notifications.
[gnucomo.git] / src / web / objects.php
1 <?php
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
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 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : objects.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.12 $
13 **
14 **  DESCRIPTION      : Objects Administration page.
15 **                     Input parameters: action (POST) : empty, 'Create'
16 **                              objname (POST) : name of the object to create or remove
17 **
18 **  EXPORTED OBJECTS : 
19 **  LOCAL    OBJECTS : 
20 **  MODULES  USED    :
21 ***************************************************************************
22 **  ADMINISTRATIVE INFORMATION *
23 ********************************
24 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
25 **      CREATION DATE   : Dec 04, 2002
26 **      LAST UPDATE     : Jul 13, 2010
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: objects.php,v $
32    Revision 1.12  2011-03-24 09:59:21  arjen
33    When removing an object, clean up logs, parameters and notifications.
34
35    Revision 1.11  2007/11/21 14:38:06  arjen
36    The buttonbar at the top of each page is now a fixed 'div' element
37    instead of a framed page.
38    Contributed by Edwin Nadorp.
39
40    Revision 1.10  2005/06/04 07:24:38  arjen
41    New page: Abuse list
42
43    Revision 1.9  2003/09/01 06:55:00  arjen
44    Provides an interface to edit the list of
45    services and users for an object.
46
47    Revision 1.8  2003/08/14 10:35:07  arjen
48    Added editing of detailed object information.
49
50    Revision 1.7  2003/07/15 11:02:25  arjen
51    Use the new object_statistics table.
52
53    Revision 1.6  2003/02/21 08:46:58  arjen
54    Improved the table layout.
55
56    Revision 1.5  2003/02/13 09:01:29  arjen
57    All web interface pages use the page class.
58
59    Revision 1.4  2003/02/13 08:48:23  arjen
60    Added log, notification and parameter counters to the 'object' table.
61    Counting these things at the time a user interface needs them is
62    too slow. Other programs, like gcm_daemon en gcm_input should prepare
63    these counters for quick retrieval.
64
65    Revision 1.3  2003/02/10 15:42:24  arjen
66    Show the total number of Log entries, parameters and notifications
67
68    Revision 1.2  2003/02/05 09:48:14  arjen
69    Added display and handling of notifications
70
71 ******************************/
72
73 // RCSID = "$Id: objects.php,v 1.12 2011-03-24 09:59:21 arjen Exp $";
74
75 ini_set('include_path', '.:./classes:../phpclasses');
76
77 require_once('page.class.php');
78
79 function clientscripts()
80 {
81 ?>
82
83 <script type='text/ecmascript'>
84
85 function CheckCreate(f)
86 {
87    if (f.objectname.value == "")
88    {
89       alert("You must supply a name");
90       return false;
91    }
92    return true;
93 }
94
95 function CheckRemove(f)
96 {
97    var message = "Are you sure you want to remove object ";
98    message += f.objectname.value;
99    message += " ?";
100
101    return confirm(message);
102 }
103
104 </script>
105
106 <?php
107 }
108
109 class object_page extends page
110 {
111
112    var $nr_parameters, $removed_parameters;
113    var $nr_notifications, $closed_notifications;
114    var $nr_logs;
115
116    function GatherStatistics($objectid)
117    {
118       //  Gather statistics on parameters
119
120       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
121                            . $objectid . "' AS BIGINT) AND statname='parameters'");
122       $stat = pg_fetch_object($r, 0);
123       $this->nr_parameters = $stat->statvalue;
124
125       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
126                            . $objectid . "' AS BIGINT) AND statname='removed_parameters'");
127       $stat = pg_fetch_object($r, 0);
128       $this->removed_parameters = $stat->statvalue;
129
130       //  Gather statistics on notifications
131
132       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
133                            . $objectid . "' AS BIGINT) AND statname='notifications'");
134       $stat = pg_fetch_object($r, 0);
135       $this->nr_notifications = $stat->statvalue;
136
137       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
138                            . $objectid . "' AS BIGINT) AND statname='closed_notifications'");
139       $stat = pg_fetch_object($r, 0);
140       $this->closed_notifications = $stat->statvalue;
141
142       //  Gather statistics on log entries
143
144       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
145                            . $objectid . "' AS BIGINT) AND statname='logs'");
146       $stat = pg_fetch_object($r, 0);
147       $this->nr_logs = $stat->statvalue;
148
149    }
150
151    function Body()
152    {
153       clientscripts();
154
155    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['objectname']))
156    {
157       pg_exec($this->database, "INSERT INTO object (objectname, log_count, parameter_count, notification_count)
158                                VALUES ('" . $_POST['objectname'] . "', '0', '0', '0')");
159    }
160
161    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['objectid']))
162    {
163       $oid = $_POST['objectid'];
164
165       //  Cleanup notifications for this object
166
167       $r = pg_exec($this->database, "SELECT notificationid FROM notification WHERE objectid='$oid'");
168       for ($i = 0; $i < pg_numrows($r); $i++)
169       {
170          $notif = pg_fetch_object($r, $i);
171          $notid = $notif->notificationid;
172          pg_exec($this->database, "DELETE FROM log_notification WHERE notificationid='$notid'");
173          pg_exec($this->database, "DELETE FROM action_user WHERE notificationid='$notid'");
174          pg_exec($this->database, "DELETE FROM parameter_notification WHERE notificationid='$notid'");
175       }
176       pg_exec($this->database, "DELETE FROM notification WHERE objectid='$oid'");
177
178       //  Cleanup parameters for this object
179
180       $r = pg_exec($this->database, "SELECT paramid FROM parameter WHERE objectid='$oid'");
181       for ($i = 0; $i < pg_numrows($r); $i++)
182       {
183          $param = pg_fetch_object($r, $i);
184          $parid = $param->paramid;
185          pg_exec($this->database, "DELETE FROM history WHERE paramid='$parid'");
186          pg_exec($this->database, "DELETE FROM property WHERE paramid='$parid'");
187       }
188       pg_exec($this->database, "DELETE FROM parameter WHERE objectid='$oid'");
189
190       //  Cleanup log and abuses for this object
191
192       pg_exec($this->database, "DELETE FROM log_abuse WHERE objectid='$oid'");
193       pg_exec($this->database, "DELETE FROM object_abuse WHERE objectid='$oid'");
194       pg_exec($this->database, "DELETE FROM log WHERE objectid='$oid'");
195
196       //  Cleanup services, statistics, users and finally, the object itself.
197
198       pg_exec($this->database, "DELETE FROM object_service WHERE objectid='$oid'");
199       pg_exec($this->database, "DELETE FROM object_statistics WHERE objectid='$oid'");
200       pg_exec($this->database, "DELETE FROM object_user WHERE objectid='$oid'");
201       pg_exec($this->database, "DELETE FROM object WHERE objectid='$oid'");
202    }
203
204    if (isset($_GET['oid']))
205    {
206       echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = \"<h1>Detailed information for object " . $_GET['oid'] . "<\/h1>\"</script><br>\n";
207
208       if (isset($_POST['action']) && $_POST['action'] == 'Save Changes')
209       {
210          $qry = "UPDATE object SET objectname='" . $_POST['oname'] . "'";
211          $qry .= ", objectcode='" . $_POST['ocode'] . "'";
212          $qry .= ", object_description='" . $_POST['odescription'] . "'";
213          $qry .= ", object_owner='" . $_POST['oowner'] . "'";
214          $qry .= ", physical_location='" . $_POST['olocation'] . "'";
215          $qry .= ", remark='" . $_POST['oremark'] . "'";
216          $qry .= ", timezone='" . $_POST['otimezone'] . "'";
217          $qry .= " WHERE objectid=" . $_GET['oid'];
218
219          pg_exec($this->database, $qry);
220       }
221       else if (isset($_POST['action']) && $_POST['action'] == 'Modify Service')
222       {
223          $obj_srv_res = pg_exec($this->database, "SELECT * from object_service WHERE objectid='" . $_GET['oid']
224                               . "' AND servicecode='" . $_POST['servcode'] . "'");
225
226          if (pg_numrows($obj_srv_res) == 0)
227          {
228             //  Add a new service for this object
229             if (isset($_POST['servused']))
230             {
231                $qry = "INSERT INTO object_service (objectid, servicecode, expected_interval) ";
232                $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['servcode'] . "', '";
233                $qry .= $_POST['expinterval'] . "')";
234
235                pg_exec($this->database, $qry);
236             }
237          }
238          else
239          {
240             //  Modify or remove the service for this object
241             if (isset($_POST['servused']))
242             {
243                //  Service is still used; update some values.
244                $qry = "UPDATE object_service SET expected_interval='" . $_POST['expinterval'];
245                $qry .= "' WHERE objectid='" . $_GET['oid'];
246                $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
247                pg_exec($this->database, $qry);
248             }
249             else
250             {
251                //  Checkbox was turned off, remove the service
252
253                $qry = "DELETE FROM object_service WHERE objectid='" . $_GET['oid'];
254                $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
255                pg_exec($this->database, $qry);
256             }
257          }
258       }
259       else if (isset($_POST['action']) && $_POST['action'] == 'Modify User')
260       {
261          $obj_srv_res = pg_exec($this->database, "SELECT * from object_user WHERE objectid='" . $_GET['oid']
262                               . "' AND username='" . $_POST['username'] . "'");
263
264          if (pg_numrows($obj_srv_res) == 0)
265          {
266             //  Add a new user for this object
267             if (isset($_POST['userused']))
268             {
269                $qry = "INSERT INTO object_user (objectid, username, security_level) ";
270                $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['username'] . "', '";
271                $qry .= $_POST['seclevel'] . "')";
272
273                pg_exec($this->database, $qry);
274             }
275          }
276          else
277          {
278             //  Modify or remove the user for this object
279             if (isset($_POST['userused']))
280             {
281                //  Service is still used; update some values.
282                $qry = "UPDATE object_user SET security_level='" . $_POST['seclevel'];
283                $qry .= "' WHERE objectid='" . $_GET['oid'];
284                $qry .= "' AND username='" . $_POST['username'] . "'";
285                pg_exec($this->database, $qry);
286             }
287             else
288             {
289                //  Checkbox was turned off, remove the user
290
291                $qry = "DELETE FROM object_user WHERE objectid='" . $_GET['oid'];
292                $qry .= "' AND username='" . $_POST['username'] . "'";
293                pg_exec($this->database, $qry);
294             }
295          }
296       }
297
298       $res = pg_exec($this->database, "SELECT * FROM object
299                                        WHERE objectid='" . $_GET['oid'] . "'");
300       $obj = pg_fetch_object($res, 0);
301
302       echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
303       echo "<table>";
304
305       echo "<tr><td>Name</td><td><input name='oname' type='text' value='";
306       echo $obj->objectname . "'></td></tr>";
307       echo "<tr><td>Identification code</td><td><input name='ocode' type='text' value='";
308       echo $obj->objectcode . "'></td></tr>";
309       echo "<tr><td>Description</td><td><textarea name='odescription' rows='3' cols='30'>";
310       echo $obj->object_description . "</textarea></td></tr>";
311       echo "<tr><td>Owner</td><td><textarea name='oowner' rows='3' cols='30'>";
312       echo $obj->object_owner . "</textarea></td></tr>";
313       echo "<tr><td>Physical location</td><td><textarea name='olocation' rows='3' cols='30'>";
314       echo $obj->physical_location . "</textarea></td></tr>";
315       echo "<tr><td>Remarks</td><td><textarea name='oremark' rows='3' cols='30'>";
316       echo $obj->remark . "</textarea></td></tr>";
317       echo "<tr><td>Timezone</td><td><input name='otimezone' type='text' value='";
318       echo $obj->timezone . "'></td></tr>";
319
320       echo "</table>";
321       echo "<input type='submit' name='action' value='Save Changes'>";
322       echo "</form>";
323
324       echo "<h1>Services on " . $obj->objectname . "</h1><hr>\n";
325
326       echo "<table>";
327       echo "<tr><th>Service name</th><th>Last entry</th><th>Expected interval</th></tr>";
328       $srv_res = pg_exec($this->database, "SELECT servicecode, servicename FROM service
329                                            ORDER BY servicecode");
330       for ($srv_row = 0; $srv_row < pg_numrows($srv_res); $srv_row++)
331       {
332          $srv = pg_fetch_object($srv_res, $srv_row);
333          $srv_obj_res = pg_exec($this->database, "SELECT * FROM object_service WHERE objectid='"
334                                    . $obj->objectid . "' AND servicecode='" . $srv->servicecode . "'");
335
336          echo "<tr>";
337          echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
338          echo "<input type='hidden' name='servcode' value='" . $srv->servicecode . "'>";
339
340          if (pg_numrows($srv_obj_res) == 0)
341          {
342             echo "<td><input name='servused' type='checkbox'> " . $srv->servicename . "</td>";
343             echo "<td>&nbsp;</td>";
344             echo "<td><input name='expinterval' type='text' value='0'></td>";
345             echo "<td><input type='submit' name='action' value='Modify Service'></td>";
346          }
347          else
348          {
349             $obj_srv = pg_fetch_object($srv_obj_res, 0);
350             echo "<td><input name='servused' type='checkbox' checked='true'> " . $srv->servicename . "</td>";
351             echo "<td>" . $obj_srv->last_entry . "</td>";
352             echo "<td><input name='expinterval' type='text' value='" . $obj_srv->expected_interval . "'></td>";
353             echo "<td><input type='submit' name='action' value='Modify Service'></td>";
354          }
355          echo "</form></tr>";
356       }
357       echo "</table>";
358
359       echo "<h1>Users of " . $obj->objectname . "</h1><hr>\n";
360
361       echo "<table>";
362       echo "<tr><th>User name</th><th>Security level</th><th>&nbsp;</th></tr>";
363
364       $usr_res = pg_exec($this->database, "SELECT username, security_level FROM usr ORDER BY username");
365       for ($usr_row = 0; $usr_row < pg_numrows($usr_res); $usr_row++)
366       {
367          $usr = pg_fetch_object($usr_res, $usr_row);
368          $usr_obj_res = pg_exec($this->database, "SELECT * FROM object_user WHERE objectid='"
369                                    . $obj->objectid . "' AND username='" . $usr->username . "'");
370
371          echo "<tr>";
372          echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
373          echo "<input type='hidden' name='username' value='" . $usr->username . "'>";
374
375          if (pg_numrows($usr_obj_res) == 0)
376          {
377             echo "<td><input name='userused' type='checkbox'> " . $usr->username . "</td>";
378             echo "<td><input name='seclevel' type='text' value='" . $usr->security_level . "'></td>";
379             echo "<td><input type='submit' name='action' value='Modify User'></td>";
380          }
381          else
382          {
383             $usr_obj = pg_fetch_object($usr_obj_res, 0);
384             echo "<td><input name='userused' type='checkbox' checked='on'> " . $usr->username . "</td>";
385             echo "<td><input name='seclevel' type='text' value='" . $usr_obj->security_level . "'></td>";
386             echo "<td><input type='submit' name='action' value='Modify User'></td>";
387          }
388          echo "</form></tr>";
389       }
390       echo "</table>";
391    }
392    else
393    {
394       echo "<script type='text/ecmascript'>
395             document.getElementById('menu_title').innerHTML = \"<h1>Objects Administration<\/h1>\"
396             </script>
397             <br>";
398
399
400       $res = pg_exec($this->database, "SELECT * FROM object ORDER BY objectname");
401 ?>
402
403       <table>
404       <tr><th>Object</th><th>Description</th><th>Log entries</th>
405           <th>Parameters</th><th>Notifications</th><th>Abuses</th></tr>
406
407 <?php
408       $obj = 0;
409
410       //The counters are set to zero
411       $count_logs = 0;
412       $count_notifications = 0;
413       $closed_notifications = 0;
414       $count_parameters = 0;
415       $removed_parameters = 0;
416
417       while ($obj < pg_numrows($res))
418       {
419          $u = pg_fetch_object($res, $obj);
420
421          $this->GatherStatistics($u->objectid);
422
423          $count_parameters += $this->nr_parameters;
424          $removed_parameters += $this->removed_parameters;
425          $count_logs = $count_logs + $this->nr_logs;
426
427          $count_notifications += $this->nr_notifications;
428          $closed_notifications += $this->closed_notifications;
429          ?>
430          <tr><td><center><a href='objects.php?oid=<?php echo $u->objectid?>'><img src='server.png' alt='server'></a><br>
431                 <b><?php echo $u->objectname ?></b></center>
432          </td><td>
433                <?php echo nl2br($u->object_description) ?>
434          </td><td class='number'>
435              <?php if ( $this->nr_logs > 0 )
436                    { 
437                        echo "<a href='log.php?oid=$u->objectid'> $this->nr_logs </a>";
438                    }
439                    else
440                    { 
441                       echo "0";
442                    }
443               ?>
444          </td><td class='number'>
445              <?php echo "<a href='parameter.php?oid=$u->objectid'>" . $this->nr_parameters
446                       . " (" . $this->removed_parameters . " removed)</a>" ?>
447          </td><td class='number'>
448              <?php if( $this->nr_notifications == $this->closed_notifications )
449                    {
450                       echo $this->nr_notifications . " (" . $this->closed_notifications . " closed)"; 
451                    }
452                    else
453                    {
454                       echo "<a href='notification.php?oid=$u->objectid'>" . $this->nr_notifications
455                       . " (" . $this->closed_notifications . " closed)</a>"; 
456                    }
457               ?>
458          </td><td class='number'>
459              <?php echo "<a href='abuse.php?oid=$u->objectid'>" . "Abuse list"
460                       . " </a>" ?>
461          </td><td>
462              <form action='objects.php' method='post' onSubmit='return CheckRemove(this)'>
463                  <input type='hidden' name='objectid' value='<?php echo $u->objectid ?>'>
464                  <input type='submit' name='action' value='Remove'>
465              </form>
466          </td></tr>
467          <?php
468          $obj++;
469       }
470
471       //Show the totals
472       echo "<tr><td><strong><B><br><br>TOTALS</B></strong></td>";
473       echo "<td>&nbsp;</td>";
474       echo "<td class='number'>$count_logs</td>";
475       echo "<td class='number'>$count_parameters ($removed_parameters removed)</td>";
476       echo "<td class='number'>$count_notifications ($closed_notifications closed)</td>";
477       echo "<td>&nbsp;</td>";
478       echo "</tr>\n";
479       echo "</table>\n";
480
481 ?>
482
483 <h2>Create new object:</h2>
484
485 <form action='objects.php' method='post' onSubmit='return CheckCreate(this)'>
486 Object's name (FQDN): <input name='objectname' type='text'>
487 <br>
488 <input type='submit' name='action' value='Create'>
489 </form>
490
491 <?php
492
493    }
494    }
495 }
496
497 $obj_page = new object_page("Gnucomo Objects Administration");
498
499 $obj_page->Showpage();
500
501 ?>
502