Marius Burkard
2016-05-04 c3189ce6c7301c3ec17878fd3918f31d0d3cb18a
commit | author | age
dead5c 1 <?php
V 2
3 /*
996bad 4     Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
M 5     All rights reserved.
dead5c 6
996bad 7     Redistribution and use in source and binary forms, with or without modification,
M 8     are permitted provided that the following conditions are met:
dead5c 9
V 10  * Redistributions of source code must retain the above copyright notice,
996bad 11     this list of conditions and the following disclaimer.
dead5c 12  * Redistributions in binary form must reproduce the above copyright notice,
996bad 13     this list of conditions and the following disclaimer in the documentation
M 14     and/or other materials provided with the distribution.
dead5c 15  * Neither the name of ISPConfig nor the names of its contributors
996bad 16     may be used to endorse or promote products derived from this software without
M 17     specific prior written permission.
dead5c 18
996bad 19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
M 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.
dead5c 29  */
V 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')) {
f35123 47             if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu')) {
T 48                 if (strstr(trim(file_get_contents('/etc/issue')), 'LTS')) {
49                     $lts=" LTS";
50                 } else {
51                     $lts="";
52                 }
53
d281bb 54                 $issue=file_get_contents('/etc/issue');
C 55                 $distname = 'Ubuntu';
56                 $distid = 'debian40';
57                 $distbaseid = 'debian';
b1a6a5 58                 $ver = explode(' ', $issue);
d281bb 59                 $ver = array_filter($ver);
C 60                 $ver = next($ver);
b1a6a5 61                 $mainver = explode('.', $ver);
d281bb 62                 $mainver = array_filter($mainver);
C 63                 $mainver = current($mainver).'.'.next($mainver);
64                 switch ($mainver){
480fef 65                 case "16.04":
TB 66                     $relname = "(Xenial Xerus)";
67                     $distid = 'ubuntu1604';
68                     break;
0a0647 69                 case "15.10":
TB 70                     $relname = "(Wily Werewolf)";
71                     break;
ed46b4 72                 case "15.04":
TB 73                     $relname = "(Vivid Vervet)";
74                     break;
bc04c3 75                 case "14.10":
TB 76                     $relname = "(Utopic Unicorn)";
77                     break;
be2cbb 78                 case "14.04":
TB 79                     $relname = "(Trusty Tahr)";
80                     break;
81                 case "13.10":
82                     $relname = "(Saucy Salamander)";
83                     break;
84                 case "13.04":
85                     $relname = "(Raring Ringtail)";
86                     break;
d281bb 87                 case "12.10":
C 88                     $relname = "(Quantal Quetzal)";
b1a6a5 89                     break;
d281bb 90                 case "12.04":
C 91                     $relname = "(Precise Pangolin)";
b1a6a5 92                     break;
d281bb 93                 case "11.10":
C 94                     $relname = "(Oneiric Ocelot)";
b1a6a5 95                     break;
d281bb 96                 case "11.14":
C 97                     $relname = "(Natty Narwhal)";
b1a6a5 98                     break;
d281bb 99                 case "10.10":
C 100                     $relname = "(Maverick Meerkat)";
b1a6a5 101                     break;
d281bb 102                 case "10.04":
C 103                     $relname = "(Lucid Lynx)";
b1a6a5 104                     break;
d281bb 105                 case "9.10":
C 106                     $relname = "(Karmic Koala)";
b1a6a5 107                     break;
d281bb 108                 case "9.04":
C 109                     $relname = "(Jaunty Jackpole)";
b1a6a5 110                     break;
d281bb 111                 case "8.10":
b1a6a5 112                     $relname = "(Intrepid Ibex)";
MC 113                     break;
d281bb 114                 case "8.04":
C 115                     $relname = "(Hardy Heron)";
b1a6a5 116                     break;
d281bb 117                 case "7.10":
C 118                     $relname = "(Gutsy Gibbon)";
b1a6a5 119                     break;
d281bb 120                 case "7.04":
C 121                     $relname = "(Feisty Fawn)";
b1a6a5 122                     break;
d281bb 123                 case "6.10":
C 124                     $relname = "(Edgy Eft)";
b1a6a5 125                     break;
d281bb 126                 case "6.06":
C 127                     $relname = "(Dapper Drake)";
b1a6a5 128                     break;
d281bb 129                 case "5.10":
C 130                     $relname = "(Breezy Badger)";
b1a6a5 131                     break;
d281bb 132                 case "5.04":
C 133                     $relname = "(Hoary Hedgehog)";
b1a6a5 134                     break;
d281bb 135                 case "4.10":
C 136                     $relname = "(Warty Warthog)";
b1a6a5 137                     break;
d281bb 138                 default:
C 139                     $relname = "UNKNOWN";
140                 }
141                 $distver = $ver.$lts." ".$relname;
142             } elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
dead5c 143                 $distname = 'Debian';
V 144                 $distver = '4.0';
145                 $distid = 'debian40';
146                 $distbaseid = 'debian';
147             } elseif (strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
148                 $distname = 'Debian';
149                 $distver = 'Lenny';
150                 $distid = 'debian40';
151                 $distbaseid = 'debian';
152             } elseif (strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
153                 $distname = 'Debian';
154                 $distver = 'Squeeze/Sid';
155                 $distid = 'debian60';
156                 $distbaseid = 'debian';
82ff62 157             } elseif (strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || substr(trim(file_get_contents('/etc/debian_version')),0,2) == '7.' || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') {
996bad 158                 $distname = 'Debian';
M 159                 $distver = 'Wheezy/Sid';
d6aef1 160                 $distid = 'debian60';
996bad 161                 $distbaseid = 'debian';
6a61a1 162             } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '8') || substr(trim(file_get_contents('/etc/debian_version')),0,1) == '8') {
TB 163                 $distname = 'Debian';
164                 $distver = 'Jessie';
165                 $distid = 'debian60';
166                 $distbaseid = 'debian';
dead5c 167             } else {
V 168                 $distname = 'Debian';
169                 $distver = 'Unknown';
170                 $distid = 'debian40';
171                 $distbaseid = 'debian';
172             }
173         }
174
175         //** OpenSuSE
176         elseif (file_exists('/etc/SuSE-release')) {
177             if (stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
178                 $distname = 'openSUSE';
179                 $distver = '11.0';
180                 $distid = 'opensuse110';
181                 $distbaseid = 'opensuse';
182             } elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
183                 $distname = 'openSUSE';
184                 $distver = '11.1';
185                 $distid = 'opensuse110';
186                 $distbaseid = 'opensuse';
187             } elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
188                 $distname = 'openSUSE';
189                 $distver = '11.1';
190                 $distid = 'opensuse110';
191                 $distbaseid = 'opensuse';
192             } else {
193                 $distname = 'openSUSE';
194                 $distver = 'Unknown';
195                 $distid = 'opensuse110';
196                 $distbaseid = 'opensuse';
197             }
198         }
199
200
201         //** Redhat
202         elseif (file_exists('/etc/redhat-release')) {
203
204             $content = file_get_contents('/etc/redhat-release');
205
206             if (stristr($content, 'Fedora release 9 (Sulphur)')) {
207                 $distname = 'Fedora';
208                 $distver = '9';
209                 $distid = 'fedora9';
210                 $distbaseid = 'fedora';
211             } elseif (stristr($content, 'Fedora release 10 (Cambridge)')) {
212                 $distname = 'Fedora';
213                 $distver = '10';
214                 $distid = 'fedora9';
215                 $distbaseid = 'fedora';
216             } elseif (stristr($content, 'Fedora release 10')) {
217                 $distname = 'Fedora';
218                 $distver = '11';
219                 $distid = 'fedora9';
220                 $distbaseid = 'fedora';
221             } elseif (stristr($content, 'CentOS release 5.2 (Final)')) {
222                 $distname = 'CentOS';
223                 $distver = '5.2';
224                 $distid = 'centos52';
225                 $distbaseid = 'fedora';
226             } elseif (stristr($content, 'CentOS release 5.3 (Final)')) {
227                 $distname = 'CentOS';
228                 $distver = '5.3';
229                 $distid = 'centos53';
230                 $distbaseid = 'fedora';
be2cbb 231             } elseif(stristr($content, 'CentOS Linux release 6')) {
TB 232                 $distname = 'CentOS';
233                 $distver = 'Unknown';
234                 $distid = 'centos53';
235                 $distbaseid = 'fedora';
236             } elseif(stristr($content, 'CentOS Linux release 7')) {
237                 $distname = 'CentOS';
238                 $distver = 'Unknown';
239                 $distid = 'centos53';
240                 $distbaseid = 'fedora';
dead5c 241             } else {
V 242                 $distname = 'Redhat';
243                 $distver = 'Unknown';
244                 $distid = 'fedora9';
245                 $distbaseid = 'fedora';
246             }
247         }
248
249         //** Gentoo
250         elseif (file_exists('/etc/gentoo-release')) {
251
252             $content = file_get_contents('/etc/gentoo-release');
253
254             preg_match_all('/([0-9]{1,2})/', $content, $version);
255             $distname = 'Gentoo';
256             $distver = $version[0][0] . $version[0][1];
257             $distid = 'gentoo';
258             $distbaseid = 'gentoo';
259         } else {
260             die('Unrecognized GNU/Linux distribution');
261         }
262
263         return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
264     }
265
b1a6a5 266     // this function remains in the tools class, because it is used by cron AND rescue
dead5c 267     public function monitorServices() {
V 268         global $app;
269         global $conf;
270
271         /** the id of the server as int */
b1a6a5 272
MC 273
dead5c 274         $server_id = intval($conf['server_id']);
V 275
eed6b3 276         /**  get the "active" Services of the server from the DB */
cc7a82 277         $services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ?', $server_id);
eed6b3 278         /*
V 279          * If the DB is down, we have to set the db to "yes".
280          * If we don't do this, then the monitor will NOT monitor, that the db is down and so the
281          * rescue-module can not try to rescue the db
282          */
283         if ($services == null) {
284             $services['db_server'] = 1;
285         }
dead5c 286
V 287         /* The type of the Monitor-data */
288         $type = 'services';
289
290         /** the State of the monitoring */
291         /* ok, if ALL active services are running,
292          * error, if not
293          * There is no other state!
294          */
295         $state = 'ok';
296
297         /* Monitor Webserver */
298         $data['webserver'] = -1; // unknown - not needed
299         if ($services['web_server'] == 1) {
300             if ($this->_checkTcp('localhost', 80)) {
301                 $data['webserver'] = 1;
302             } else {
303                 $data['webserver'] = 0;
304                 $state = 'error'; // because service is down
305             }
306         }
307
308         /* Monitor FTP-Server */
309         $data['ftpserver'] = -1; // unknown - not needed
310         if ($services['file_server'] == 1) {
311             if ($this->_checkFtp('localhost', 21)) {
312                 $data['ftpserver'] = 1;
313             } else {
314                 $data['ftpserver'] = 0;
315                 $state = 'error'; // because service is down
316             }
317         }
318
319         /* Monitor SMTP-Server */
320         $data['smtpserver'] = -1; // unknown - not needed
321         if ($services['mail_server'] == 1) {
322             if ($this->_checkTcp('localhost', 25)) {
323                 $data['smtpserver'] = 1;
324             } else {
325                 $data['smtpserver'] = 0;
326                 $state = 'error'; // because service is down
327             }
328         }
329
330         /* Monitor POP3-Server */
331         $data['pop3server'] = -1; // unknown - not needed
332         if ($services['mail_server'] == 1) {
333             if ($this->_checkTcp('localhost', 110)) {
334                 $data['pop3server'] = 1;
335             } else {
336                 $data['pop3server'] = 0;
337                 $state = 'error'; // because service is down
338             }
339         }
340
341         /* Monitor IMAP-Server */
342         $data['imapserver'] = -1; // unknown - not needed
343         if ($services['mail_server'] == 1) {
344             if ($this->_checkTcp('localhost', 143)) {
345                 $data['imapserver'] = 1;
346             } else {
347                 $data['imapserver'] = 0;
348                 $state = 'error'; // because service is down
349             }
350         }
351
352         /* Monitor BIND-Server */
353         $data['bindserver'] = -1; // unknown - not needed
354         if ($services['dns_server'] == 1) {
dfcd55 355             if ($this->_checkUdp('localhost', 53)) {
dead5c 356                 $data['bindserver'] = 1;
V 357             } else {
358                 $data['bindserver'] = 0;
359                 $state = 'error'; // because service is down
360             }
361         }
362
363         /* Monitor MySQL Server */
364         $data['mysqlserver'] = -1; // unknown - not needed
365         if ($services['db_server'] == 1) {
366             if ($this->_checkTcp('localhost', 3306)) {
367                 $data['mysqlserver'] = 1;
368             } else {
369                 $data['mysqlserver'] = 0;
370                 $state = 'error'; // because service is down
371             }
372         }
e7ae4e 373 /*
b1a6a5 374         $data['mongodbserver'] = -1;
MC 375         if ($this->_checkTcp('localhost', 27017)) {
376             $data['mongodbserver'] = 1;
377         } else {
378             $data['mongodbserver'] = 0;
e7ae4e 379 */
b1a6a5 380             //$state = 'error'; // because service is down
MC 381             /* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */
e7ae4e 382 //        }
def897 383
dead5c 384         /*
V 385          * Return the Result
386          */
387         $res['server_id'] = $server_id;
388         $res['type'] = $type;
389         $res['data'] = $data;
390         $res['state'] = $state;
391         return $res;
392     }
b1a6a5 393
dead5c 394     public function _getLogData($log) {
V 395         global $conf;
396
397         $dist = '';
398         $logfile = '';
399
400         if (@is_file('/etc/debian_version')) {
401             $dist = 'debian';
402         } elseif (@is_file('/etc/redhat-release')) {
403             $dist = 'redhat';
404         } elseif (@is_file('/etc/SuSE-release')) {
405             $dist = 'suse';
406         } elseif (@is_file('/etc/gentoo-release')) {
407             $dist = 'gentoo';
408         }
409
410         switch ($log) {
b1a6a5 411         case 'log_mail':
MC 412             if ($dist == 'debian') {
413                 $logfile = '/var/log/mail.log';
414             } elseif ($dist == 'redhat') {
415                 $logfile = '/var/log/maillog';
416             } elseif ($dist == 'suse') {
417                 $logfile = '/var/log/mail.info';
418             } elseif ($dist == 'gentoo') {
419                 $logfile = '/var/log/maillog';
420             }
421             break;
422         case 'log_mail_warn':
423             if ($dist == 'debian') {
424                 $logfile = '/var/log/mail.warn';
425             } elseif ($dist == 'redhat') {
426                 $logfile = '/var/log/maillog';
427             } elseif ($dist == 'suse') {
428                 $logfile = '/var/log/mail.warn';
429             } elseif ($dist == 'gentoo') {
430                 $logfile = '/var/log/maillog';
431             }
432             break;
433         case 'log_mail_err':
434             if ($dist == 'debian') {
435                 $logfile = '/var/log/mail.err';
436             } elseif ($dist == 'redhat') {
437                 $logfile = '/var/log/maillog';
438             } elseif ($dist == 'suse') {
439                 $logfile = '/var/log/mail.err';
440             } elseif ($dist == 'gentoo') {
441                 $logfile = '/var/log/maillog';
442             }
443             break;
444         case 'log_messages':
445             if ($dist == 'debian') {
446                 $logfile = '/var/log/syslog';
447             } elseif ($dist == 'redhat') {
448                 $logfile = '/var/log/messages';
449             } elseif ($dist == 'suse') {
450                 $logfile = '/var/log/messages';
451             } elseif ($dist == 'gentoo') {
452                 $logfile = '/var/log/messages';
453             }
454             break;
455         case 'log_ispc_cron':
456             if ($dist == 'debian') {
457                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
458             } elseif ($dist == 'redhat') {
459                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
460             } elseif ($dist == 'suse') {
461                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
462             } elseif ($dist == 'gentoo') {
463                 $logfile = '/var/log/cron';
464             }
465             break;
466         case 'log_freshclam':
467             if ($dist == 'debian') {
468                 $logfile = '/var/log/clamav/freshclam.log';
469             } elseif ($dist == 'redhat') {
470                 $logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
471             } elseif ($dist == 'suse') {
472                 $logfile = '/var/log/freshclam.log';
473             } elseif ($dist == 'gentoo') {
474                 $logfile = '/var/log/clamav/freshclam.log';
475             }
476             break;
477         case 'log_clamav':
478             if ($dist == 'debian') {
479                 $logfile = '/var/log/clamav/clamav.log';
480             } elseif ($dist == 'redhat') {
481                 $logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
482             } elseif ($dist == 'suse') {
483                 $logfile = '/var/log/clamd.log';
484             } elseif ($dist == 'gentoo') {
485                 $logfile = '/var/log/clamav/clamd.log';
486             }
487             break;
488         case 'log_fail2ban':
489             if ($dist == 'debian') {
490                 $logfile = '/var/log/fail2ban.log';
491             } elseif ($dist == 'redhat') {
492                 $logfile = '/var/log/fail2ban.log';
493             } elseif ($dist == 'suse') {
494                 $logfile = '/var/log/fail2ban.log';
495             } elseif ($dist == 'gentoo') {
496                 $logfile = '/var/log/fail2ban.log';
497             }
498             break;
499         case 'log_mongodb':
500             $logfile = '/var/log/mongodb/mongodb.log';
501             break;
502         case 'log_ispconfig':
503             if ($dist == 'debian') {
504                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
505             } elseif ($dist == 'redhat') {
506                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
507             } elseif ($dist == 'suse') {
508                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
509             } elseif ($dist == 'gentoo') {
510                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
511             }
512             break;
513         default:
514             $logfile = '';
515             break;
dead5c 516         }
V 517
518         // Getting the logfile content
519         if ($logfile != '') {
520             $logfile = escapeshellcmd($logfile);
521             if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) {
522                 $log = 'Logfile path error.';
523             } else {
524                 $log = '';
525                 if (is_readable($logfile)) {
526                     $fd = popen('tail -n 100 ' . $logfile, 'r');
527                     if ($fd) {
528                         while (!feof($fd)) {
529                             $log .= fgets($fd, 4096);
530                             $n++;
531                             if ($n > 1000)
532                                 break;
533                         }
534                         fclose($fd);
535                     }
536                 } else {
537                     $log = 'Unable to read ' . $logfile;
538                 }
539             }
540         }
541
542         return $log;
543     }
544
545     private function _checkTcp($host, $port) {
e5c5af 546         /* Try to open a connection */
dead5c 547         $fp = @fsockopen($host, $port, $errno, $errstr, 2);
V 548
549         if ($fp) {
8bbcc1 550             /*
e5c5af 551              * We got a connection, this means, everything is O.K.
V 552              * But maybe we are able to do more deep testing?
8bbcc1 553              */
e5c5af 554             if ($port == 80) {
V 555                 /*
556                  * Port 80 means, testing APACHE
557                  * So we can do a deepter test and try to get data over this connection.
558                  * (if apache hangs, we get a connection but a timeout by trying to GET the data!)
559                  */
8cf78b 560                 // fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
T 561                 $out = "GET / HTTP/1.1\r\n";
562                 $out .= "Host: localhost\r\n";
563                 $out .= "User-Agent: Mozilla/5.0 (ISPConfig monitor)\r\n";
564                 $out .= "Accept: application/xml,application/xhtml+xml,text/html\r\n";
565                 $out .= "Connection: Close\r\n\r\n";
566                 fwrite($fp, $out);
e5c5af 567                 stream_set_timeout($fp, 5); // Timeout after 5 seconds
V 568                 $res = fread($fp, 10);  // try to get 10 bytes (enough to test!)
569                 $info = stream_get_meta_data($fp);
570                 if ($info['timed_out']) {
571                     return false; // Apache was not able to send data over this connection
572                 }
eed6b3 573             }
e5c5af 574
V 575             /* The connection is no longer needed */
576             fclose($fp);
577             /* We are able to establish a connection */
578             return true;
dead5c 579         } else {
e5c5af 580             /* We are NOT able to establish a connection */
V 581             return false;
dead5c 582         }
V 583     }
584
585     private function _checkUdp($host, $port) {
586
587         $fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2);
588
589         if ($fp) {
590             fclose($fp);
591             return true;
592         } else {
593             return false;
594         }
595     }
596
597     private function _checkFtp($host, $port) {
598
599         $conn_id = @ftp_connect($host, $port);
600
601         if ($conn_id) {
602             @ftp_close($conn_id);
603             return true;
604         } else {
605             return false;
606         }
607     }
eed6b3 608
e5c5af 609     /**
V 610      * Set the state to the given level (or higher, but not lesser).
611      * * If the actual state is critical and you call the method with ok,
612      *   then the state is critical.
613      *
614      * * If the actual state is critical and you call the method with error,
615      *   then the state is error.
616      */
5de2af 617     public function _setState($oldState, $newState) {
e5c5af 618         /*
V 619          * Calculate the weight of the old state
620          */
621         switch ($oldState) {
b1a6a5 622         case 'no_state': $oldInt = 0;
MC 623             break;
624         case 'ok': $oldInt = 1;
625             break;
626         case 'unknown': $oldInt = 2;
627             break;
628         case 'info': $oldInt = 3;
629             break;
630         case 'warning': $oldInt = 4;
631             break;
632         case 'critical': $oldInt = 5;
633             break;
634         case 'error': $oldInt = 6;
635             break;
e5c5af 636         }
V 637         /*
638          * Calculate the weight of the new state
639          */
640         switch ($newState) {
b1a6a5 641         case 'no_state': $newInt = 0;
MC 642             break;
643         case 'ok': $newInt = 1;
644             break;
645         case 'unknown': $newInt = 2;
646             break;
647         case 'info': $newInt = 3;
648             break;
649         case 'warning': $newInt = 4;
650             break;
651         case 'critical': $newInt = 5;
652             break;
653         case 'error': $newInt = 6;
654             break;
e5c5af 655         }
V 656
657         /*
658          * Set to the higher level
659          */
660         if ($newInt > $oldInt) {
661             return $newState;
662         } else {
663             return $oldState;
664         }
665     }
dead5c 666
5de2af 667     /**
M 668      * Deletes Records older than 4 minutes.
669      * The monitor writes new data every 5 minutes or longer (4 hour, 1 day).
670      * So if i delete all Date older than 4 minutes i can be sure, that all old data
671      * are deleted...
672      */
673     public function delOldRecords($type, $serverId) {
674         global $app;
675
676         // $now = time();
677         // $old = $now - (4 * 60); // 4 minutes
6a3742 678         $old = 240; //seconds
5de2af 679
M 680         /*
681          * ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type)
682          * of ALL servers. This means, if i have a multiserver-environment and a server has a
683          * time not synced with the others (for example, all server has 11:00 and ONE server has
684          * 10:45) then the actual data of this server (with the time-stamp 10:45) get lost
685          * even though it is the NEWEST data of this server. To avoid this i HAVE to include
686          * the server-id!
687          */
8081df 688         $sql = 'DELETE FROM `monitor_data` WHERE `type` = ? AND `created` < UNIX_TIMESTAMP() - ? AND `server_id` = ?';
cc7a82 689         $app->dbmaster->query($sql, $type, $old, $serverId);
dead5c 690     }
V 691
a20430 692     public function send_notification_email($template, $placeholders, $recipients) {
FS 693         global $conf;
694
695         if(!is_array($recipients) || count($recipients) < 1) return false;
696         if(!is_array($placeholders)) $placeholders = array();
697
698         if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) {
699             $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt');
700         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) {
701             $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt');
702         } elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) {
703             $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt');
704         } else {
705             $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt');
706         }
707
708         //* get mail headers, subject and body
709         $mailHeaders = '';
710         $mailBody = '';
711         $mailSubject = '';
712         $inHeader = true;
713         for($l = 0; $l < count($lines); $l++) {
d8faa4 714             if(trim($lines[$l]) == '') {
a20430 715                 $inHeader = false;
FS 716                 continue;
717             }
718             if($inHeader == true) {
719                 $parts = explode(':', $lines[$l], 2);
b31c14 720                 if(strtolower($parts[0]) == 'subject') {
MB 721                     $mailSubject = trim($parts[1]);
722                     continue;
723                 }
a20430 724                 unset($parts);
FS 725                 $mailHeaders .= trim($lines[$l]) . "\n";
726             } else {
727                 $mailBody .= trim($lines[$l]) . "\n";
728             }
729         }
730         $mailBody = trim($mailBody);
731
732         //* Replace placeholders
733         $mailHeaders = strtr($mailHeaders, $placeholders);
734         $mailSubject = strtr($mailSubject, $placeholders);
735         $mailBody = strtr($mailBody, $placeholders);
736
737         for($r = 0; $r < count($recipients); $r++) {
738             mail($recipients[$r], $mailSubject, $mailBody, $mailHeaders);
739         }
740
741         unset($mailSubject);
742         unset($mailHeaders);
743         unset($mailBody);
744         unset($lines);
745
746         return true;
747     }
5de2af 748
dead5c 749 }
V 750
2332b2 751 ?>