show_sys_state.php 12.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
Copyright (c) 2007-2008, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

require_once('../../lib/config.inc.php');
require_once('../../lib/app.inc.php');
require_once('tools.inc.php');

/* Check permissions for module */
$app->auth->check_module_permissions('monitor');

/* Change the Server if needed */
if (isset($_GET['server'])){
    $server = explode('|', $_GET['server'], 2);
    $_SESSION['monitor']['server_id'] = $server[0];
    $_SESSION['monitor']['server_name'] = $server[1];
}

/*
 *  Loading the template
 */
$app->uses('tpl');
$app->tpl->newTemplate("form.tpl.htm");
$app->tpl->setInclude('content_tpl','templates/show_sys_state.htm');

/*
 * setting the content
 */
if ($_GET['state'] == 'server')
{
    $output = _getServerState($_SESSION['monitor']['server_id'], $_SESSION['monitor']['server_name'], true);
    $title = "Server State";
}
else
{
    $output = _getSysState();
    $title = "System State";
}

$app->tpl->setVar("state_data",$output);
$app->tpl->setVar("title",$title);
$app->tpl->setVar("description",$description);

/*
 * doing the output
 */
$app->tpl_defaults();
$app->tpl->pparse();


function _getSysState(){
    global $app;

    /*
     * Get all Servers and calculate the state of them
     */
    $html = '';

    $servers = $app->db->queryAllRecords("SELECT server_id, server_name FROM server order by server_name");
    foreach ($servers as $server)
    {
        $html .= _getServerState($server['server_id'], $server['server_name'], false);
    }

    return $html;
}

/*
 * Calculates the State of ONE Server
 */
function _getServerState($serverId, $serverName, $showAll)
{
    global $app;

    /*  The State of the server */
101
    $serverState = 'ok';
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

    /** The Number of several infos, warnings, errors, ... */
    $count = array('unknown' => 0, 'info' => 0, 'warning' => 0, 'critical' => 0, 'error' => 0);

    /** The messages */
    $messages = array();

    /** The Result of the function */
    $res = '';

    /*
     * get all monitoring-data from the server als process then
     * (count them and set the server-state)
     */
    $records = $app->db->queryAllRecords("SELECT DISTINCT type FROM monitor_data WHERE server_id = " . $serverId);
    foreach($records as $record){
        _processDbState($record['type'], $serverId, &$serverState, &$messages);
    }

    $res .= '<div class="systemmonitor-state systemmonitor-state-' . $serverState . '">';
    $res .= '<div class="systemmonitor-serverstate">';
    $res .= '<div class="systemmonitor-state-' . $serverState . '-icon">';
    $res .= 'Server: ' . $serverName . '<br />';
    $res .= 'State: ' . $serverState . '<br />';
    //        $res .= sizeof($messages['ok']) . ' ok | ';
    $res .= sizeof($messages['unknown']) . ' unknown | ';
    $res .= sizeof($messages['info']) . ' info | ';
    $res .= sizeof($messages['warning']) . ' warning | ';
    $res .= sizeof($messages['critical']) . ' critical | ';
    $res .= sizeof($messages['error']) . ' error <br />';
    $res .= '<br />';
    if ($showAll){
        /*
         * if we have to show all, then we do it...
         */

        /*
        * Show all messages
        */
        foreach($messages as $key => $state){
            /*
             * There is no need, to show the "ok" - messages
             */
            if ($key != 'ok')
            {
                $res .= $key . ':<br />';
                foreach ($state as $msg)
                {
                    $res .= $msg . '<br />';
                }
                $res .= '<br />';
            }
        }
    }
    else
    {
        /*
         * if not, we only show a link to the server...
         */
        $res .= "<a href='#' onclick='loadContent(\"monitor/show_sys_state.php?state=server&server=" . $serverId . '|' . $serverName . "\");'> More information...</a>";
    }
    $res .= '</div>';
    $res .= '</div>';
    $res .= '</div>';

    if ($showAll){
        /*
         * Show some state-info
         */
        $res .= showServerLoad();
        $res .= '&nbsp;'. showDiskUsage();
        $res .= '&nbsp;'.showServices();
    }


    return $res;
}

/*
 * gets the state from the db and process it
 */
function _processDbState($type, $serverId, &$serverState, &$messages)
{
    global $app;

   /*
    * Always the NEWEST record of each monitoring is responsible for the
    * state
    */
    // get the State from the DB
    $record = $app->db->queryOneRecord("SELECT state FROM monitor_data WHERE type = '" . $type . "' and server_id = " . $serverId . " order by created desc");
    // change the new state to the highest state
    $serverState = _setState($serverState, $record['state']);
    // count the states
    $count[$record['state']]+= 1;

    /*
     * The message depands on the type and the state
     */
    if ($type == 'cpu_info'){
        /* this type has no state */
    }
    if ($type == 'disk_usage'){
        switch ($record['state']) {
            case 'ok':
                $messages['ok'][] = 'The state of your Hard-Disk space is ok ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;
            case 'info':
                $messages['info'][] = 'Your Hard-Disk space is going full ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;
            case 'warning':
                $messages['warning'][] = 'Your Hard-Disk is nearly full ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;
            case 'critical':
                $messages['critical'][] = 'Your Hard-Disk is very full '.
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;
            case 'error':
                $messages['error'][] = 'Your Hard-Disk has no more space left ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;

            default:
                $messages['unknown'][] = 'Hard-Disk: ??? ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=disk_usage\");'>[more...]</a>";
                break;
        }
    }
    if ($type == 'mem_usage'){
        /* this type has no state */
    }
    if ($type == 'server_load'){
        switch ($record['state']) {
            case 'ok':
                $messages['ok'][] = 'Your Server load is ok ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
            case 'info':
                $messages['info'][] = 'Your Server in under heavy load ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
            case 'warning':
                $messages['warning'][] = 'Your Server in under high load ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
            case 'critical':
                $messages['critical'][] = 'Your Server in under higher load ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
            case 'error':
                $messages['error'][] = 'Your Server in under highest load ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
            default:
                $messages['unknown'][] = 'Server Load: ??? ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=server_load\");'>[more...]</a>";
                break;
        }
    }
    if ($type == 'services'){
        switch ($record['state']) {
            case 'ok':
267
                $messages['ok'][] = 'All needed Services are online ' .
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";

                break;
            case 'error':
                $messages['error'][] = 'One or more needed Services are offline ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";
                break;
            default:
                $messages['unknown'][] = 'services:??? ' .
                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=services\");'>[more...]</a>";
                break;
        }
    }
    if ($type == 'log_clamav'){
        /* this type has no state */
    }
    if ($type == 'log_freshclam'){
        /* this type has no state */
    }
    if ($type == 'log_ispconfig'){
        /* this type has no state */
    }
    if ($type == 'log_mail'){
        /* this type has no state */
    }
    if ($type == 'log_mail_err'){
        /* this type has no state */
    }
    if ($type == 'log_mail_warn'){
        /* this type has no state */
    }
    if ($type == 'log_messages'){
        /* this type has no state */
    }
}

 /*
  * Set the state to the given level (or higher, but not lesser).
  * * If the actual state is critical and you call the method with ok,
  *   then the state is critical.
  *
  * * If the actual state is critical and you call the method with error,
  *   then the state is error.
  */
function _setState($oldState, $newState)
{
        /*
         * Calculate the weight of the old state
         */
    switch ($oldState) {
        case 'no_state': $oldInt = 0;
            break;
320
        case 'ok': $oldInt = 1;
321
            break;
322
        case 'unknown': $oldInt = 2;
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
            break;
        case 'info': $oldInt = 3;
            break;
        case 'warning': $oldInt = 4;
            break;
        case 'critical': $oldInt = 5;
            break;
        case 'error': $oldInt = 6;
            break;
    }
        /*
         * Calculate the weight of the new state
         */
    switch ($newState) {
        case 'no_state': $newInt = 0 ;
            break;
        case 'ok': $newInt = 1 ;
            break;
        case 'unknown': $newInt = 2 ;
            break;
        case 'info': $newInt = 3 ;
            break;
        case 'warning': $newInt = 4 ;
            break;
        case 'critical': $newInt = 5 ;
            break;
        case 'error': $newInt = 6 ;
            break;
    }

        /*
         * Set to the higher level
         */
    if ($newInt > $oldInt){
        return $newState;
    }
    else
    {
        return $oldState;
    }
}

?>