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