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