tbrehm
2011-10-20 d2fbc5f9de0b802088b464fa4276416a581994bc
commit | author | age
dead5c 1 <?php
V 2
3 /*
4   Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
5   All rights reserved.
6
7   Redistribution and use in source and binary forms, with or without modification,
8   are permitted provided that the following conditions are met:
9
10  * Redistributions of source code must retain the above copyright notice,
11   this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice,
13   this list of conditions and the following disclaimer in the documentation
14   and/or other materials provided with the distribution.
15  * Neither the name of ISPConfig nor the names of its contributors
16   may be used to endorse or promote products derived from this software without
17   specific prior written permission.
18
19   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 class monitor_tools {
32
33     //** Get distribution identifier
34     //** IMPORTANT!
35     //   This is the same code as in install/lib/install.lib.php
36     //   So if you change it here, you also have to change it in there!
37     //   Please do not forget to remove the swriteln(); - lines here at this file
38     public function get_distname() {
39
40         $distname = '';
41         $distver = '';
42         $distid = '';
43         $distbaseid = '';
44
45         //** Debian or Ubuntu
46         if (file_exists('/etc/debian_version')) {
47
48             if (trim(file_get_contents('/etc/debian_version')) == '4.0') {
49                 $distname = 'Debian';
50                 $distver = '4.0';
51                 $distid = 'debian40';
52                 $distbaseid = 'debian';
53             } elseif (strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
54                 $distname = 'Debian';
55                 $distver = 'Lenny';
56                 $distid = 'debian40';
57                 $distbaseid = 'debian';
58             } elseif (strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
59                 $distname = 'Debian';
60                 $distver = 'Squeeze/Sid';
61                 $distid = 'debian60';
62                 $distbaseid = 'debian';
63             } else {
64                 $distname = 'Debian';
65                 $distver = 'Unknown';
66                 $distid = 'debian40';
67                 $distbaseid = 'debian';
68             }
69         }
70
71         //** OpenSuSE
72         elseif (file_exists('/etc/SuSE-release')) {
73             if (stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
74                 $distname = 'openSUSE';
75                 $distver = '11.0';
76                 $distid = 'opensuse110';
77                 $distbaseid = 'opensuse';
78             } elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
79                 $distname = 'openSUSE';
80                 $distver = '11.1';
81                 $distid = 'opensuse110';
82                 $distbaseid = 'opensuse';
83             } elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
84                 $distname = 'openSUSE';
85                 $distver = '11.1';
86                 $distid = 'opensuse110';
87                 $distbaseid = 'opensuse';
88             } else {
89                 $distname = 'openSUSE';
90                 $distver = 'Unknown';
91                 $distid = 'opensuse110';
92                 $distbaseid = 'opensuse';
93             }
94         }
95
96
97         //** Redhat
98         elseif (file_exists('/etc/redhat-release')) {
99
100             $content = file_get_contents('/etc/redhat-release');
101
102             if (stristr($content, 'Fedora release 9 (Sulphur)')) {
103                 $distname = 'Fedora';
104                 $distver = '9';
105                 $distid = 'fedora9';
106                 $distbaseid = 'fedora';
107             } elseif (stristr($content, 'Fedora release 10 (Cambridge)')) {
108                 $distname = 'Fedora';
109                 $distver = '10';
110                 $distid = 'fedora9';
111                 $distbaseid = 'fedora';
112             } elseif (stristr($content, 'Fedora release 10')) {
113                 $distname = 'Fedora';
114                 $distver = '11';
115                 $distid = 'fedora9';
116                 $distbaseid = 'fedora';
117             } elseif (stristr($content, 'CentOS release 5.2 (Final)')) {
118                 $distname = 'CentOS';
119                 $distver = '5.2';
120                 $distid = 'centos52';
121                 $distbaseid = 'fedora';
122             } elseif (stristr($content, 'CentOS release 5.3 (Final)')) {
123                 $distname = 'CentOS';
124                 $distver = '5.3';
125                 $distid = 'centos53';
126                 $distbaseid = 'fedora';
127             } else {
128                 $distname = 'Redhat';
129                 $distver = 'Unknown';
130                 $distid = 'fedora9';
131                 $distbaseid = 'fedora';
132             }
133         }
134
135         //** Gentoo
136         elseif (file_exists('/etc/gentoo-release')) {
137
138             $content = file_get_contents('/etc/gentoo-release');
139
140             preg_match_all('/([0-9]{1,2})/', $content, $version);
141             $distname = 'Gentoo';
142             $distver = $version[0][0] . $version[0][1];
143             $distid = 'gentoo';
144             $distbaseid = 'gentoo';
145         } else {
146             die('Unrecognized GNU/Linux distribution');
147         }
148
149         return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
150     }
151
7cd997 152     //** Email Quota
L 153     public function monitorEmailQuota() {
27179f 154         global $conf, $app;
7cd997 155
L 156         //* Initialize data array
157         $data = array();
158
159         //* the id of the server as int
160         $server_id = intval($conf['server_id']);
161
162         //* The type of the data
163         $type = 'email_quota';
164
165         //* The state of the email_quota.
166         $state = 'ok';
91c381 167         
T 168         $mailboxes = $app->db->queryAllRecords("SELECT email FROM mail_user WHERE server_id = $server_id");
169         if(is_array($mailboxes)) {
170             foreach($mailboxes as $mb) {
171                 $email = $mb['email'];
172                 $email_parts = explode('@',$mb['email']);
173                 $filename = '/var/vmail/'.$email_parts[1].'/'.$email_parts[0].'/.quotausage';
174                 if(file_exists($filename)) {
175                     $quotafile = file($filename);
176                     $data[$email]['used'] = trim($quotafile['1']);
177                     unset($quotafile);
178                 }   
179             }
180         }
181         
182         unset($mailboxes);
7cd997 183
L 184         //* Dovecot quota check Courier in progress lathama@gmail.com
91c381 185         /*
7cd997 186         if($dir = opendir("/var/vmail")){
L 187             while (($quotafiles = readdir($dir)) !== false){
188                 if(preg_match('/.\_quota$/', $quotafiles)){
189                     $quotafile = (file("/var/vmail/" . $quotafiles));
190                     $emailaddress = preg_replace('/_quota/',"", $quotafiles);
191                     $emailaddress = preg_replace('/_/',"@", $emailaddress);
192                     $data[$emailaddress]['used'] = trim($quotafile['1']);
193                 }
194             }
195             closedir($dir);
196         }
91c381 197         */
7cd997 198         $res['server_id'] = $server_id;
L 199         $res['type'] = $type;
200         $res['data'] = $data;
201         $res['state'] = $state;
202         return $res;
203     }
204
205     //** Filesystem Quota
dead5c 206     public function monitorHDQuota() {
V 207         global $conf;
208
7cd997 209         //* Initialize data array
dead5c 210         $data = array();
V 211
7cd997 212         //* the id of the server as int
dead5c 213         $server_id = intval($conf['server_id']);
V 214
7cd997 215         //* The type of the data
dead5c 216         $type = 'harddisk_quota';
V 217
7cd997 218         //* The state of the harddisk_quota.
dead5c 219         $state = 'ok';
V 220
7cd997 221         //* Fetch the data for all users
dead5c 222         $dfData = shell_exec('repquota -au');
V 223
7cd997 224         //* Split into array
dead5c 225         $df = explode("\n", $dfData);
V 226
7cd997 227         //* ignore the first 5 lines, process the rest
dead5c 228         for ($i = 5; $i <= sizeof($df); $i++) {
V 229             if ($df[$i] != '') {
7cd997 230                 //* Make a array of the data
dead5c 231                 $s = preg_split('/[\s]+/', $df[$i]);
V 232                 $username = $s[0];
233                 if (substr($username, 0, 3) == 'web') {
234                     if (isset($data['user'][$username])) {
235                         $data['user'][$username]['used'] += $s[2];
236                         $data['user'][$username]['soft'] += $s[3];
237                         $data['user'][$username]['hard'] += $s[4];
238                     } else {
239                         $data['user'][$username]['used'] = $s[2];
240                         $data['user'][$username]['soft'] = $s[3];
241                         $data['user'][$username]['hard'] = $s[4];
242                     }
243                 }
244             }
245         }
246
7cd997 247         //** Fetch the data for all users
dead5c 248         $dfData = shell_exec('repquota -ag');
V 249
7cd997 250         //* split into array
dead5c 251         $df = explode("\n", $dfData);
V 252
7cd997 253         //* ignore the first 5 lines, process the rest
dead5c 254         for ($i = 5; $i <= sizeof($df); $i++) {
V 255             if ($df[$i] != '') {
7cd997 256                 //* Make a array of the data
dead5c 257                 $s = preg_split('/[\s]+/', $df[$i]);
V 258                 $groupname = $s[0];
259                 if (substr($groupname, 0, 6) == 'client') {
260                     if (isset($data['group'][$groupname])) {
261                         $data['group'][$groupname]['used'] += $s[1];
262                         $data['group'][$groupname]['soft'] += $s[2];
263                         $data['group'][$groupname]['hard'] += $s[3];
264                     } else {
265                         $data['group'][$groupname]['used'] = $s[1];
266                         $data['group'][$groupname]['soft'] = $s[2];
267                         $data['group'][$groupname]['hard'] = $s[3];
268                     }
269                 }
270             }
271         }
272
7cd997 273         //* Return the Result
dead5c 274         $res['server_id'] = $server_id;
V 275         $res['type'] = $type;
276         $res['data'] = $data;
277         $res['state'] = $state;
278         return $res;
279     }
280
281     public function monitorServer() {
282         global $conf;
283
284         /* the id of the server as int */
285         $server_id = intval($conf['server_id']);
286
287         /** The type of the data */
288         $type = 'server_load';
289
290         /*
291           Fetch the data into a array
292          */
293         $procUptime = shell_exec("cat /proc/uptime | cut -f1 -d' '");
294         $data['up_days'] = floor($procUptime / 86400);
295         $data['up_hours'] = floor(($procUptime - $data['up_days'] * 86400) / 3600);
296         $data['up_minutes'] = floor(($procUptime - $data['up_days'] * 86400 - $data['up_hours'] * 3600) / 60);
297
298         $data['uptime'] = shell_exec('uptime');
299
300         $tmp = explode(',', $data['uptime'], 4);
301         $tmpUser = explode(' ', trim($tmp[2]));
302         $data['user_online'] = intval($tmpUser[0]);
303
304         //* New Load Average code to fix "always zero" bug in non-english distros. NEEDS TESTING
305         $loadTmp = shell_exec("cat /proc/loadavg | cut -f1-3 -d' '");
306         $load = explode(' ', $loadTmp);
307         $data['load_1'] = floatval(str_replace(',', '.', $load[0]));
308         $data['load_5'] = floatval(str_replace(',', '.', $load[1]));
309         $data['load_15'] = floatval(str_replace(',', '.', $load[2]));
310
311         /** The state of the server-load. */
312         $state = 'ok';
313         if ($data['load_1'] > 20)
314             $state = 'info';
315         if ($data['load_1'] > 50)
316             $state = 'warning';
317         if ($data['load_1'] > 100)
318             $state = 'critical';
319         if ($data['load_1'] > 150)
320             $state = 'error';
321
322         /*
323          * Return the Result
324          */
325         $res['server_id'] = $server_id;
326         $res['type'] = $type;
327         $res['data'] = $data;
328         $res['state'] = $state;
329         return $res;
330     }
331
332     public function monitorOsVer() {
333         global $conf;
334
335         /* the id of the server as int */
336         $server_id = intval($conf['server_id']);
337
338         /** The type of the data */
339         $type = 'os_info';
340
341         /*
342           Fetch the data into a array
343          */
344         $dist = $this->get_distname();
345
346         $data['name'] = $dist['name'];
347         $data['version'] = $dist['version'];
348
349         /* the OS has no state. It is, what it is */
350         $state = 'no_state';
351
352         /*
353          * Return the Result
354          */
355         $res['server_id'] = $server_id;
356         $res['type'] = $type;
357         $res['data'] = $data;
358         $res['state'] = $state;
359         return $res;
360     }
361
362     public function monitorIspcVer() {
363         global $conf;
364
365         /* the id of the server as int */
366         $server_id = intval($conf['server_id']);
367
368         /** The type of the data */
369         $type = 'ispc_info';
370
371         /*
372           Fetch the data into a array
373          */
374         $data['name'] = ISPC_APP_TITLE;
375         $data['version'] = ISPC_APP_VERSION;
376
377         /* the ISPC-Version has no state. It is, what it is */
378         $state = 'no_state';
def897 379
dead5c 380         /*
V 381          * Return the Result
382          */
383         $res['server_id'] = $server_id;
384         $res['type'] = $type;
385         $res['data'] = $data;
386         $res['state'] = $state;
387         return $res;
388     }
389
390     public function monitorDiskUsage() {
391         global $conf;
392
393         /* the id of the server as int */
394         $server_id = intval($conf['server_id']);
395
396         /** The type of the data */
397         $type = 'disk_usage';
398
399         /** The state of the disk-usage */
400         $state = 'ok';
401
402         /** Fetch the data of ALL devices into a array (needed for monitoring!) */
403         $dfData = shell_exec('df -hT');
404
405         // split into array
406         $df = explode("\n", $dfData);
407
408         /*
409          * ignore the first line, process the rest
410          */
411         for ($i = 1; $i <= sizeof($df); $i++) {
412             if ($df[$i] != '') {
413                 /*
414                  * Make an array of the data
415                  */
416                 $s = preg_split('/[\s]+/', $df[$i]);
417                 $data[$i]['fs'] = $s[0];
418                 $data[$i]['type'] = $s[1];
419                 $data[$i]['size'] = $s[2];
420                 $data[$i]['used'] = $s[3];
421                 $data[$i]['available'] = $s[4];
422                 $data[$i]['percent'] = $s[5];
423                 $data[$i]['mounted'] = $s[6];
424                 /*
425                  * calculate the state
426                  */
427                 $usePercent = floatval($data[$i]['percent']);
9c9382 428                 
T 429                 //* get the free memsize
430                 if(substr($data[$i]['available'],-1) == 'G') {
431                     $freesize = floatval($data[$i]['available'])*1024;
432                 } elseif(substr($data[$i]['available'],-1) == 'T') {
433                     $freesize = floatval($data[$i]['available'])*1024*1024;
434                 } else {
435                     $freesize = floatval($data[$i]['available']);
436                 }
dead5c 437
V 438                 //* We don't want to check some filesystem which have no sensible filling levels
439                 switch ($data[$i]['type']) {
440                     case 'iso9660':
441                     case 'cramfs':
442                     case 'udf':
443                     case 'tmpfs':
444                     case 'devtmpfs':
445                     case 'udev':
446                         break;
447                     default:
9c9382 448                         if ($usePercent > 75 && $freesize < 2000)
dead5c 449                             $state = $this->_setState($state, 'info');
9c9382 450                         if ($usePercent > 80 && $freesize < 1000)
dead5c 451                             $state = $this->_setState($state, 'warning');
9c9382 452                         if ($usePercent > 90 && $freesize < 500)
dead5c 453                             $state = $this->_setState($state, 'critical');
9c9382 454                         if ($usePercent > 95 && $freesize < 100)
dead5c 455                             $state = $this->_setState($state, 'error');
V 456                         break;
457                 }
458             }
459         }
460
461         /*
462          * Return the Result
463          */
464         $res['server_id'] = $server_id;
465         $res['type'] = $type;
466         $res['data'] = $data;
467         $res['state'] = $state;
468         return $res;
469     }
470
471     public function monitorMemUsage() {
472         global $conf;
473
474         /* the id of the server as int */
475         $server_id = intval($conf['server_id']);
476
477         /** The type of the data */
478         $type = 'mem_usage';
479
480         /*
481           Fetch the data into a array
482          */
483         $miData = shell_exec('cat /proc/meminfo');
484
485         $memInfo = explode("\n", $miData);
486
487         foreach ($memInfo as $line) {
488             $part = preg_split('/:/', $line);
489             $key = trim($part[0]);
490             $tmp = explode(' ', trim($part[1]));
491             $value = 0;
492             if ($tmp[1] == 'kB')
493                 $value = $tmp[0] * 1024;
494             $data[$key] = $value;
495         }
496
497         /*
498          * actually this info has no state.
499          * maybe someone knows better...???...
500          */
501         $state = 'no_state';
def897 502
dead5c 503         /*
V 504          * Return the Result
505          */
506         $res['server_id'] = $server_id;
507         $res['type'] = $type;
508         $res['data'] = $data;
509         $res['state'] = $state;
510         return $res;
511     }
512
513     public function monitorCpu() {
514         global $conf;
515
516         /* the id of the server as int */
517         $server_id = intval($conf['server_id']);
518
519         /** The type of the data */
520         $type = 'cpu_info';
521
522         /*
523           Fetch the data into a array
524          */
525         if (file_exists('/proc/cpuinfo')) {
526             $cpuData = shell_exec('cat /proc/cpuinfo');
527             $cpuInfo = explode("\n", $cpuData);
528             $processor = 0;
529
530             foreach ($cpuInfo as $line) {
531
532                 $part = preg_split('/:/', $line);
533                 $key = trim($part[0]);
534                 $value = trim($part[1]);
535                 if ($key == 'processor')
536                     $processor = intval($value);
537                 if ($key != '')
538                     $data[$key . ' ' . $processor] = $value;
539             }
540
541             /* the cpu has no state. It is, what it is */
542             $state = 'no_state';
543         } else {
544             /*
545              * It is not Linux, so there is no data and no state
546              *
547              * no_state, NOT unknown, because "unknown" is shown as state
548              * inside the GUI. no_state is hidden.
549              *
550              * We have to write NO DATA inside the DB, because the GUI
551              * could not know, if there is any dat, or not...
552              */
553             $state = 'no_state';
554             $data['output'] = '';
555         }
556
557         /*
558          * Return the Result
559          */
560         $res['server_id'] = $server_id;
561         $res['type'] = $type;
562         $res['data'] = $data;
563         $res['state'] = $state;
564         return $res;
565     }
566
567     public function monitorServices() {
568         global $app;
569         global $conf;
570
571         /** the id of the server as int */
572         $server_id = intval($conf['server_id']);
573
eed6b3 574         /**  get the "active" Services of the server from the DB */
039b46 575         $services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ' . $server_id);
eed6b3 576         /*
V 577          * If the DB is down, we have to set the db to "yes".
578          * If we don't do this, then the monitor will NOT monitor, that the db is down and so the
579          * rescue-module can not try to rescue the db
580          */
581         if ($services == null) {
582             $services['db_server'] = 1;
583         }
dead5c 584
V 585         /* The type of the Monitor-data */
586         $type = 'services';
587
588         /** the State of the monitoring */
589         /* ok, if ALL active services are running,
590          * error, if not
591          * There is no other state!
592          */
593         $state = 'ok';
594
595         /* Monitor Webserver */
596         $data['webserver'] = -1; // unknown - not needed
597         if ($services['web_server'] == 1) {
598             if ($this->_checkTcp('localhost', 80)) {
599                 $data['webserver'] = 1;
600             } else {
601                 $data['webserver'] = 0;
602                 $state = 'error'; // because service is down
603             }
604         }
605
606         /* Monitor FTP-Server */
607         $data['ftpserver'] = -1; // unknown - not needed
608         if ($services['file_server'] == 1) {
609             if ($this->_checkFtp('localhost', 21)) {
610                 $data['ftpserver'] = 1;
611             } else {
612                 $data['ftpserver'] = 0;
613                 $state = 'error'; // because service is down
614             }
615         }
616
617         /* Monitor SMTP-Server */
618         $data['smtpserver'] = -1; // unknown - not needed
619         if ($services['mail_server'] == 1) {
620             if ($this->_checkTcp('localhost', 25)) {
621                 $data['smtpserver'] = 1;
622             } else {
623                 $data['smtpserver'] = 0;
624                 $state = 'error'; // because service is down
625             }
626         }
627
628         /* Monitor POP3-Server */
629         $data['pop3server'] = -1; // unknown - not needed
630         if ($services['mail_server'] == 1) {
631             if ($this->_checkTcp('localhost', 110)) {
632                 $data['pop3server'] = 1;
633             } else {
634                 $data['pop3server'] = 0;
635                 $state = 'error'; // because service is down
636             }
637         }
638
639         /* Monitor IMAP-Server */
640         $data['imapserver'] = -1; // unknown - not needed
641         if ($services['mail_server'] == 1) {
642             if ($this->_checkTcp('localhost', 143)) {
643                 $data['imapserver'] = 1;
644             } else {
645                 $data['imapserver'] = 0;
646                 $state = 'error'; // because service is down
647             }
648         }
649
650         /* Monitor BIND-Server */
651         $data['bindserver'] = -1; // unknown - not needed
652         if ($services['dns_server'] == 1) {
dfcd55 653             if ($this->_checkUdp('localhost', 53)) {
dead5c 654                 $data['bindserver'] = 1;
V 655             } else {
656                 $data['bindserver'] = 0;
657                 $state = 'error'; // because service is down
658             }
659         }
660
661         /* Monitor MySQL Server */
662         $data['mysqlserver'] = -1; // unknown - not needed
663         if ($services['db_server'] == 1) {
664             if ($this->_checkTcp('localhost', 3306)) {
665                 $data['mysqlserver'] = 1;
666             } else {
667                 $data['mysqlserver'] = 0;
668                 $state = 'error'; // because service is down
669             }
670         }
def897 671
dead5c 672         /*
V 673          * Return the Result
674          */
675         $res['server_id'] = $server_id;
676         $res['type'] = $type;
677         $res['data'] = $data;
678         $res['state'] = $state;
679         return $res;
680     }
681
682     public function monitorOpenVzHost() {
683         global $app;
684         global $conf;
685
686         /* the id of the server as int */
687         $server_id = intval($conf['server_id']);
688
689         /** The type of the data */
690         $type = 'openvz_veinfo';
691
692         /*
693           Fetch the data into a array
694          */
695         $app->load(openvz_tools);
696         $openVzTools = new openvz_tools();
697         $data = $openVzTools->getOpenVzVeInfo();
698
699         /* the VE-Info has no state. It is, what it is */
700         $state = 'no_state';
701
702         /*
703          * Return the Result
704          */
705         $res['server_id'] = $server_id;
706         $res['type'] = $type;
707         $res['data'] = $data;
708         $res['state'] = $state;
709         return $res;
710     }
711
712     public function monitorOpenVzUserBeancounter() {
713         global $app;
714         global $conf;
715
716         /* the id of the server as int */
717         $server_id = intval($conf['server_id']);
718
719         /** The type of the data */
720         $type = 'openvz_beancounter';
721
722         /*
723           Fetch the data into a array
724          */
725         $app->load(openvz_tools);
726         $openVzTools = new openvz_tools();
727         $data = $openVzTools->getOpenVzVeBeanCounter();
728
729         /* calculate the state of the beancounter */
730         if ($data == '') {
731             $state = 'no_state';
732         } else {
733             $state = 'ok';
734
735             /* transfer this output-string into a array */
736             $test = explode("\n", $data);
737
738             /* the first list of the output is not needed */
739             array_shift($test);
740
741             /* now process all items of the rest */
742             foreach ($test as $item) {
743                 /*
744                  * eliminate all doubled spaces and spaces at the beginning and end
745                  */
746                 while (strpos($item, '  ') !== false) {
747                     $item = str_replace('  ', ' ', $item);
748                 }
749                 $item = trim($item);
750
751                 /*
752                  * The failcounter is the LAST
753                  */
754                 if ($item != '') {
755                     $tmp = explode(' ', $item);
756                     $failCounter = $tmp[sizeof($tmp) - 1];
757                     if ($failCounter > 0)
758                         $state = 'info';
759                     if ($failCounter > 50)
760                         $state = 'warning';
761                     if ($failCounter > 200)
762                         $state = 'critical';
763                     if ($failCounter > 10000)
764                         $state = 'error';
765                 }
766             }
767         }
768
769         /*
770          * Return the Result
771          */
772         $res['server_id'] = $server_id;
773         $res['type'] = $type;
774         $res['data'] = $data;
775         $res['state'] = $state;
776         return $res;
777     }
778
779     public function monitorSystemUpdate() {
780         global $conf;
781
782         /* the id of the server as int */
783         $server_id = intval($conf['server_id']);
784
785         /** The type of the data */
786         $type = 'system_update';
787
788         /* This monitoring is only available on Debian or Ubuntu */
789         if (file_exists('/etc/debian_version')) {
790
791             /*
792              * first update the "apt database"
793              */
794             shell_exec('apt-get update');
795
796             /*
797              * Then test the upgrade.
798              * if there is any output, then there is a needed update
799              */
800             $aptData = shell_exec('apt-get -s -qq dist-upgrade');
801             if ($aptData == '') {
802                 /* There is nothing to update! */
803                 $state = 'ok';
804             } else {
805                 /*
806                  * There is something to update! this is in most cases not critical, so we can
807                  * do a system-update once a month or so...
808                  */
809                 $state = 'info';
810             }
811
812             /*
813              * Fetch the output
814              */
815             $data['output'] = shell_exec('apt-get -s -q dist-upgrade');
816         } elseif (file_exists('/etc/gentoo-release')) {
817
818             /*
819              * first update the portage tree
820              */
821
822             // In keeping with gentoo's rsync policy, don't update to frequently (every four hours - taken from http://www.gentoo.org/doc/en/source_mirrors.xml)
823             $do_update = true;
824             if (file_exists('/usr/portage/metadata/timestamp.chk')) {
825                 $datetime = file_get_contents('/usr/portage/metadata/timestamp.chk');
826                 $datetime = trim($datetime);
827
828                 $dstamp = strtotime($datetime);
829                 if ($dstamp) {
830                     $checkat = $dstamp + 14400; // + 4hours
831                     if (mktime() < $checkat) {
832                         $do_update = false;
833                     }
834                 }
835             }
836
837             if ($do_update) {
838                 shell_exec('emerge --sync --quiet');
839             }
840
841             /*
842              * Then test the upgrade.
843              * if there is any output, then there is a needed update
844              */
845             $emergeData = shell_exec('glsa-check -t affected');
846             if ($emergeData == '') {
847                 /* There is nothing to update! */
848                 $state = 'ok';
849                 $data['output'] = 'No unapplied GLSA\'s found on the system.';
850             } else {
851                 /* There is something to update! */
852                 $state = 'info';
853                 $data['output'] = shell_exec('glsa-check -pv --nocolor affected 2>/dev/null');
854             }
def897 855         } elseif (file_exists('/etc/SuSE-release')) {
V 856
857             /*
858              * update and find the upgrade.
859              * if there is any output, then there is a needed update
860              */
861             $aptData = shell_exec('zypper -q lu');
862             if ($aptData == '') {
863                 /* There is nothing to update! */
864                 $state = 'ok';
865             } else {
866                 /*
867                  * There is something to update! this is in most cases not critical, so we can
868                  * do a system-update once a month or so...
869                  */
870                 $state = 'info';
871             }
872
873             /*
874              * Fetch the output
875              */
876             $data['output'] = shell_exec('zypper --non-interactive up');
dead5c 877         } else {
V 878             /*
879              * It is not Debian/Ubuntu, so there is no data and no state
880              *
881              * no_state, NOT unknown, because "unknown" is shown as state
882              * inside the GUI. no_state is hidden.
883              *
884              * We have to write NO DATA inside the DB, because the GUI
885              * could not know, if there is any dat, or not...
886              */
887             $state = 'no_state';
888             $data['output'] = '';
889         }
d2fbc5 890         
T 891         //* Encode data
892         $data['output'] = htmlentities($data['output']);
dead5c 893
V 894         /*
895          * Return the Result
896          */
897         $res['server_id'] = $server_id;
898         $res['type'] = $type;
899         $res['data'] = $data;
900         $res['state'] = $state;
901         return $res;
902     }
903
904     public function monitorMailQueue() {
905         global $conf;
906
907         /* the id of the server as int */
908         $server_id = intval($conf['server_id']);
909
910         /** The type of the data */
911         $type = 'mailq';
912
913         /* Get the data from the mailq */
914         $data['output'] = shell_exec('mailq');
915
916         /*
917          *  The last line has more informations
918          */
919         $tmp = explode("\n", $data['output']);
920         $more = $tmp[sizeof($tmp) - 1];
921         $this->_getIntArray($more);
922         $data['bytes'] = $res[0];
923         $data['requests'] = $res[1];
924
925         /** The state of the mailq. */
926         $state = 'ok';
927         if ($data['requests'] > 2000)
928             $state = 'info';
929         if ($data['requests'] > 5000)
930             $state = 'warning';
931         if ($data['requests'] > 8000)
932             $state = 'critical';
933         if ($data['requests'] > 10000)
934             $state = 'error';
935
936         /*
937          * Return the Result
938          */
939         $res['server_id'] = $server_id;
940         $res['type'] = $type;
941         $res['data'] = $data;
942         $res['state'] = $state;
943         return $res;
944     }
945
946     public function monitorRaid() {
947         global $conf;
948
949         /* the id of the server as int */
950         $server_id = intval($conf['server_id']);
951
952         /** The type of the data */
953         $type = 'raid_state';
954
955         /*
956          * We support several RAID types, but if we can't find any of them, we have no data
957          */
958         $state = 'no_state';
959         $data['output'] = '';
960
961         /*
962          * Check, if Software-RAID is enabled
963          */
964         if (file_exists('/proc/mdstat')) {
965             /*
966              * Fetch the output
967              */
968             $data['output'] = shell_exec('cat /proc/mdstat');
969
970             /*
971              * Then calc the state.
972              */
973             $tmp = explode("\n", $data['output']);
974             $state = 'ok';
975             for ($i = 0; $i < sizeof($tmp); $i++) {
976                 /* fetch the next line */
977                 $line = $tmp[$i];
978
979                 if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false)) {
980                     /* One Disk is not working.
981                      * if the next line starts with "[>" or "[=" then
982                      * recovery (resync) is in state and the state is
983                      * information instead of critical
984                      */
985                     $nextLine = $tmp[$i + 1];
986                     if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
987                         $state = $this->_setState($state, 'critical');
988                     } else {
989                         $state = $this->_setState($state, 'info');
990                     }
991                 }
992                 if (strpos($line, '[__]') !== false) {
993                     /* both Disk are not working */
994                     $state = $this->_setState($state, 'error');
995                 }
996                 if (strpos($line, '[UU]') !== false) {
997                     /* The disks are OK.
998                      * if the next line starts with "[>" or "[=" then
999                      * recovery (resync) is in state and the state is
1000                      * information instead of ok
1001                      */
1002                     $nextLine = $tmp[$i + 1];
1003                     if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
1004                         $state = $this->_setState($state, 'ok');
1005                     } else {
1006                         $state = $this->_setState($state, 'info');
1007                     }
1008                 }
1009             }
1010         }
1011         /*
1012          * Check, if we have mpt-status installed (LSIsoftware-raid)
1013          */
1014         if (file_exists('/proc/mpt/summary')) {
1015             system('which mpt-status', $retval);
1016             if ($retval === 0) {
1017                 /*
1018                  * Fetch the output
1019                  */
1020                 $data['output'] = shell_exec('mpt-status --autoload -n');
1021
1022                 /*
1023                  * Then calc the state.
1024                  */
1025                 $state = 'ok';
794082 1026                 if(is_array($data['output'])) {
T 1027                     foreach ($data['output'] as $item) {
dead5c 1028                         /*
794082 1029                         * The output contains information for every RAID and every HDD.
T 1030                         * We only need the state of the RAID
1031                         */
1032                         if (strpos($item, 'raidlevel:') !== false) {
1033                             /*
1034                             * We found a raid, process the state of it
1035                             */
1036                             if (strpos($item, ' ONLINE ') !== false) {
1037                                 $this->_setState($state, 'ok');
1038                             } elseif (strpos($item, ' OPTIMAL ') !== false) {
1039                                 $this->_setState($state, 'ok');
1040                             } elseif (strpos($item, ' INITIAL ') !== false) {
1041                                 $this->_setState($state, 'info');
1042                             } elseif (strpos($item, ' INACTIVE ') !== false) {
1043                                 $this->_setState($state, 'critical');
1044                             } elseif (strpos($item, ' RESYNC ') !== false) {
1045                                 $this->_setState($state, 'info');
1046                             } elseif (strpos($item, ' DEGRADED ') !== false) {
1047                                 $this->_setState($state, 'critical');
1048                             } else {
1049                                 /* we don't know the state. so we set the state to critical, that the
1050                                 * admin is warned, that something is wrong
1051                                 */
1052                                 $this->_setState($state, 'critical');
1053                             }
dead5c 1054                         }
V 1055                     }
1056                 }
1057             }
1058         }
a5fe27 1059         
X 1060         /*
1061         * 3ware Controller
1062         */
1063         system('which tw_cli', $retval);
1064         if($retval === 0) {
1065
1066             $data['output'] = shell_exec('tw_cli info c0');
1067
1068             $state = 'ok';
1069             foreach ($data['output'] as $item) {
1070                 if (strpos($item, 'RAID') !== false) {
1071                     if (strpos($item, ' VERIFYING ') !== false) {
1072                         $this->_setState($state, 'info');
1073                     }
1074                     else if (strpos($item, ' MIGRATE-PAUSED ') !== false) {
1075                         $this->_setState($state, 'info');
1076                     }
1077                     else if (strpos($item, ' MIGRATING ') !== false) {
1078                         $this->_setState($state, 'ok');
1079                     }
1080                     else if (strpos($item, ' INITIALIZING ') !== false) {
1081                         $this->_setState($state, 'info');
1082                     }
1083                     else if (strpos($item, ' INIT-PAUSED ') !== false) {
1084                         $this->_setState($state, 'info');
1085                     }
1086                     else if (strpos($item, ' REBUILDING ') !== false) {
1087                         $this->_setState($state, 'info');
1088                     }
1089                     else if (strpos($item, ' REBUILD-PAUSED ') !== false) {
1090                         $this->_setState($state, 'warning');
1091                     }
1092                     else if (strpos($item, ' RECOVERY ') !== false) {
1093                         $this->_setState($state, 'warning');
1094                     }
1095                     else if (strpos($item, ' DEGRADED ') !== false) {
1096                         $this->_setState($state, 'critical');
1097                     }
1098                     else if (strpos($item, ' UNKNOWN ') !== false) {
1099                         $this->_setState($state, 'critical');
1100                     }
1101                     else if (strpos($item, ' OK ') !== false) {
1102                         $this->_setState($state, 'ok');
1103                     }
1104                     else if (strpos($item, ' OPTIMAL ') !== false) {
1105                         $this->_setState($state, 'ok');
1106                     }
1107                     else {
1108                         $this->_setState($state, 'critical');
1109                     }
1110                 }
1111             }
1112         }
1113         
dead5c 1114
V 1115         /*
1116          * Return the Result
1117          */
1118         $res['server_id'] = $server_id;
1119         $res['type'] = $type;
1120         $res['data'] = $data;
1121         $res['state'] = $state;
1122         return $res;
1123     }
1124
1125     public function monitorRkHunter() {
1126         global $conf;
1127
1128         /* the id of the server as int */
1129         $server_id = intval($conf['server_id']);
1130
1131         /** The type of the data */
1132         $type = 'rkhunter';
1133
1134         /* This monitoring is only available if rkhunter is installed */
1135         system('which rkhunter', $retval);
1136         if ($retval === 0) {
1137             /*
1138              * Fetch the output
1139              */
1140             $data['output'] = shell_exec('rkhunter --update --checkall --nocolors --skip-keypress');
1141
1142             /*
1143              * At this moment, there is no state (maybe later)
1144              */
1145             $state = 'no_state';
1146         } else {
1147             /*
1148              * rkhunter is not installed, so there is no data and no state
1149              *
1150              * no_state, NOT unknown, because "unknown" is shown as state
1151              * inside the GUI. no_state is hidden.
1152              *
1153              * We have to write NO DATA inside the DB, because the GUI
1154              * could not know, if there is any dat, or not...
1155              */
1156             $state = 'no_state';
1157             $data['output'] = '';
1158         }
1159
1160         /*
1161          * Return the Result
1162          */
1163         $res['server_id'] = $server_id;
1164         $res['type'] = $type;
1165         $res['data'] = $data;
1166         $res['state'] = $state;
1167         return $res;
1168     }
1169
1170     public function monitorFail2ban() {
1171         global $conf;
1172
1173         /* the id of the server as int */
1174         $server_id = intval($conf['server_id']);
1175
1176         /** The type of the data */
1177         $type = 'log_fail2ban';
1178
1179         /* This monitoring is only available if fail2ban is installed */
1180         system('which fail2ban-client', $retval); // Debian, Ubuntu, Fedora
1181         if ($retval !== 0)
1182             system('which fail2ban', $retval); // CentOS
1183         if ($retval === 0) {
1184             /*  Get the data of the log */
1185             $data = $this->_getLogData($type);
1186
1187             /*
1188              * At this moment, there is no state (maybe later)
1189              */
1190             $state = 'no_state';
1191         } else {
1192             /*
1193              * fail2ban is not installed, so there is no data and no state
1194              *
1195              * no_state, NOT unknown, because "unknown" is shown as state
1196              * inside the GUI. no_state is hidden.
1197              *
1198              * We have to write NO DATA inside the DB, because the GUI
1199              * could not know, if there is any dat, or not...
1200              */
1201             $state = 'no_state';
1202             $data = '';
1203         }
1204
1205         /*
1206          * Return the Result
1207          */
1208         $res['server_id'] = $server_id;
1209         $res['type'] = $type;
1210         $res['data'] = $data;
1211         $res['state'] = $state;
1212         return $res;
1213     }
1214
28548b 1215     public function monitorIPTables() {
L 1216         global $conf;
1217
1218         /* the id of the server as int */
1219         $server_id = intval($conf['server_id']);
1220
1221         /** The type of the data */
1222         $type = 'iptables_rules';
1223
1224         /* This monitoring is only available if fail2ban is installed */
1225         system('which iptables', $retval); // Debian, Ubuntu, Fedora
1226         if ($retval === 0) {
1227             /*  Get the data of the log */
87dd10 1228             $data['output'] = '<h2>iptables -S (ipv4)</h2>'.shell_exec('iptables -S');
X 1229
1230             /*
1231              * At this moment, there is no state (maybe later)
1232              */
1233             $state = 'no_state';
1234         } else {
1235             $state = 'no_state';
1236             $data = '';
1237         }
1238         
1239         
1240         /* This monitoring is only available if fail2ban is installed */
1241         system('which ip6tables', $retval); // Debian, Ubuntu, Fedora
1242         if ($retval === 0) {
1243             /*  Get the data of the log */
1244             $data['output'] .= '<br><h2>ip6tables -S (ipv6)</h2>'.shell_exec('ip6tables -S');
28548b 1245
L 1246             /*
1247              * At this moment, there is no state (maybe later)
1248              */
1249             $state = 'no_state';
1250         } else {
1251             $state = 'no_state';
1252             $data = '';
1253         }
1254
1255         /*
1256          * Return the Result
1257          */
1258         $res['server_id'] = $server_id;
1259         $res['type'] = $type;
1260         $res['data'] = $data;
1261         $res['state'] = $state;
1262         return $res;
1263     }
1264
dead5c 1265     public function monitorSysLog() {
f63bda 1266         global $app;
dead5c 1267         global $conf;
V 1268
1269         /* the id of the server as int */
1270         $server_id = intval($conf['server_id']);
1271
1272         /** The type of the data */
1273         $type = 'sys_log';
1274
1275         /*
1276          * is there any warning or error for this server?
1277          */
1278         $state = 'ok';
1279         $dbData = $app->dbmaster->queryAllRecords('SELECT loglevel FROM sys_log WHERE server_id = ' . $server_id . ' AND loglevel > 0');
1280         if (is_array($dbData)) {
1281             foreach ($dbData as $item) {
1282                 if ($item['loglevel'] == 1)
1283                     $state = $this->_setState($state, 'warning');
1284                 if ($item['loglevel'] == 2)
1285                     $state = $this->_setState($state, 'error');
1286             }
1287         }
1288
1289         /** There is no monitor-data because the data is in the sys_log table */
1290         $data['output'] = '';
1291
1292         /*
1293          * Return the Result
1294          */
1295         $res['server_id'] = $server_id;
1296         $res['type'] = $type;
1297         $res['data'] = $data;
1298         $res['state'] = $state;
1299         return $res;
1300     }
1301
1302     public function monitorMailLog() {
1303         global $conf;
1304
1305         /* the id of the server as int */
1306         $server_id = intval($conf['server_id']);
1307
1308         /** The type of the data */
1309         $type = 'log_mail';
1310
1311         /* Get the data of the log */
1312         $data = $this->_getLogData($type);
1313
1314         /*
1315          * actually this info has no state.
1316          * maybe someone knows better...???...
1317          */
1318         $state = 'no_state';
1319
1320         /*
1321          * Return the Result
1322          */
1323         $res['server_id'] = $server_id;
1324         $res['type'] = $type;
1325         $res['data'] = $data;
1326         $res['state'] = $state;
1327         return $res;
1328     }
1329
1330     public function monitorMailWarnLog() {
1331         global $conf;
1332
1333         /* the id of the server as int */
1334         $server_id = intval($conf['server_id']);
1335
1336         /** The type of the data */
1337         $type = 'log_mail_warn';
1338
1339         /* Get the data of the log */
1340         $data = $this->_getLogData($type);
1341
1342         /*
1343          * actually this info has no state.
1344          * maybe someone knows better...???...
1345          */
1346         $state = 'no_state';
1347
1348         /*
1349          * Return the Result
1350          */
1351         $res['server_id'] = $server_id;
1352         $res['type'] = $type;
1353         $res['data'] = $data;
1354         $res['state'] = $state;
1355         return $res;
1356     }
1357
1358     public function monitorMailErrLog() {
1359         global $conf;
1360
1361         /* the id of the server as int */
1362         $server_id = intval($conf['server_id']);
1363
1364         /** The type of the data */
1365         $type = 'log_mail_err';
1366
1367         /* Get the data of the log */
1368         $data = $this->_getLogData($type);
1369
1370         /*
1371          * actually this info has no state.
1372          * maybe someone knows better...???...
1373          */
1374         $state = 'no_state';
1375
1376         /*
1377          * Return the Result
1378          */
1379         $res['server_id'] = $server_id;
1380         $res['type'] = $type;
1381         $res['data'] = $data;
1382         $res['state'] = $state;
1383         return $res;
1384     }
1385
1386     public function monitorMessagesLog() {
1387         global $conf;
1388
1389         /* the id of the server as int */
1390         $server_id = intval($conf['server_id']);
1391
1392         /** The type of the data */
1393         $type = 'log_messages';
1394
1395         /* Get the data of the log */
1396         $data = $this->_getLogData($type);
1397
1398         /*
1399          * actually this info has no state.
1400          * maybe someone knows better...???...
1401          */
1402         $state = 'no_state';
1403
1404         /*
1405          * Return the Result
1406          */
1407         $res['server_id'] = $server_id;
1408         $res['type'] = $type;
1409         $res['data'] = $data;
1410         $res['state'] = $state;
1411         return $res;
1412     }
1413
1414     public function monitorISPCCronLog() {
1415         global $conf;
1416
1417         /* the id of the server as int */
1418         $server_id = intval($conf['server_id']);
1419
1420         /** The type of the data */
1421         $type = 'log_ispc_cron';
1422
1423         /* Get the data of the log */
1424         $data = $this->_getLogData($type);
1425
1426         /*
1427          * actually this info has no state.
1428          * maybe someone knows better...???...
1429          */
1430         $state = 'no_state';
1431
1432         /*
1433          * Return the Result
1434          */
1435         $res['server_id'] = $server_id;
1436         $res['type'] = $type;
1437         $res['data'] = $data;
1438         $res['state'] = $state;
1439         return $res;
1440     }
1441
1442     public function monitorFreshClamLog() {
1443         global $conf;
1444
1445         /* the id of the server as int */
1446         $server_id = intval($conf['server_id']);
1447
1448         /** The type of the data */
1449         $type = 'log_freshclam';
1450
1451         /* Get the data of the log */
1452         $data = $this->_getLogData($type);
1453
1454         /* Get the data from the LAST log-Entry.
1455          * if there can be found:
1456          * WARNING: Your ClamAV installation is OUTDATED!
1457          * then the clamav is outdated. This is a warning!
1458          */
1459         $state = 'ok';
1460
1461         $tmp = explode("\n", $data);
1462         $lastLog = array();
1463         if ($tmp[sizeof($tmp) - 1] == '') {
1464             /* the log ends with an empty line remove this */
1465             array_pop($tmp);
1466         }
1467         if (strpos($tmp[sizeof($tmp) - 1], '-------------') !== false) {
1468             /* the log ends with "-----..." remove this */
1469             array_pop($tmp);
1470         }
1471         for ($i = sizeof($tmp) - 1; $i > 0; $i--) {
1472             if (strpos($tmp[$i], '---------') === false) {
1473                 /* no delimiter found, so add this to the last-log */
1474                 $lastLog[] = $tmp[$i];
1475             } else {
1476                 /* delimiter found, so there is no more line left! */
1477                 break;
1478             }
1479         }
1480
1481         /*
1482          * Now we have the last log in the array.
1483          * Check if the outdated-string is found...
1484          */
1485         foreach ($lastLog as $line) {
1486             if (strpos(strtolower($line), 'outdated') !== false) {
1487                 /*
1488                  * Outdatet is only info, because if we set this to warning, the server is
1489                  * as long in state warning, as there is a new version of ClamAv which takes
1490                  * sometimes weeks!
1491                  */
1492                 $state = $this->_setState($state, 'info');
1493             }
1494         }
1495
1496         /*
1497          * Return the Result
1498          */
1499         $res['server_id'] = $server_id;
1500         $res['type'] = $type;
1501         $res['data'] = $data;
1502         $res['state'] = $state;
1503         return $res;
1504     }
1505
1506     public function monitorClamAvLog() {
1507         global $conf;
1508
1509         /* the id of the server as int */
1510         $server_id = intval($conf['server_id']);
1511
1512         /** The type of the data */
1513         $type = 'log_clamav';
1514
1515         /* Get the data of the log */
1516         $data = $this->_getLogData($type);
1517
1518         // Todo: the state should be calculated.
1519         $state = 'ok';
1520
1521         /*
1522          * Return the Result
1523          */
1524         $res['server_id'] = $server_id;
1525         $res['type'] = $type;
1526         $res['data'] = $data;
1527         $res['state'] = $state;
1528         return $res;
1529     }
1530
1531     public function monitorIspConfigLog() {
1532         global $conf;
1533
1534         /* the id of the server as int */
1535         $server_id = intval($conf['server_id']);
1536
1537         /** The type of the data */
1538         $type = 'log_ispconfig';
1539
1540         /* Get the data of the log */
1541         $data = $this->_getLogData($type);
1542
1543         // Todo: the state should be calculated.
1544         $state = 'ok';
1545
1546         /*
1547          * Return the Result
1548          */
1549         $res['server_id'] = $server_id;
1550         $res['type'] = $type;
1551         $res['data'] = $data;
1552         $res['state'] = $state;
1553         return $res;
1554     }
1555
1556     public function _getLogData($log) {
1557         global $conf;
1558
1559         $dist = '';
1560         $logfile = '';
1561
1562         if (@is_file('/etc/debian_version')) {
1563             $dist = 'debian';
1564         } elseif (@is_file('/etc/redhat-release')) {
1565             $dist = 'redhat';
1566         } elseif (@is_file('/etc/SuSE-release')) {
1567             $dist = 'suse';
1568         } elseif (@is_file('/etc/gentoo-release')) {
1569             $dist = 'gentoo';
1570         }
1571
1572         switch ($log) {
1573             case 'log_mail':
1574                 if ($dist == 'debian') {
1575                     $logfile = '/var/log/mail.log';
1576                 } elseif ($dist == 'redhat') {
1577                     $logfile = '/var/log/maillog';
1578                 } elseif ($dist == 'suse') {
1579                     $logfile = '/var/log/mail.info';
1580                 } elseif ($dist == 'gentoo') {
1581                     $logfile = '/var/log/maillog';
1582                 }
1583                 break;
1584             case 'log_mail_warn':
1585                 if ($dist == 'debian') {
1586                     $logfile = '/var/log/mail.warn';
1587                 } elseif ($dist == 'redhat') {
1588                     $logfile = '/var/log/maillog';
1589                 } elseif ($dist == 'suse') {
1590                     $logfile = '/var/log/mail.warn';
1591                 } elseif ($dist == 'gentoo') {
1592                     $logfile = '/var/log/maillog';
1593                 }
1594                 break;
1595             case 'log_mail_err':
1596                 if ($dist == 'debian') {
1597                     $logfile = '/var/log/mail.err';
1598                 } elseif ($dist == 'redhat') {
1599                     $logfile = '/var/log/maillog';
1600                 } elseif ($dist == 'suse') {
1601                     $logfile = '/var/log/mail.err';
1602                 } elseif ($dist == 'gentoo') {
1603                     $logfile = '/var/log/maillog';
1604                 }
1605                 break;
1606             case 'log_messages':
1607                 if ($dist == 'debian') {
1608                     $logfile = '/var/log/messages';
1609                 } elseif ($dist == 'redhat') {
1610                     $logfile = '/var/log/messages';
1611                 } elseif ($dist == 'suse') {
1612                     $logfile = '/var/log/messages';
1613                 } elseif ($dist == 'gentoo') {
1614                     $logfile = '/var/log/messages';
1615                 }
1616                 break;
1617             case 'log_ispc_cron':
1618                 if ($dist == 'debian') {
1619                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1620                 } elseif ($dist == 'redhat') {
1621                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1622                 } elseif ($dist == 'suse') {
1623                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1624                 } elseif ($dist == 'gentoo') {
1625                     $logfile = '/var/log/cron';
1626                 }
1627                 break;
1628             case 'log_freshclam':
1629                 if ($dist == 'debian') {
1630                     $logfile = '/var/log/clamav/freshclam.log';
1631                 } elseif ($dist == 'redhat') {
1632                     $logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
1633                 } elseif ($dist == 'suse') {
1634                     $logfile = '/var/log/freshclam.log';
1635                 } elseif ($dist == 'gentoo') {
1636                     $logfile = '/var/log/clamav/freshclam.log';
1637                 }
1638                 break;
1639             case 'log_clamav':
1640                 if ($dist == 'debian') {
1641                     $logfile = '/var/log/clamav/clamav.log';
1642                 } elseif ($dist == 'redhat') {
1643                     $logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
1644                 } elseif ($dist == 'suse') {
1645                     $logfile = '/var/log/clamd.log';
1646                 } elseif ($dist == 'gentoo') {
1647                     $logfile = '/var/log/clamav/clamd.log';
1648                 }
1649                 break;
1650             case 'log_fail2ban':
1651                 if ($dist == 'debian') {
1652                     $logfile = '/var/log/fail2ban.log';
1653                 } elseif ($dist == 'redhat') {
1654                     $logfile = '/var/log/fail2ban.log';
1655                 } elseif ($dist == 'suse') {
1656                     $logfile = '/var/log/fail2ban.log';
1657                 } elseif ($dist == 'gentoo') {
1658                     $logfile = '/var/log/fail2ban.log';
1659                 }
1660                 break;
1661             case 'log_ispconfig':
1662                 if ($dist == 'debian') {
1663                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1664                 } elseif ($dist == 'redhat') {
1665                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1666                 } elseif ($dist == 'suse') {
1667                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1668                 } elseif ($dist == 'gentoo') {
1669                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1670                 }
1671                 break;
1672             default:
1673                 $logfile = '';
1674                 break;
1675         }
1676
1677         // Getting the logfile content
1678         if ($logfile != '') {
1679             $logfile = escapeshellcmd($logfile);
1680             if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) {
1681                 $log = 'Logfile path error.';
1682             } else {
1683                 $log = '';
1684                 if (is_readable($logfile)) {
1685                     $fd = popen('tail -n 100 ' . $logfile, 'r');
1686                     if ($fd) {
1687                         while (!feof($fd)) {
1688                             $log .= fgets($fd, 4096);
1689                             $n++;
1690                             if ($n > 1000)
1691                                 break;
1692                         }
1693                         fclose($fd);
1694                     }
1695                 } else {
1696                     $log = 'Unable to read ' . $logfile;
1697                 }
1698             }
1699         }
1700
1701         return $log;
1702     }
1703
1704     private function _checkTcp($host, $port) {
e5c5af 1705         /* Try to open a connection */
dead5c 1706         $fp = @fsockopen($host, $port, $errno, $errstr, 2);
V 1707
1708         if ($fp) {
8bbcc1 1709             /*
e5c5af 1710              * We got a connection, this means, everything is O.K.
V 1711              * But maybe we are able to do more deep testing?
8bbcc1 1712              */
e5c5af 1713             if ($port == 80) {
V 1714                 /*
1715                  * Port 80 means, testing APACHE
1716                  * So we can do a deepter test and try to get data over this connection.
1717                  * (if apache hangs, we get a connection but a timeout by trying to GET the data!)
1718                  */
1719                 fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
1720                 stream_set_timeout($fp, 5); // Timeout after 5 seconds
1721                 $res = fread($fp, 10);  // try to get 10 bytes (enough to test!)
1722                 $info = stream_get_meta_data($fp);
1723                 if ($info['timed_out']) {
1724                     return false; // Apache was not able to send data over this connection
1725                 }
eed6b3 1726             }
e5c5af 1727
V 1728             /* The connection is no longer needed */
1729             fclose($fp);
1730             /* We are able to establish a connection */
1731             return true;
dead5c 1732         } else {
e5c5af 1733             /* We are NOT able to establish a connection */
V 1734             return false;
dead5c 1735         }
V 1736     }
1737
1738     private function _checkUdp($host, $port) {
1739
1740         $fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2);
1741
1742         if ($fp) {
1743             fclose($fp);
1744             return true;
1745         } else {
1746             return false;
1747         }
1748     }
1749
1750     private function _checkFtp($host, $port) {
1751
1752         $conn_id = @ftp_connect($host, $port);
1753
1754         if ($conn_id) {
1755             @ftp_close($conn_id);
1756             return true;
1757         } else {
1758             return false;
1759         }
1760     }
eed6b3 1761
e5c5af 1762     /**
V 1763      * Set the state to the given level (or higher, but not lesser).
1764      * * If the actual state is critical and you call the method with ok,
1765      *   then the state is critical.
1766      *
1767      * * If the actual state is critical and you call the method with error,
1768      *   then the state is error.
1769      */
1770     private function _setState($oldState, $newState) {
1771         /*
1772          * Calculate the weight of the old state
1773          */
1774         switch ($oldState) {
1775             case 'no_state': $oldInt = 0;
1776                 break;
1777             case 'ok': $oldInt = 1;
1778                 break;
1779             case 'unknown': $oldInt = 2;
1780                 break;
1781             case 'info': $oldInt = 3;
1782                 break;
1783             case 'warning': $oldInt = 4;
1784                 break;
1785             case 'critical': $oldInt = 5;
1786                 break;
1787             case 'error': $oldInt = 6;
1788                 break;
1789         }
1790         /*
1791          * Calculate the weight of the new state
1792          */
1793         switch ($newState) {
1794             case 'no_state': $newInt = 0;
1795                 break;
1796             case 'ok': $newInt = 1;
1797                 break;
1798             case 'unknown': $newInt = 2;
1799                 break;
1800             case 'info': $newInt = 3;
1801                 break;
1802             case 'warning': $newInt = 4;
1803                 break;
1804             case 'critical': $newInt = 5;
1805                 break;
1806             case 'error': $newInt = 6;
1807                 break;
1808         }
1809
1810         /*
1811          * Set to the higher level
1812          */
1813         if ($newInt > $oldInt) {
1814             return $newState;
1815         } else {
1816             return $oldState;
1817         }
1818     }
dead5c 1819
V 1820     private function _getIntArray($line) {
1821         /** The array of float found */
1822         $res = array();
1823         /* First build a array from the line */
1824         $data = explode(' ', $line);
1825         /* then check if any item is a float */
1826         foreach ($data as $item) {
1827             if ($item . '' == (int) $item . '') {
1828                 $res[] = $item;
1829             }
1830         }
1831         return $res;
1832     }
1833
1834 }
1835
7cd997 1836 ?>