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