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