vogelor
2008-12-02 fb4c27e329659f7ebfb72855297549ac01a54f02
commit | author | age
8793b3 1 <?php
V 2 /*
3 Copyright (c) 2007-2008, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 require_once('../../lib/config.inc.php');
31 require_once('../../lib/app.inc.php');
32 require_once('tools.inc.php');
33
34 /* Check permissions for module */
35 $app->auth->check_module_permissions('monitor');
36
37 /* Change the Server if needed */
38 if (isset($_GET['server'])){
39     $server = explode('|', $_GET['server'], 2);
40     $_SESSION['monitor']['server_id'] = $server[0];
41     $_SESSION['monitor']['server_name'] = $server[1];
42 }
43
44 /*
45  *  Loading the template
46  */
47 $app->uses('tpl');
48 $app->tpl->newTemplate("form.tpl.htm");
49 $app->tpl->setInclude('content_tpl','templates/show_sys_state.htm');
50
51 /*
52  * setting the content
53  */
54 if ($_GET['state'] == 'server')
55 {
56     $output = _getServerState($_SESSION['monitor']['server_id'], $_SESSION['monitor']['server_name'], true);
57     $title = "Server State";
9cd0f6 58     $stateType = 'server';
8793b3 59 }
V 60 else
61 {
62     $output = _getSysState();
63     $title = "System State";
9cd0f6 64     $stateType = 'system';
8793b3 65 }
V 66
67 $app->tpl->setVar("state_data",$output);
9cd0f6 68 $app->tpl->setVar("state_type",$stateType);
8793b3 69 $app->tpl->setVar("title",$title);
V 70 $app->tpl->setVar("description",$description);
71
72 /*
9cd0f6 73  Creating the array with the refresh intervals
V 74  Attention: the core-moule ist triggered every 5 minutes,
75             so reload every 2 minutes is impossible!
76 */
77 $refresh = (isset($_GET["refresh"]))?intval($_GET["refresh"]):0;
78
79 $refresh_values = array('0' => '- '.$app->lng("No Refresh").' -','5' => '5 '.$app->lng("minutes"),'10' => '10 '.$app->lng("minutes"),'15' => '15 '.$app->lng("minutes"),'30' => '30 '.$app->lng("minutes"),'60' => '60 '.$app->lng("minutes"));
80 $tmp = '';
81 foreach($refresh_values as $key => $val) {
82     if($key == $refresh) {
83         $tmp .= "<option value='$key' SELECTED>$val</option>";
84     } else {
85         $tmp .= "<option value='$key'>$val</option>";
86     }
87 }
88 $app->tpl->setVar("refresh",$tmp);
89
90 /*
8793b3 91  * doing the output
V 92  */
93 $app->tpl_defaults();
94 $app->tpl->pparse();
95
96
97 function _getSysState(){
98     global $app;
99
100     /*
101      * Get all Servers and calculate the state of them
102      */
103     $html = '';
104
105     $servers = $app->db->queryAllRecords("SELECT server_id, server_name FROM server order by server_name");
106     foreach ($servers as $server)
107     {
108         $html .= _getServerState($server['server_id'], $server['server_name'], false);
109     }
110
111     return $html;
112 }
113
114 /*
115  * Calculates the State of ONE Server
116  */
117 function _getServerState($serverId, $serverName, $showAll)
118 {
119     global $app;
120
121     /*  The State of the server */
a2e689 122     $serverState = 'ok';
8793b3 123
V 124     /** The Number of several infos, warnings, errors, ... */
125     $count = array('unknown' => 0, 'info' => 0, 'warning' => 0, 'critical' => 0, 'error' => 0);
126
127     /** The messages */
128     $messages = array();
129
130     /** The Result of the function */
131     $res = '';
132
133     /*
134      * get all monitoring-data from the server als process then
135      * (count them and set the server-state)
136      */
137     $records = $app->db->queryAllRecords("SELECT DISTINCT type FROM monitor_data WHERE server_id = " . $serverId);
138     foreach($records as $record){
139         _processDbState($record['type'], $serverId, &$serverState, &$messages);
140     }
141
142     $res .= '<div class="systemmonitor-state systemmonitor-state-' . $serverState . '">';
143     $res .= '<div class="systemmonitor-serverstate">';
144     $res .= '<div class="systemmonitor-state-' . $serverState . '-icon">';
145     $res .= 'Server: ' . $serverName . '<br />';
146     $res .= 'State: ' . $serverState . '<br />';
147     //        $res .= sizeof($messages['ok']) . ' ok | ';
148     $res .= sizeof($messages['unknown']) . ' unknown | ';
149     $res .= sizeof($messages['info']) . ' info | ';
150     $res .= sizeof($messages['warning']) . ' warning | ';
151     $res .= sizeof($messages['critical']) . ' critical | ';
152     $res .= sizeof($messages['error']) . ' error <br />';
153     $res .= '<br />';
154     if ($showAll){
155         /*
156          * if we have to show all, then we do it...
157          */
158
159         /*
160         * Show all messages
161         */
162         foreach($messages as $key => $state){
163             /*
164              * There is no need, to show the "ok" - messages
165              */
716255 166 //            if ($key != 'ok')
8793b3 167             {
V 168                 $res .= $key . ':<br />';
169                 foreach ($state as $msg)
170                 {
171                     $res .= $msg . '<br />';
172                 }
173                 $res .= '<br />';
174             }
175         }
176     }
177     else
178     {
179         /*
180          * if not, we only show a link to the server...
181          */
182         $res .= "<a href='#' onclick='loadContent(\"monitor/show_sys_state.php?state=server&server=" . $serverId . '|' . $serverName . "\");'> More information...</a>";
183     }
184     $res .= '</div>';
185     $res .= '</div>';
186     $res .= '</div>';
187
188     if ($showAll){
189         /*
190          * Show some state-info
191          */
36d307 192         //$res .= showServerLoad();
V 193         //$res .= '&nbsp;'. showDiskUsage();
194         //$res .= '&nbsp;'.showServices();
8793b3 195     }
V 196
197
198     return $res;
199 }
200
201 /*
202  * gets the state from the db and process it
203  */
204 function _processDbState($type, $serverId, &$serverState, &$messages)
205 {
206     global $app;
207
208    /*
209     * Always the NEWEST record of each monitoring is responsible for the
210     * state
211     */
212     // get the State from the DB
213     $record = $app->db->queryOneRecord("SELECT state FROM monitor_data WHERE type = '" . $type . "' and server_id = " . $serverId . " order by created desc");
214     // change the new state to the highest state
215     $serverState = _setState($serverState, $record['state']);
216     // count the states
217     $count[$record['state']]+= 1;
218
219     /*
220      * The message depands on the type and the state
221      */
222     if ($type == 'cpu_info'){
223         /* this type has no state */
224     }
225     if ($type == 'disk_usage'){
226         switch ($record['state']) {
227             case 'ok':
228                 $messages['ok'][] = 'The state of your Hard-Disk space is ok ' .
229                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
230                 break;
231             case 'info':
232                 $messages['info'][] = 'Your Hard-Disk space is going full ' .
233                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
234                 break;
235             case 'warning':
236                 $messages['warning'][] = 'Your Hard-Disk is nearly full ' .
237                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
238                 break;
239             case 'critical':
240                 $messages['critical'][] = 'Your Hard-Disk is very full '.
241                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
242                 break;
243             case 'error':
244                 $messages['error'][] = 'Your Hard-Disk has no more space left ' .
245                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
246                 break;
247
248             default:
249                 $messages['unknown'][] = 'Hard-Disk: ??? ' .
250                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
251                 break;
252         }
253     }
254     if ($type == 'mem_usage'){
255         /* this type has no state */
256     }
257     if ($type == 'server_load'){
258         switch ($record['state']) {
259             case 'ok':
260                 $messages['ok'][] = 'Your Server load is ok ' .
261                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
262                 break;
263             case 'info':
264                 $messages['info'][] = 'Your Server in under heavy load ' .
265                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
266                 break;
267             case 'warning':
268                 $messages['warning'][] = 'Your Server in under high load ' .
269                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
270                 break;
271             case 'critical':
272                 $messages['critical'][] = 'Your Server in under higher load ' .
273                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
274                 break;
275             case 'error':
276                 $messages['error'][] = 'Your Server in under highest load ' .
277                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
278                 break;
279             default:
280                 $messages['unknown'][] = 'Server Load: ??? ' .
281                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
282                 break;
283         }
284     }
285     if ($type == 'services'){
286         switch ($record['state']) {
287             case 'ok':
a2e689 288                 $messages['ok'][] = 'All needed Services are online ' .
8793b3 289                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";
V 290
291                 break;
292             case 'error':
293                 $messages['error'][] = 'One or more needed Services are offline ' .
294                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";
295                 break;
296             default:
297                 $messages['unknown'][] = 'services:??? ' .
298                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";
299                 break;
300         }
301     }
716255 302     if ($type == 'system_update'){
V 303         switch ($record['state']) {
304             case 'ok':
305                 $messages['ok'][] = 'Your System is up to date. ' .
306                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
307
308                 break;
309             case 'warning':
310                 $messages['warning'][] = 'One or more Components needs a update ' .
311                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
312                 break;
36d307 313             case 'no_state':
V 314                 /*
315                  *  not debian and not Ubuntu, so the state could not be monitored...
316                  */
317                 break;
716255 318             default:
36d307 319                 $messages['unknown'][] = 'System-Update:??? ' .
716255 320                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
V 321                 break;
322         }
323     }
324
36d307 325     if ($type == 'raid_state'){
V 326         switch ($record['state']) {
327             case 'ok':
328                 $messages['ok'][] = 'Your RAID is ok ' .
329                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=raid_state\");'>[more...]</a>";
330                 break;
331             case 'info':
332                 $messages['info'][] = 'Your RAID is in RESYNC mode ' .
333                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=raid_state\");'>[more...]</a>";
334                 break;
335             case 'critical':
336                 $messages['critical'][] = 'Your RAID has one FAULT disk. Replace as soon as possible! '.
337                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=raid_state\");'>[more...]</a>";
338                 break;
339             case 'error':
340                 $messages['error'][] = 'Your RAID is not working anymore ' .
341                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=raid_state\");'>[more...]</a>";
342                 break;
343             case 'no_state':
344                 /*
345                  *  mdadm is not installed or the RAID is not supported...
346                  */
347                 break;
348             default:
349                 $messages['unknown'][] = 'RAID state: ??? ' .
350                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=raid_state\");'>[more...]</a>";
351                 break;
352         }
353     }
354
355
716255 356     if ($type == 'mailq'){
V 357         switch ($record['state']) {
358             case 'ok':
359                 $messages['ok'][] = 'Your Mailq load is ok ' .
360                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
361                 break;
362             case 'info':
363                 $messages['info'][] = 'Your Mailq in under heavy load ' .
364                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
365                 break;
366             case 'warning':
367                 $messages['warning'][] = 'Your Mailq in under high load ' .
368                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
369                 break;
370             case 'critical':
371                 $messages['critical'][] = 'Your Mailq in under higher load ' .
372                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
373                 break;
374             case 'error':
375                 $messages['error'][] = 'Your Mailq in under highest load ' .
376                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
377                 break;
378             default:
379                 $messages['unknown'][] = 'Mailq: ??? ' .
380                                     "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
381                 break;
382         }
383     }
8793b3 384     if ($type == 'log_clamav'){
V 385         /* this type has no state */
386     }
387     if ($type == 'log_freshclam'){
fb4c27 388         switch ($record['state']) {
V 389             case 'ok':
390                 $messages['ok'][] = 'Your Virus-protection is ok ' .
391                                     "<a href='#' onclick='loadContent(\"monitor/show_log.php?log=log_freshclam\");'>[more...]</a>";
392                 break;
393             case 'warning':
394                 $messages['warning'][] = 'Your Virus-protection is OUTDATED! ' .
395                                     "<a href='#' onclick='loadContent(\"monitor/show_log.php?log=log_freshclam\");'>[more...]</a>";
396                 break;
397             default:
398                 $messages['unknown'][] = 'Freshclam: ???! ' .
399                                     "<a href='#' onclick='loadContent(\"monitor/show_log.php?log=log_freshclam\");'>[more...]</a>";
400                 break;
401         }
8793b3 402     }
V 403     if ($type == 'log_ispconfig'){
404         /* this type has no state */
405     }
406     if ($type == 'log_mail'){
407         /* this type has no state */
408     }
409     if ($type == 'log_mail_err'){
410         /* this type has no state */
411     }
412     if ($type == 'log_mail_warn'){
413         /* this type has no state */
414     }
415     if ($type == 'log_messages'){
416         /* this type has no state */
417     }
fb4c27 418     if ($type == 'rkhunter'){
V 419         /* this type has no state */
420     }
8793b3 421 }
V 422
423  /*
424   * Set the state to the given level (or higher, but not lesser).
425   * * If the actual state is critical and you call the method with ok,
426   *   then the state is critical.
427   *
428   * * If the actual state is critical and you call the method with error,
429   *   then the state is error.
430   */
431 function _setState($oldState, $newState)
432 {
716255 433    /*
V 434     * Calculate the weight of the old state
435     */
8793b3 436     switch ($oldState) {
V 437         case 'no_state': $oldInt = 0;
438             break;
a2e689 439         case 'ok': $oldInt = 1;
8793b3 440             break;
a2e689 441         case 'unknown': $oldInt = 2;
8793b3 442             break;
V 443         case 'info': $oldInt = 3;
444             break;
445         case 'warning': $oldInt = 4;
446             break;
447         case 'critical': $oldInt = 5;
448             break;
449         case 'error': $oldInt = 6;
450             break;
451     }
452         /*
453          * Calculate the weight of the new state
454          */
455     switch ($newState) {
456         case 'no_state': $newInt = 0 ;
457             break;
458         case 'ok': $newInt = 1 ;
459             break;
460         case 'unknown': $newInt = 2 ;
461             break;
462         case 'info': $newInt = 3 ;
463             break;
464         case 'warning': $newInt = 4 ;
465             break;
466         case 'critical': $newInt = 5 ;
467             break;
468         case 'error': $newInt = 6 ;
469             break;
470     }
471
716255 472    /*
V 473     * Set to the higher level
474     */
8793b3 475     if ($newInt > $oldInt){
V 476         return $newState;
477     }
478     else
479     {
480         return $oldState;
481     }
482 }
483
484 ?>