tbrehm
2011-09-29 91c38127621ee7587c058af50178eddf26ee700c
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() {
154         global $conf;
155
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         }
890
891         /*
892          * Return the Result
893          */
894         $res['server_id'] = $server_id;
895         $res['type'] = $type;
896         $res['data'] = $data;
897         $res['state'] = $state;
898         return $res;
899     }
900
901     public function monitorMailQueue() {
902         global $conf;
903
904         /* the id of the server as int */
905         $server_id = intval($conf['server_id']);
906
907         /** The type of the data */
908         $type = 'mailq';
909
910         /* Get the data from the mailq */
911         $data['output'] = shell_exec('mailq');
912
913         /*
914          *  The last line has more informations
915          */
916         $tmp = explode("\n", $data['output']);
917         $more = $tmp[sizeof($tmp) - 1];
918         $this->_getIntArray($more);
919         $data['bytes'] = $res[0];
920         $data['requests'] = $res[1];
921
922         /** The state of the mailq. */
923         $state = 'ok';
924         if ($data['requests'] > 2000)
925             $state = 'info';
926         if ($data['requests'] > 5000)
927             $state = 'warning';
928         if ($data['requests'] > 8000)
929             $state = 'critical';
930         if ($data['requests'] > 10000)
931             $state = 'error';
932
933         /*
934          * Return the Result
935          */
936         $res['server_id'] = $server_id;
937         $res['type'] = $type;
938         $res['data'] = $data;
939         $res['state'] = $state;
940         return $res;
941     }
942
943     public function monitorRaid() {
944         global $conf;
945
946         /* the id of the server as int */
947         $server_id = intval($conf['server_id']);
948
949         /** The type of the data */
950         $type = 'raid_state';
951
952         /*
953          * We support several RAID types, but if we can't find any of them, we have no data
954          */
955         $state = 'no_state';
956         $data['output'] = '';
957
958         /*
959          * Check, if Software-RAID is enabled
960          */
961         if (file_exists('/proc/mdstat')) {
962             /*
963              * Fetch the output
964              */
965             $data['output'] = shell_exec('cat /proc/mdstat');
966
967             /*
968              * Then calc the state.
969              */
970             $tmp = explode("\n", $data['output']);
971             $state = 'ok';
972             for ($i = 0; $i < sizeof($tmp); $i++) {
973                 /* fetch the next line */
974                 $line = $tmp[$i];
975
976                 if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false)) {
977                     /* One Disk is not working.
978                      * if the next line starts with "[>" or "[=" then
979                      * recovery (resync) is in state and the state is
980                      * information instead of critical
981                      */
982                     $nextLine = $tmp[$i + 1];
983                     if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
984                         $state = $this->_setState($state, 'critical');
985                     } else {
986                         $state = $this->_setState($state, 'info');
987                     }
988                 }
989                 if (strpos($line, '[__]') !== false) {
990                     /* both Disk are not working */
991                     $state = $this->_setState($state, 'error');
992                 }
993                 if (strpos($line, '[UU]') !== false) {
994                     /* The disks are OK.
995                      * if the next line starts with "[>" or "[=" then
996                      * recovery (resync) is in state and the state is
997                      * information instead of ok
998                      */
999                     $nextLine = $tmp[$i + 1];
1000                     if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
1001                         $state = $this->_setState($state, 'ok');
1002                     } else {
1003                         $state = $this->_setState($state, 'info');
1004                     }
1005                 }
1006             }
1007         }
1008         /*
1009          * Check, if we have mpt-status installed (LSIsoftware-raid)
1010          */
1011         if (file_exists('/proc/mpt/summary')) {
1012             system('which mpt-status', $retval);
1013             if ($retval === 0) {
1014                 /*
1015                  * Fetch the output
1016                  */
1017                 $data['output'] = shell_exec('mpt-status --autoload -n');
1018
1019                 /*
1020                  * Then calc the state.
1021                  */
1022                 $state = 'ok';
794082 1023                 if(is_array($data['output'])) {
T 1024                     foreach ($data['output'] as $item) {
dead5c 1025                         /*
794082 1026                         * The output contains information for every RAID and every HDD.
T 1027                         * We only need the state of the RAID
1028                         */
1029                         if (strpos($item, 'raidlevel:') !== false) {
1030                             /*
1031                             * We found a raid, process the state of it
1032                             */
1033                             if (strpos($item, ' ONLINE ') !== false) {
1034                                 $this->_setState($state, 'ok');
1035                             } elseif (strpos($item, ' OPTIMAL ') !== false) {
1036                                 $this->_setState($state, 'ok');
1037                             } elseif (strpos($item, ' INITIAL ') !== false) {
1038                                 $this->_setState($state, 'info');
1039                             } elseif (strpos($item, ' INACTIVE ') !== false) {
1040                                 $this->_setState($state, 'critical');
1041                             } elseif (strpos($item, ' RESYNC ') !== false) {
1042                                 $this->_setState($state, 'info');
1043                             } elseif (strpos($item, ' DEGRADED ') !== false) {
1044                                 $this->_setState($state, 'critical');
1045                             } else {
1046                                 /* we don't know the state. so we set the state to critical, that the
1047                                 * admin is warned, that something is wrong
1048                                 */
1049                                 $this->_setState($state, 'critical');
1050                             }
dead5c 1051                         }
V 1052                     }
1053                 }
1054             }
1055         }
a5fe27 1056         
X 1057         /*
1058         * 3ware Controller
1059         */
1060         system('which tw_cli', $retval);
1061         if($retval === 0) {
1062
1063             $data['output'] = shell_exec('tw_cli info c0');
1064
1065             $state = 'ok';
1066             foreach ($data['output'] as $item) {
1067                 if (strpos($item, 'RAID') !== false) {
1068                     if (strpos($item, ' VERIFYING ') !== false) {
1069                         $this->_setState($state, 'info');
1070                     }
1071                     else if (strpos($item, ' MIGRATE-PAUSED ') !== false) {
1072                         $this->_setState($state, 'info');
1073                     }
1074                     else if (strpos($item, ' MIGRATING ') !== false) {
1075                         $this->_setState($state, 'ok');
1076                     }
1077                     else if (strpos($item, ' INITIALIZING ') !== false) {
1078                         $this->_setState($state, 'info');
1079                     }
1080                     else if (strpos($item, ' INIT-PAUSED ') !== false) {
1081                         $this->_setState($state, 'info');
1082                     }
1083                     else if (strpos($item, ' REBUILDING ') !== false) {
1084                         $this->_setState($state, 'info');
1085                     }
1086                     else if (strpos($item, ' REBUILD-PAUSED ') !== false) {
1087                         $this->_setState($state, 'warning');
1088                     }
1089                     else if (strpos($item, ' RECOVERY ') !== false) {
1090                         $this->_setState($state, 'warning');
1091                     }
1092                     else if (strpos($item, ' DEGRADED ') !== false) {
1093                         $this->_setState($state, 'critical');
1094                     }
1095                     else if (strpos($item, ' UNKNOWN ') !== false) {
1096                         $this->_setState($state, 'critical');
1097                     }
1098                     else if (strpos($item, ' OK ') !== false) {
1099                         $this->_setState($state, 'ok');
1100                     }
1101                     else if (strpos($item, ' OPTIMAL ') !== false) {
1102                         $this->_setState($state, 'ok');
1103                     }
1104                     else {
1105                         $this->_setState($state, 'critical');
1106                     }
1107                 }
1108             }
1109         }
1110         
dead5c 1111
V 1112         /*
1113          * Return the Result
1114          */
1115         $res['server_id'] = $server_id;
1116         $res['type'] = $type;
1117         $res['data'] = $data;
1118         $res['state'] = $state;
1119         return $res;
1120     }
1121
1122     public function monitorRkHunter() {
1123         global $conf;
1124
1125         /* the id of the server as int */
1126         $server_id = intval($conf['server_id']);
1127
1128         /** The type of the data */
1129         $type = 'rkhunter';
1130
1131         /* This monitoring is only available if rkhunter is installed */
1132         system('which rkhunter', $retval);
1133         if ($retval === 0) {
1134             /*
1135              * Fetch the output
1136              */
1137             $data['output'] = shell_exec('rkhunter --update --checkall --nocolors --skip-keypress');
1138
1139             /*
1140              * At this moment, there is no state (maybe later)
1141              */
1142             $state = 'no_state';
1143         } else {
1144             /*
1145              * rkhunter is not installed, so there is no data and no state
1146              *
1147              * no_state, NOT unknown, because "unknown" is shown as state
1148              * inside the GUI. no_state is hidden.
1149              *
1150              * We have to write NO DATA inside the DB, because the GUI
1151              * could not know, if there is any dat, or not...
1152              */
1153             $state = 'no_state';
1154             $data['output'] = '';
1155         }
1156
1157         /*
1158          * Return the Result
1159          */
1160         $res['server_id'] = $server_id;
1161         $res['type'] = $type;
1162         $res['data'] = $data;
1163         $res['state'] = $state;
1164         return $res;
1165     }
1166
1167     public function monitorFail2ban() {
1168         global $conf;
1169
1170         /* the id of the server as int */
1171         $server_id = intval($conf['server_id']);
1172
1173         /** The type of the data */
1174         $type = 'log_fail2ban';
1175
1176         /* This monitoring is only available if fail2ban is installed */
1177         system('which fail2ban-client', $retval); // Debian, Ubuntu, Fedora
1178         if ($retval !== 0)
1179             system('which fail2ban', $retval); // CentOS
1180         if ($retval === 0) {
1181             /*  Get the data of the log */
1182             $data = $this->_getLogData($type);
1183
1184             /*
1185              * At this moment, there is no state (maybe later)
1186              */
1187             $state = 'no_state';
1188         } else {
1189             /*
1190              * fail2ban is not installed, so there is no data and no state
1191              *
1192              * no_state, NOT unknown, because "unknown" is shown as state
1193              * inside the GUI. no_state is hidden.
1194              *
1195              * We have to write NO DATA inside the DB, because the GUI
1196              * could not know, if there is any dat, or not...
1197              */
1198             $state = 'no_state';
1199             $data = '';
1200         }
1201
1202         /*
1203          * Return the Result
1204          */
1205         $res['server_id'] = $server_id;
1206         $res['type'] = $type;
1207         $res['data'] = $data;
1208         $res['state'] = $state;
1209         return $res;
1210     }
1211
28548b 1212     public function monitorIPTables() {
L 1213         global $conf;
1214
1215         /* the id of the server as int */
1216         $server_id = intval($conf['server_id']);
1217
1218         /** The type of the data */
1219         $type = 'iptables_rules';
1220
1221         /* This monitoring is only available if fail2ban is installed */
1222         system('which iptables', $retval); // Debian, Ubuntu, Fedora
1223         if ($retval === 0) {
1224             /*  Get the data of the log */
87dd10 1225             $data['output'] = '<h2>iptables -S (ipv4)</h2>'.shell_exec('iptables -S');
X 1226
1227             /*
1228              * At this moment, there is no state (maybe later)
1229              */
1230             $state = 'no_state';
1231         } else {
1232             $state = 'no_state';
1233             $data = '';
1234         }
1235         
1236         
1237         /* This monitoring is only available if fail2ban is installed */
1238         system('which ip6tables', $retval); // Debian, Ubuntu, Fedora
1239         if ($retval === 0) {
1240             /*  Get the data of the log */
1241             $data['output'] .= '<br><h2>ip6tables -S (ipv6)</h2>'.shell_exec('ip6tables -S');
28548b 1242
L 1243             /*
1244              * At this moment, there is no state (maybe later)
1245              */
1246             $state = 'no_state';
1247         } else {
1248             $state = 'no_state';
1249             $data = '';
1250         }
1251
1252         /*
1253          * Return the Result
1254          */
1255         $res['server_id'] = $server_id;
1256         $res['type'] = $type;
1257         $res['data'] = $data;
1258         $res['state'] = $state;
1259         return $res;
1260     }
1261
dead5c 1262     public function monitorSysLog() {
f63bda 1263         global $app;
dead5c 1264         global $conf;
V 1265
1266         /* the id of the server as int */
1267         $server_id = intval($conf['server_id']);
1268
1269         /** The type of the data */
1270         $type = 'sys_log';
1271
1272         /*
1273          * is there any warning or error for this server?
1274          */
1275         $state = 'ok';
1276         $dbData = $app->dbmaster->queryAllRecords('SELECT loglevel FROM sys_log WHERE server_id = ' . $server_id . ' AND loglevel > 0');
1277         if (is_array($dbData)) {
1278             foreach ($dbData as $item) {
1279                 if ($item['loglevel'] == 1)
1280                     $state = $this->_setState($state, 'warning');
1281                 if ($item['loglevel'] == 2)
1282                     $state = $this->_setState($state, 'error');
1283             }
1284         }
1285
1286         /** There is no monitor-data because the data is in the sys_log table */
1287         $data['output'] = '';
1288
1289         /*
1290          * Return the Result
1291          */
1292         $res['server_id'] = $server_id;
1293         $res['type'] = $type;
1294         $res['data'] = $data;
1295         $res['state'] = $state;
1296         return $res;
1297     }
1298
1299     public function monitorMailLog() {
1300         global $conf;
1301
1302         /* the id of the server as int */
1303         $server_id = intval($conf['server_id']);
1304
1305         /** The type of the data */
1306         $type = 'log_mail';
1307
1308         /* Get the data of the log */
1309         $data = $this->_getLogData($type);
1310
1311         /*
1312          * actually this info has no state.
1313          * maybe someone knows better...???...
1314          */
1315         $state = 'no_state';
1316
1317         /*
1318          * Return the Result
1319          */
1320         $res['server_id'] = $server_id;
1321         $res['type'] = $type;
1322         $res['data'] = $data;
1323         $res['state'] = $state;
1324         return $res;
1325     }
1326
1327     public function monitorMailWarnLog() {
1328         global $conf;
1329
1330         /* the id of the server as int */
1331         $server_id = intval($conf['server_id']);
1332
1333         /** The type of the data */
1334         $type = 'log_mail_warn';
1335
1336         /* Get the data of the log */
1337         $data = $this->_getLogData($type);
1338
1339         /*
1340          * actually this info has no state.
1341          * maybe someone knows better...???...
1342          */
1343         $state = 'no_state';
1344
1345         /*
1346          * Return the Result
1347          */
1348         $res['server_id'] = $server_id;
1349         $res['type'] = $type;
1350         $res['data'] = $data;
1351         $res['state'] = $state;
1352         return $res;
1353     }
1354
1355     public function monitorMailErrLog() {
1356         global $conf;
1357
1358         /* the id of the server as int */
1359         $server_id = intval($conf['server_id']);
1360
1361         /** The type of the data */
1362         $type = 'log_mail_err';
1363
1364         /* Get the data of the log */
1365         $data = $this->_getLogData($type);
1366
1367         /*
1368          * actually this info has no state.
1369          * maybe someone knows better...???...
1370          */
1371         $state = 'no_state';
1372
1373         /*
1374          * Return the Result
1375          */
1376         $res['server_id'] = $server_id;
1377         $res['type'] = $type;
1378         $res['data'] = $data;
1379         $res['state'] = $state;
1380         return $res;
1381     }
1382
1383     public function monitorMessagesLog() {
1384         global $conf;
1385
1386         /* the id of the server as int */
1387         $server_id = intval($conf['server_id']);
1388
1389         /** The type of the data */
1390         $type = 'log_messages';
1391
1392         /* Get the data of the log */
1393         $data = $this->_getLogData($type);
1394
1395         /*
1396          * actually this info has no state.
1397          * maybe someone knows better...???...
1398          */
1399         $state = 'no_state';
1400
1401         /*
1402          * Return the Result
1403          */
1404         $res['server_id'] = $server_id;
1405         $res['type'] = $type;
1406         $res['data'] = $data;
1407         $res['state'] = $state;
1408         return $res;
1409     }
1410
1411     public function monitorISPCCronLog() {
1412         global $conf;
1413
1414         /* the id of the server as int */
1415         $server_id = intval($conf['server_id']);
1416
1417         /** The type of the data */
1418         $type = 'log_ispc_cron';
1419
1420         /* Get the data of the log */
1421         $data = $this->_getLogData($type);
1422
1423         /*
1424          * actually this info has no state.
1425          * maybe someone knows better...???...
1426          */
1427         $state = 'no_state';
1428
1429         /*
1430          * Return the Result
1431          */
1432         $res['server_id'] = $server_id;
1433         $res['type'] = $type;
1434         $res['data'] = $data;
1435         $res['state'] = $state;
1436         return $res;
1437     }
1438
1439     public function monitorFreshClamLog() {
1440         global $conf;
1441
1442         /* the id of the server as int */
1443         $server_id = intval($conf['server_id']);
1444
1445         /** The type of the data */
1446         $type = 'log_freshclam';
1447
1448         /* Get the data of the log */
1449         $data = $this->_getLogData($type);
1450
1451         /* Get the data from the LAST log-Entry.
1452          * if there can be found:
1453          * WARNING: Your ClamAV installation is OUTDATED!
1454          * then the clamav is outdated. This is a warning!
1455          */
1456         $state = 'ok';
1457
1458         $tmp = explode("\n", $data);
1459         $lastLog = array();
1460         if ($tmp[sizeof($tmp) - 1] == '') {
1461             /* the log ends with an empty line remove this */
1462             array_pop($tmp);
1463         }
1464         if (strpos($tmp[sizeof($tmp) - 1], '-------------') !== false) {
1465             /* the log ends with "-----..." remove this */
1466             array_pop($tmp);
1467         }
1468         for ($i = sizeof($tmp) - 1; $i > 0; $i--) {
1469             if (strpos($tmp[$i], '---------') === false) {
1470                 /* no delimiter found, so add this to the last-log */
1471                 $lastLog[] = $tmp[$i];
1472             } else {
1473                 /* delimiter found, so there is no more line left! */
1474                 break;
1475             }
1476         }
1477
1478         /*
1479          * Now we have the last log in the array.
1480          * Check if the outdated-string is found...
1481          */
1482         foreach ($lastLog as $line) {
1483             if (strpos(strtolower($line), 'outdated') !== false) {
1484                 /*
1485                  * Outdatet is only info, because if we set this to warning, the server is
1486                  * as long in state warning, as there is a new version of ClamAv which takes
1487                  * sometimes weeks!
1488                  */
1489                 $state = $this->_setState($state, 'info');
1490             }
1491         }
1492
1493         /*
1494          * Return the Result
1495          */
1496         $res['server_id'] = $server_id;
1497         $res['type'] = $type;
1498         $res['data'] = $data;
1499         $res['state'] = $state;
1500         return $res;
1501     }
1502
1503     public function monitorClamAvLog() {
1504         global $conf;
1505
1506         /* the id of the server as int */
1507         $server_id = intval($conf['server_id']);
1508
1509         /** The type of the data */
1510         $type = 'log_clamav';
1511
1512         /* Get the data of the log */
1513         $data = $this->_getLogData($type);
1514
1515         // Todo: the state should be calculated.
1516         $state = 'ok';
1517
1518         /*
1519          * Return the Result
1520          */
1521         $res['server_id'] = $server_id;
1522         $res['type'] = $type;
1523         $res['data'] = $data;
1524         $res['state'] = $state;
1525         return $res;
1526     }
1527
1528     public function monitorIspConfigLog() {
1529         global $conf;
1530
1531         /* the id of the server as int */
1532         $server_id = intval($conf['server_id']);
1533
1534         /** The type of the data */
1535         $type = 'log_ispconfig';
1536
1537         /* Get the data of the log */
1538         $data = $this->_getLogData($type);
1539
1540         // Todo: the state should be calculated.
1541         $state = 'ok';
1542
1543         /*
1544          * Return the Result
1545          */
1546         $res['server_id'] = $server_id;
1547         $res['type'] = $type;
1548         $res['data'] = $data;
1549         $res['state'] = $state;
1550         return $res;
1551     }
1552
1553     public function _getLogData($log) {
1554         global $conf;
1555
1556         $dist = '';
1557         $logfile = '';
1558
1559         if (@is_file('/etc/debian_version')) {
1560             $dist = 'debian';
1561         } elseif (@is_file('/etc/redhat-release')) {
1562             $dist = 'redhat';
1563         } elseif (@is_file('/etc/SuSE-release')) {
1564             $dist = 'suse';
1565         } elseif (@is_file('/etc/gentoo-release')) {
1566             $dist = 'gentoo';
1567         }
1568
1569         switch ($log) {
1570             case 'log_mail':
1571                 if ($dist == 'debian') {
1572                     $logfile = '/var/log/mail.log';
1573                 } elseif ($dist == 'redhat') {
1574                     $logfile = '/var/log/maillog';
1575                 } elseif ($dist == 'suse') {
1576                     $logfile = '/var/log/mail.info';
1577                 } elseif ($dist == 'gentoo') {
1578                     $logfile = '/var/log/maillog';
1579                 }
1580                 break;
1581             case 'log_mail_warn':
1582                 if ($dist == 'debian') {
1583                     $logfile = '/var/log/mail.warn';
1584                 } elseif ($dist == 'redhat') {
1585                     $logfile = '/var/log/maillog';
1586                 } elseif ($dist == 'suse') {
1587                     $logfile = '/var/log/mail.warn';
1588                 } elseif ($dist == 'gentoo') {
1589                     $logfile = '/var/log/maillog';
1590                 }
1591                 break;
1592             case 'log_mail_err':
1593                 if ($dist == 'debian') {
1594                     $logfile = '/var/log/mail.err';
1595                 } elseif ($dist == 'redhat') {
1596                     $logfile = '/var/log/maillog';
1597                 } elseif ($dist == 'suse') {
1598                     $logfile = '/var/log/mail.err';
1599                 } elseif ($dist == 'gentoo') {
1600                     $logfile = '/var/log/maillog';
1601                 }
1602                 break;
1603             case 'log_messages':
1604                 if ($dist == 'debian') {
1605                     $logfile = '/var/log/messages';
1606                 } elseif ($dist == 'redhat') {
1607                     $logfile = '/var/log/messages';
1608                 } elseif ($dist == 'suse') {
1609                     $logfile = '/var/log/messages';
1610                 } elseif ($dist == 'gentoo') {
1611                     $logfile = '/var/log/messages';
1612                 }
1613                 break;
1614             case 'log_ispc_cron':
1615                 if ($dist == 'debian') {
1616                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1617                 } elseif ($dist == 'redhat') {
1618                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1619                 } elseif ($dist == 'suse') {
1620                     $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
1621                 } elseif ($dist == 'gentoo') {
1622                     $logfile = '/var/log/cron';
1623                 }
1624                 break;
1625             case 'log_freshclam':
1626                 if ($dist == 'debian') {
1627                     $logfile = '/var/log/clamav/freshclam.log';
1628                 } elseif ($dist == 'redhat') {
1629                     $logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
1630                 } elseif ($dist == 'suse') {
1631                     $logfile = '/var/log/freshclam.log';
1632                 } elseif ($dist == 'gentoo') {
1633                     $logfile = '/var/log/clamav/freshclam.log';
1634                 }
1635                 break;
1636             case 'log_clamav':
1637                 if ($dist == 'debian') {
1638                     $logfile = '/var/log/clamav/clamav.log';
1639                 } elseif ($dist == 'redhat') {
1640                     $logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
1641                 } elseif ($dist == 'suse') {
1642                     $logfile = '/var/log/clamd.log';
1643                 } elseif ($dist == 'gentoo') {
1644                     $logfile = '/var/log/clamav/clamd.log';
1645                 }
1646                 break;
1647             case 'log_fail2ban':
1648                 if ($dist == 'debian') {
1649                     $logfile = '/var/log/fail2ban.log';
1650                 } elseif ($dist == 'redhat') {
1651                     $logfile = '/var/log/fail2ban.log';
1652                 } elseif ($dist == 'suse') {
1653                     $logfile = '/var/log/fail2ban.log';
1654                 } elseif ($dist == 'gentoo') {
1655                     $logfile = '/var/log/fail2ban.log';
1656                 }
1657                 break;
1658             case 'log_ispconfig':
1659                 if ($dist == 'debian') {
1660                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1661                 } elseif ($dist == 'redhat') {
1662                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1663                 } elseif ($dist == 'suse') {
1664                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1665                 } elseif ($dist == 'gentoo') {
1666                     $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
1667                 }
1668                 break;
1669             default:
1670                 $logfile = '';
1671                 break;
1672         }
1673
1674         // Getting the logfile content
1675         if ($logfile != '') {
1676             $logfile = escapeshellcmd($logfile);
1677             if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) {
1678                 $log = 'Logfile path error.';
1679             } else {
1680                 $log = '';
1681                 if (is_readable($logfile)) {
1682                     $fd = popen('tail -n 100 ' . $logfile, 'r');
1683                     if ($fd) {
1684                         while (!feof($fd)) {
1685                             $log .= fgets($fd, 4096);
1686                             $n++;
1687                             if ($n > 1000)
1688                                 break;
1689                         }
1690                         fclose($fd);
1691                     }
1692                 } else {
1693                     $log = 'Unable to read ' . $logfile;
1694                 }
1695             }
1696         }
1697
1698         return $log;
1699     }
1700
1701     private function _checkTcp($host, $port) {
e5c5af 1702         /* Try to open a connection */
dead5c 1703         $fp = @fsockopen($host, $port, $errno, $errstr, 2);
V 1704
1705         if ($fp) {
8bbcc1 1706             /*
e5c5af 1707              * We got a connection, this means, everything is O.K.
V 1708              * But maybe we are able to do more deep testing?
8bbcc1 1709              */
e5c5af 1710             if ($port == 80) {
V 1711                 /*
1712                  * Port 80 means, testing APACHE
1713                  * So we can do a deepter test and try to get data over this connection.
1714                  * (if apache hangs, we get a connection but a timeout by trying to GET the data!)
1715                  */
1716                 fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
1717                 stream_set_timeout($fp, 5); // Timeout after 5 seconds
1718                 $res = fread($fp, 10);  // try to get 10 bytes (enough to test!)
1719                 $info = stream_get_meta_data($fp);
1720                 if ($info['timed_out']) {
1721                     return false; // Apache was not able to send data over this connection
1722                 }
eed6b3 1723             }
e5c5af 1724
V 1725             /* The connection is no longer needed */
1726             fclose($fp);
1727             /* We are able to establish a connection */
1728             return true;
dead5c 1729         } else {
e5c5af 1730             /* We are NOT able to establish a connection */
V 1731             return false;
dead5c 1732         }
V 1733     }
1734
1735     private function _checkUdp($host, $port) {
1736
1737         $fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2);
1738
1739         if ($fp) {
1740             fclose($fp);
1741             return true;
1742         } else {
1743             return false;
1744         }
1745     }
1746
1747     private function _checkFtp($host, $port) {
1748
1749         $conn_id = @ftp_connect($host, $port);
1750
1751         if ($conn_id) {
1752             @ftp_close($conn_id);
1753             return true;
1754         } else {
1755             return false;
1756         }
1757     }
eed6b3 1758
e5c5af 1759     /**
V 1760      * Set the state to the given level (or higher, but not lesser).
1761      * * If the actual state is critical and you call the method with ok,
1762      *   then the state is critical.
1763      *
1764      * * If the actual state is critical and you call the method with error,
1765      *   then the state is error.
1766      */
1767     private function _setState($oldState, $newState) {
1768         /*
1769          * Calculate the weight of the old state
1770          */
1771         switch ($oldState) {
1772             case 'no_state': $oldInt = 0;
1773                 break;
1774             case 'ok': $oldInt = 1;
1775                 break;
1776             case 'unknown': $oldInt = 2;
1777                 break;
1778             case 'info': $oldInt = 3;
1779                 break;
1780             case 'warning': $oldInt = 4;
1781                 break;
1782             case 'critical': $oldInt = 5;
1783                 break;
1784             case 'error': $oldInt = 6;
1785                 break;
1786         }
1787         /*
1788          * Calculate the weight of the new state
1789          */
1790         switch ($newState) {
1791             case 'no_state': $newInt = 0;
1792                 break;
1793             case 'ok': $newInt = 1;
1794                 break;
1795             case 'unknown': $newInt = 2;
1796                 break;
1797             case 'info': $newInt = 3;
1798                 break;
1799             case 'warning': $newInt = 4;
1800                 break;
1801             case 'critical': $newInt = 5;
1802                 break;
1803             case 'error': $newInt = 6;
1804                 break;
1805         }
1806
1807         /*
1808          * Set to the higher level
1809          */
1810         if ($newInt > $oldInt) {
1811             return $newState;
1812         } else {
1813             return $oldState;
1814         }
1815     }
dead5c 1816
V 1817     private function _getIntArray($line) {
1818         /** The array of float found */
1819         $res = array();
1820         /* First build a array from the line */
1821         $data = explode(' ', $line);
1822         /* then check if any item is a float */
1823         foreach ($data as $item) {
1824             if ($item . '' == (int) $item . '') {
1825                 $res[] = $item;
1826             }
1827         }
1828         return $res;
1829     }
1830
1831 }
1832
7cd997 1833 ?>