Use the new object_statistics table.
[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.7 $
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     : Feb 14, 2003
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: objects.php,v $
32    Revision 1.7  2003-07-15 11:02:25  arjen
33    Use the new object_statistics table.
34
35    Revision 1.6  2003/02/21 08:46:58  arjen
36    Improved the table layout.
37
38    Revision 1.5  2003/02/13 09:01:29  arjen
39    All web interface pages use the page class.
40
41    Revision 1.4  2003/02/13 08:48:23  arjen
42    Added log, notification and parameter counters to the 'object' table.
43    Counting these things at the time a user interface needs them is
44    too slow. Other programs, like gcm_daemon en gcm_input should prepare
45    these counters for quick retrieval.
46
47    Revision 1.3  2003/02/10 15:42:24  arjen
48    Show the total number of Log entries, parameters and notifications
49
50    Revision 1.2  2003/02/05 09:48:14  arjen
51    Added display and handling of notifications
52
53 ******************************/
54
55 // RCSID = "$Id: objects.php,v 1.7 2003-07-15 11:02:25 arjen Exp $";
56
57 ini_set('include_path', '.:./classes:../phpclasses');
58
59 require_once('page.class.php');
60
61 function clientscripts()
62 {
63 ?>
64
65 <script language='JavaScript'>
66
67 function CheckCreate(f)
68 {
69    if (f.objectname.value == "")
70    {
71       alert("You must supply a name");
72       return false;
73    }
74    return true;
75 }
76
77 function CheckRemove(f)
78 {
79    var message = "Are you sure you want to remove object ";
80    message += f.objectname.value;
81    message += " ?";
82
83    return confirm(message);
84 }
85
86 </script>
87
88 <?php
89 }
90
91 class object_page extends page
92 {
93
94    var $nr_parameters, $removed_parameters;
95    var $nr_notifications, $closed_notifications;
96    var $nr_logs;
97
98    function GatherStatistics($objectid)
99    {
100       //  Gather statistics on parameters
101
102       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
103                            . $objectid . "' AS BIGINT) AND statname='parameters'");
104       $stat = pg_fetch_object($r, 0);
105       $this->nr_parameters = $stat->statvalue;
106
107       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
108                            . $objectid . "' AS BIGINT) AND statname='removed_parameters'");
109       $stat = pg_fetch_object($r, 0);
110       $this->removed_parameters = $stat->statvalue;
111
112       //  Gather statistics on notifications
113
114       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
115                            . $objectid . "' AS BIGINT) AND statname='notifications'");
116       $stat = pg_fetch_object($r, 0);
117       $this->nr_notifications = $stat->statvalue;
118
119       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
120                            . $objectid . "' AS BIGINT) AND statname='closed_notifications'");
121       $stat = pg_fetch_object($r, 0);
122       $this->closed_notifications = $stat->statvalue;
123
124       //  Gather statistics on log entries
125
126       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
127                            . $objectid . "' AS BIGINT) AND statname='logs'");
128       $stat = pg_fetch_object($r, 0);
129       $this->nr_logs = $stat->statvalue;
130
131    }
132
133    function Body()
134    {
135       clientscripts();
136
137    echo "<h1>Objects Administration</h1><hr>";
138
139    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['objectname']))
140    {
141       pg_exec($this->database, "INSERT INTO object (objectname, log_count, parameter_count, notification_count)
142                                VALUES ('" . $_POST['objectname'] . "', '0', '0', '0')");
143    }
144
145    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['objectname']))
146    {
147       pg_exec($this->database, "DELETE FROM object WHERE objectname='" . $_POST['objectname'] . "'");
148    }
149
150    $res = pg_exec($this->database, "SELECT objectid,objectname, log_count, notification_count
151                                     FROM object ORDER BY objectname");
152 ?>
153
154    <table>
155    <tr><th>Object</th><th>Log entries</th>
156        <th>Parameters</th><th>Notifications</th>
157    </tr>
158
159 <?php
160    $obj = 0;
161
162    //The counters are set to zero
163    $count_logs = 0;
164    $count_notifications = 0;
165    $closed_notifications = 0;
166    $count_parameters = 0;
167    $removed_parameters = 0;
168
169    while ($obj < pg_numrows($res))
170    {
171       $u = pg_fetch_object($res, $obj);
172
173       $this->GatherStatistics($u->objectid);
174
175       $count_parameters += $this->nr_parameters;
176       $removed_parameters += $this->removed_parameters;
177       $count_logs = $count_logs + $this->nr_logs;
178
179       //$nr_notifications = $u->notification_count;
180       $count_notifications += $this->nr_notifications;
181       $closed_notifications += $this->closed_notifications;
182       ?>
183       <tr><td><center><img src='server.png'><br>
184              <b><?php echo $u->objectname ?></b></center>
185       </td><td class='number'>
186           <?php echo "<a href='log.php?oid=$u->objectid'> $this->nr_logs </a>" ?>
187       </td><td class='number'>
188           <?php echo "<a href='parameter.php?oid=$u->objectid'>" . $this->nr_parameters
189                    . " (" . $this->removed_parameters . " removed)</a>" ?>
190       </td><td class='number'>
191           <?php echo "<a href='notification.php?oid=$u->objectid'>" . $this->nr_notifications . " (" . $this->closed_notifications . " closed)</a>" ?>
192       </td><td>
193           <form action='objects.php' method='post' onSubmit='return CheckRemove(this)'>
194               <input type='hidden' name='objectname' value='<?php echo $u->objectname ?>'>
195               <input type='submit' name='action' value='Remove'>
196           </form>
197       </td></tr>
198       <?php
199       $obj++;
200    }
201
202    //Show the totals
203    echo "<tr><td><strong><B><br><br>TOTALS</B></strong></td>";
204    echo "<td class='number'>$count_logs</td>";
205    echo "<td class='number'>$count_parameters ($removed_parameters removed)</td>";
206    echo "<td class='number'>$count_notifications ($closed_notifications closed)</td></tr>";
207    echo "</table>";
208
209 ?>
210
211 <h2>Create new object:</h2>
212 <p>
213
214 <form action='objects.php' method='post' onSubmit='return CheckCreate(this)'>
215 Object's name (FQDN): <input name='objectname' type='text'>
216 <br>
217 <input type='submit' name='action' value='Create'>
218 </form>
219 </p>
220 <?php
221
222    }
223 }
224
225 $obj_page = new object_page("Gnucomo Objects Administration");
226
227 $obj_page->Showpage();
228
229 ?>
230