Added an interface to edit check patterns
[gnucomo.git] / src / web / services.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      : services.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.2 $
13 **
14 **  DESCRIPTION      :  Service Administration page.
15 **                      Input parameters: action (POST) : empty, 'Create'
16 **                              servcode (POST) : short name of the service 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   : Aug 04, 2003
26 **      LAST UPDATE     : Aug 28, 2003
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: services.php,v $
32    Revision 1.2  2005-06-04 07:25:59  arjen
33    Added an interface to edit check patterns
34
35    Revision 1.1  2003/09/01 06:53:22  arjen
36    New page to enter and modify services.
37
38 ******************************/
39
40 // RCSID = "$Id: services.php,v 1.2 2005-06-04 07:25:59 arjen Exp $";
41
42 ini_set('include_path', '.:./classes:../phpclasses');
43
44 require_once('page.class.php');
45
46 function clientscripts()
47 {
48
49 ?>
50
51 <script language='JavaScript'>
52
53 function CheckCreate(f)
54 {
55    if (f.servcode.value == "")
56    {
57       alert("You must supply a service code");
58       return false;
59    }
60    return true;
61 }
62
63 function CheckRemove(f)
64 {
65    var message = "Are you sure you want to remove service ";
66    message += f.servcode.value;
67    message += " ?";
68
69    return confirm(message);
70 }
71
72 </script>
73
74 <?php
75 }
76
77 class service_page extends page
78 {
79
80    function Body()
81    {
82
83    clientscripts();
84
85    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['servcode']))
86    {
87       pg_exec($this->database, "INSERT INTO service
88                        (servicecode, servicename, default_priority, max_priority) VALUES ('"
89                      . $_POST['servcode'] . "','" . $_POST['servname'] . "','"
90                      . $_POST['defprior'] . "','" . $_POST['maxprior'] . "')");
91    }
92
93    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['servcode']))
94    {
95       pg_exec($this->database, "DELETE FROM service WHERE servicecode='"
96                                 . $_POST['servcode'] . "'");
97    }
98
99    if (isset($_GET['servcode']))
100    {
101       echo "<h1>Detailed information for service " . $_GET['servcode'] . "</h1><br>\n";
102
103       if (isset($_POST['action']) && $_POST['action'] == 'Save Changes')
104       {
105          $qry = "UPDATE service SET servicename='" . $_POST['servname'] . "'";
106          $qry .= ", default_priority='" . $_POST['defprior'] . "'";
107          $qry .= ", max_priority='" . $_POST['maxprior'] . "'";
108          $qry .= " WHERE servicecode='" . $_GET['servcode'] . "'";
109
110          pg_exec($this->database, $qry);
111       }
112       if (isset($_POST['action']) && $_POST['action'] == 'Add')
113       {
114          $qry = "INSERT INTO service_pattern VALUES ('". $_GET['servcode'] . "'";
115          $qry .= ", '" . $_POST['Rank'] . "'";
116          $qry .= ", '" . $_POST['Pattern'] . "'";
117          $qry .= ", '" . $_POST['Pat_Action'] . "'";
118          $qry .= ", '" . $_POST['Argument'] . "')";
119
120          pg_exec($this->database, $qry);
121       }
122       if (isset($_POST['action']) && $_POST['action'] == 'Change')
123       {
124          $qry = "UPDATE service_pattern SET rank = '" . $_POST['Rank'] . "'";
125          $qry .= ", pattern = '" . $_POST['Pattern'] . "'";
126          $qry .= ", action = '" . $_POST['Pat_Action'] . "'";
127          $qry .= ", argument = '" . $_POST['Argument'] . "'";
128          $qry .= " WHERE service = '" . $_GET['servcode'] . "' AND rank = '" . $_POST['Rank'] . "'";
129
130          pg_exec($this->database, $qry);
131       }
132       if (isset($_POST['action']) && $_POST['action'] == 'Delete')
133       {
134          $qry = "DELETE FROM service_pattern WHERE service = '". $_GET['servcode'] . "'";
135          $qry .= " AND rank =  '" . $_POST['Rank'] . "'";
136
137          pg_exec($this->database, $qry);
138       }
139
140       $res = pg_exec($this->database, "SELECT * FROM service
141                                        WHERE servicecode='" . $_GET['servcode'] . "'");
142       $srv = pg_fetch_object($res, 0);
143
144       echo "<form action='services.php?servcode=" . $srv->servicecode . "' method='POST'>";
145       echo "<table>";
146
147       echo "<tr><td>Service name</td><td><input name='servname' type='text' value='";
148       echo $srv->servicename . "'></td></tr>";
149       echo "<tr><td>Default priority</td><td><select name='defprior'>";
150       for ($prior = 1; $prior < 6; $prior++)
151       {
152          echo "<option value='$prior'";
153          if ($prior == $srv->default_priority)
154          {
155             echo " selected='true'";
156          }
157          echo ">$prior</option>\n";
158       }
159       echo "</select>";
160       echo "<tr><td>Maximum priority</td><td><select name='maxprior'>";
161       for ($prior = 1; $prior < 6; $prior++)
162       {
163          echo "<option value='$prior'";
164          if ($prior == $srv->max_priority)
165          {
166             echo " selected='true'";
167          }
168          echo ">$prior</option>\n";
169       }
170       echo "</select>";
171       echo "</table>";
172       echo "<input type='submit' name='action' value='Save Changes'>";
173       echo "</form>";
174
175       echo "<h1>Patterns to check logs of " . $srv->servicename . "</h1>\n";
176
177       echo "<table>";
178       echo "<tr><th>Rank</th><th>Pattern</th><th>Action</th>";
179       echo "<th>Argument</th><th>&nbsp;</th><th>&nbsp;</th></tr>\n";
180
181       $pat_res = pg_exec($this->database, "SELECT * FROM service_pattern
182                                            WHERE service = '" . $srv->servicecode . "' ORDER BY rank");
183       for ($pat_row = 0; $pat_row < pg_numrows($pat_res); $pat_row++)
184       {
185          $pat = pg_fetch_object($pat_res, $pat_row);
186          echo "<tr><form action='services.php?servcode=" . $srv->servicecode . "' method='POST'><td>";
187          echo "<input type='text' name='Rank' value='" . $pat->rank . "'>";
188          echo "</td><td>";
189          echo "<input type='text' size='40' name='Pattern' value='" . $pat->pattern . "'>";
190          echo "</td><td>";
191          echo "<input type='text' name='Pat_Action' value='" . $pat->action . "'>";
192          echo "</td><td>";
193          echo "<input type='text' name='Argument' value='" . $pat->argument . "'>";
194          echo "</td><td>";
195          echo "<input type='submit' name='action' value='Change'>";
196          echo "</td><td>";
197          echo "<input type='submit' name='action' value='Delete'>";
198          echo "</td></form></tr>";
199       }
200       echo "<tr><form action='services.php?servcode=" . $srv->servicecode . "' method='POST'><td>";
201       echo "<input type='text' name='Rank' value=''>";
202       echo "</td><td>";
203       echo "<input type='text' size='40' name='Pattern' value=''>";
204       echo "</td><td>";
205       echo "<input type='text' name='Pat_Action' value=''>";
206       echo "</td><td>";
207       echo "<input type='text' name='Argument' value=''>";
208       echo "</td><td>";
209       echo "<input type='submit' name='action' value='Add'>";
210       echo "</td><td>&nbsp;";
211       echo "</td></form></tr>";
212       echo "</table>";
213    }
214    else
215    {
216    echo "<h1>Service Administration</h1><hr>";
217
218    $res = pg_exec($this->database, "SELECT * FROM service ORDER BY servicecode");
219
220    echo "<table>";
221    echo "<tr><th>Service code</th><th>Name</th><th>Priority</th><th>Max priority</th><tr>";
222    $srv = 0;
223    while ($srv < pg_numrows($res))
224    {
225       $u = pg_fetch_object($res, $srv);
226       ?>
227       <tr><td align='center'><a href='services.php?servcode=<?php echo $u->servicecode?>'><img src='service.png'></a><br>
228              <b><?php echo $u->servicecode ?></b>
229       </td><td>
230          <?php echo $u->servicename ?>
231       </td><td>
232          <?php echo $u->default_priority ?>
233       </td><td>
234          <?php echo $u->max_priority ?>
235       </td><td>
236           <form action='services.php' method='post' onSubmit='return CheckRemove(this)'>
237               <input type='hidden' name='servcode' value='<?php echo $u->servicecode ?>'>
238               <input type='submit' name='action' value='Remove'>
239           </form>
240       </td></tr>
241       <?php
242       $srv++;
243    }
244    echo "</table>";
245
246 ?>
247
248 <h2>Create new service:</h2>
249 <p>
250
251 <form action='services.php' method='post' onSubmit='return CheckCreate(this)'>
252 Service code: <input name='servcode' type='text'>
253 Service name: <input name='servname' type='text'>
254 Default priority: <select name='defprior'>
255 <option value='1'>1</option>
256 <option value='2'>2</option>
257 <option value='3'>3</option>
258 <option value='4'>4</option>
259 <option value='5'>5</option>
260 </select>
261 Maximum priority: <select name='maxprior'>
262 <option value='1'>1</option>
263 <option value='2'>2</option>
264 <option value='3'>3</option>
265 <option value='4'>4</option>
266 <option value='5' selected='true'>5</option>
267 </select>
268 <br>
269 <input type='submit' name='action' value='Create'>
270 </form>
271 </p>
272
273 <?php
274    }
275    }
276 }
277
278 $page = new service_page("Gnucomo User Administration");
279
280 $page->Showpage();
281
282 ?>